Compaction, Notes & Sub-Agents
The three tools for outlasting a context window: compacting history, externalizing notes, and delegating to fresh-context sub-agents.
When the task outlives the window
However well you budget, some tasks generate more history than any window holds: a multi-hour refactor, a research sweep across fifty sources, an agent playing a game across thousands of moves. Anthropic's context-engineering guidance names exactly three techniques for working past the limit. Each answers the same question — what happens to history the window can't keep? — differently: compress it, externalize it, or never accumulate it.
Technique 1: compaction — compress the history
Compaction summarizes the conversation as the window nears its limit, then reinitiates with the compressed version: recent messages stay verbatim, older history becomes a summary.
Compaction is lossy, so what to preserve is the design problem. Anthropic's tuning advice: maximize recall first, then improve precision. First make sure the summary never drops anything that matters — decisions made, files modified, current plan, unresolved errors, test commands. Only once nothing critical is lost do you trim the summary's verbosity. A compaction that loses "we already tried X and it failed" sends the agent back down a dead end an hour later.
The safest first lever barely deserves the name summarization: clear old tool results. A file read from forty turns ago has been processed — its conclusions are in the conversation. The raw dump is spent fuel, and dropping it is low-risk and high-volume.
In Claude Code this is auto-compact and /compact, which accepts instructions — /compact Focus on the API changes — and CLAUDE.md can pin what compaction must always preserve (say, the list of modified files and the test commands).
Technique 2: structured note-taking — move state outside
Note-taking has the agent write persistent memory outside the context window — a NOTES.md, a todo list, Anthropic's file-based memory tool — and read it back later. The window forgets; the notes don't. Cost: near zero, a handful of tokens per update.
The signature demonstration: Claude playing Pokémon with a notes file tracked progress across thousands of steps — party state, objectives, what worked and failed. After every context reset it read its own notes and resumed as if nothing happened. The same pattern powers multi-hour coding sessions: keep a running log of decisions, dead ends, and next steps, and any future window — or a fresh session tomorrow — re-enters cleanly.
Notes beat compaction where fidelity matters, because the agent chooses at the moment of writing what deserves persistence, with full context still in view — rather than a summarizer later guessing what mattered. Pair formats to content: structured JSON for checklists and pass/fail state, freeform prose for narrative and gotchas.
Technique 3: sub-agents — never accumulate the history
Sub-agent architectures sidestep accumulation entirely: instead of one context absorbing everything, specialist agents explore in their own clean windows and return condensed summaries of 1,000–2,000 tokens while the lead agent coordinates.
A sub-agent might burn 50,000 tokens grepping, reading, and dead-ending its way to an answer. The lead sees only the 1,500-token distillation. The mess happened — in a window that was then thrown away. It's compression by separation of concerns: detailed work isolated, only conclusions promoted.
In Claude Code, this is the Task/Agent tool with the built-in Explore agent and custom subagents; a "use a subagent to investigate X" prompt keeps the investigation out of your main conversation entirely.
Choosing by task shape
| Task shape | Best fit | Why |
|---|---|---|
| Long conversational back-and-forth (iterating on one problem for hours) | Compaction | flow must continue; history is one continuous thread |
| Iterative development with clear milestones (migrations, multi-day features) | Note-taking | milestones checkpoint naturally; sessions re-enter from notes |
| Parallel exploration-heavy research (many independent strands) | Sub-agents | strands don't need each other's details, only conclusions |
And the Claude Code mapping in one line: compaction → auto-compact and /compact; note-taking → CLAUDE.md and memory files; sub-agents → the Task tool with Explore and custom agents.
They stack
Real long-horizon systems use all three at once. A day-long migration might run sub-agents to scout each package (exploration stays out of the main window), notes to track per-package status in progress.md (survives every reset), and compaction when the implementation conversation itself runs long (continuity preserved).
One discipline ties the whole module together: decide deliberately what deserves to persist. Compaction decides at summary time, notes decide at write time, sub-agents decide at hand-back time — but in every case something chooses what the future context will know. Leaving that choice to chance is how agents forget the one thing that mattered.