Context compaction
Context compaction is the step where an agent summarises a conversation that is nearing its context window limit and continues in a fresh window that starts from that summary, so a long task can keep going.
Also called: auto-compact
By Divya Sharma · Checked Nov 15, 2026 · How we review
Every model has a context window, and long agent sessions fill it with tool output, file contents and back-and-forth. When the window runs out, the session would otherwise have to stop. Compaction swaps the full history for a condensed version and carries on.
How it works
The agent, or the harness around it, asks the model to summarise the conversation so far: the goal, decisions made, files touched, open problems and next steps. The old messages are dropped and the summary becomes the start of a new context. Some tools do this automatically when usage crosses a threshold, often called auto-compact; Claude Code also lets you trigger it by hand with /compact. A cheaper variant clears old tool results while keeping the rest.
Why it matters
Compaction keeps long tasks alive, and it can also improve answers, because a model working from a short, focused context is less exposed to context rot. Anthropic describes it as the first lever for keeping agents coherent over long runs.
Compaction versus memory
Compaction rewrites the current session. Agent memory stores facts outside the session to reuse later. Sending side work to a subagent avoids filling the window in the first place.
Limits
A summary loses detail. An exact error message, a subtle constraint or an early instruction can vanish, and the agent then repeats work or breaks a rule it had followed. Keep key rules in an instructions file that is reloaded, and re-check important details after a compaction.
Related terms
- Context rot
- Context rot is the drop in a language model's accuracy and reliability as its input grows longer, even when the input still fits inside the context window and the needed information is present.
- Agent memory
- Agent memory is the set of techniques that let an AI agent keep information beyond a single context window, such as facts about a user or past work, by storing it outside the model and loading it back when needed.
- Context window
- A context window is the maximum amount of text, counted in tokens, that a language model can consider at once, covering the system prompt, conversation, documents, tool results and the response it writes.
- Context engineering
- Context engineering is the practice of choosing and maintaining everything that goes into a language model's context window during a task: instructions, retrieved documents, tool definitions, tool results and conversation history.
- Subagent
- A subagent is a helper agent that a main agent hands a task to. It works in its own fresh context window, often with its own instructions and tools, and returns only a summary of the result.