Compaction
When the context window fills, summarize the conversation and continue from the summary.
Anthropic, Cognition, OpenAI
The problem
A long task needs more tokens than the context window holds.
Long-horizon tasks require agents to maintain coherence, context, and goal-directed behavior over sequences of actions where the token count exceeds the LLM’s context window.
Therefore
Compaction is the practice of taking a conversation nearing the context window limit, summarizing its contents, and reinitiating a new context window with the summary.
To support long-running interactions, you can use compaction to reduce context size while preserving state needed for subsequent turns.
In use
Claude Code
In Claude Code, for example, we implement this by passing the message history to the model to summarize and compress the most critical details.
Where it breaks
A summary can drop a detail that turns out to matter later.
The art of compaction lies in the selection of what to keep versus what to discard, as overly aggressive compaction can result in the loss of subtle but critical context whose importance only becomes apparent later.
This is hard to get right. It takes investment into figuring out what ends up being the key information and creating a system that is good at this.
Sources
- Effective context engineering for AI agentsPrithvi Rajasekaran, Ethan Dixon, Carly Ryan, and Jeremy Hadfield, Anthropic, Sep 29, 2025
- Don’t Build Multi-AgentsWalden Yan, Cognition, Jun 12, 2025
- CompactionOpenAI
Added Nov 14, 2026


