Context Compaction and Handoff
When an agent's conversation approaches the window limit, compaction summarises the history and reinitialises a fresh window from the summary; what survives the compression determines whether the agent continues or silently restarts.
A coding agent forty tool calls into a debugging session is holding a window that is 80% stale file reads. The architectural decision it made at turn six, the bug it has not fixed yet, the reason it rejected the obvious approach: all of that is real state, and all of it is buried in tokens that will shortly stop fitting.
Compaction is the operation that resolves this. Take the conversation approaching the limit, summarise it, and reinitialise a new window from the summary. The mechanism is simple. The engineering is entirely in the selection policy.
What a good compaction keeps
Anthropic describes the Claude Code policy directly: the model "preserves architectural decisions, unresolved bugs, and implementation details while discarding redundant tool outputs", then continues with the compressed context plus the five most recently accessed files (Anthropic, Effective context engineering for AI agents, 2025).
Three categories are worth naming separately because they compress differently:
Decisions and their reasons. Irreplaceable and cheap. "We are using the async client because the sync one deadlocks under the test harness" is thirty tokens that prevent a hundred turns of rediscovery.
Open state. The unfixed bug, the pending question, the half-written function. Losing this is what turns compaction into a restart.
Recoverable artefacts. File contents, query results, page text. These should be discarded aggressively, because they can be re-fetched on demand. Keeping a pointer costs ten tokens and keeping the content costs four thousand.
The asymmetry is the whole design: compress hard on anything the agent can re-derive, compress not at all on anything it cannot.
Compaction against the alternatives
Compaction is one of three long-horizon techniques, and they are not interchangeable. Structured note-taking writes state to a file outside the window and reads it back later; Anthropic's Claude-playing-Pokemon example maintains precise tallies across thousands of game steps this way, tracking things like "for the last 1,234 steps I've been training my Pokémon in Route 1". Subagent architectures avoid the problem by never letting exploration into the main window at all.
Compaction suits extended back-and-forth in one continuous task. Note-taking suits iterative development where state outlives any single window. Subagents suit parallel research. Most production agents use all three.
Cognition's variant is worth noting because it goes further: they recommend training a dedicated model whose only job is compressing an action history into key details, events and decisions, on the grounds that general summarisation is not good enough at deciding what a coding agent will need later (Yan, Don't Build Multi-Agents, 2025).
When it breaks
Over-compression loses what only later turns out to matter. Anthropic states this as the central risk: "overly aggressive compaction can result in the loss of subtle but critical context whose importance only becomes apparent later." The compaction step cannot know the future of the task, so it is optimising under genuine uncertainty, not just being careless.
Compaction destroys the KV cache. A new window means a new prefix, so the next turn pays a full prefill on the summary and every subsequent turn loses the cache hits it would have had. On a long session the compaction boundary is a visible latency spike, and if compaction is triggered often it can cost more than the tokens it saved.
Errors accumulate across boundaries. Each compaction summarises a context that already contains a previous summary. Three compactions in, the agent is working from a summary of a summary of a summary, and small distortions introduced early are now unverifiable.
Silent restart. The worst failure is not a crash. It is an agent that compacts away the fact that it already tried an approach, tries it again, fails again, and compacts that away too.
5 flashcards for this concept
Click a card to reveal the answer.