Compaction
Understand This First
- Context Window – compaction addresses context window limits.
- Harness (Agentic) – many harnesses perform compaction automatically.
Context
At the agentic level, compaction is the summarization of prior conversation context to free up space in the context window so that work can continue without starting a fresh thread. It’s a technique for extending the useful life of a conversation when the thread-per-task approach is impractical, either because the task is genuinely long-running or because significant context would be lost by starting over.
Compaction is performed by the harness or by the agent itself. The older parts of the conversation (early explorations, dead-end approaches, resolved sub-problems) are condensed into a summary that captures the state, decisions, and remaining work. The full conversation is then replaced by the summary plus the recent, actively relevant portion.
Problem
How do you continue a productive conversation with an agent when the context window is full but the task isn’t done?
Long, complex tasks (multi-file refactorings, extended debugging sessions, feature implementations that span many components) can exhaust the context window before the work is complete. When this happens, the agent’s output quality degrades: it forgets earlier decisions, contradicts its own work, or loses track of the overall plan. Starting a completely fresh thread risks losing context about what’s been tried, what’s worked, and what remains.
Forces
- Context window limits are hard. Once full, new information pushes old information out.
- Long tasks exist. Not everything fits neatly into a single-thread conversation.
- Context quality degrades gradually. The agent doesn’t announce that it’s forgetting; it just gets worse.
- Summary loss: any summarization discards detail that might later prove important.
Solution
When a conversation approaches context limits, compact the history: summarize what’s been accomplished, what decisions have been made, what the current state is, and what work remains. Replace the full conversation history with this summary plus the most recent, actively relevant exchanges.
Good compaction captures:
Decisions made. What approaches were chosen and why. What alternatives were considered and rejected.
Current state. What files have been modified, what tests are passing or failing, what the code looks like now.
Remaining work. What still needs to be done, in what order.
Key constraints. Any constraints or conventions established during the conversation that the agent needs to continue following.
Some harnesses perform compaction automatically when the context approaches its limit. Others require the human to request it (“summarize our progress so far and continue”). Either way, the human should review the summary to ensure nothing critical was lost.
If you anticipate a long task, periodically ask the agent to summarize progress even before the context window is full. This creates natural checkpoints and catches misunderstandings early, similar to a standup meeting during a long sprint.
How It Plays Out
A developer is debugging a complex issue that involves tracing execution across five modules. After ninety minutes and hundreds of messages, the agent starts repeating suggestions it made earlier, a sign that the early context has fallen out of effective memory. The developer asks the agent to compact: “Summarize what we’ve tried, what we’ve learned, and what we should try next.” The summary captures the three failed hypotheses, the two promising leads, and the current state of the code. The conversation continues from the summary with renewed clarity.
A harness automatically compacts the context when it reaches eighty percent capacity. The compaction preserves the current task description, the list of modified files, the most recent test results, and the active plan. Older exchanges (exploratory file reads, rejected approaches, tangential discussions) are condensed to a few sentences each. The agent continues working without interruption, and the developer may not even notice the compaction happened.
“We’ve been working on this for a while and the context is getting long. Summarize what we’ve accomplished, what’s still broken, and what approach we should try next. Then continue from that summary.”
Consequences
Compaction extends the useful life of a conversation, allowing complex tasks to proceed without losing all accumulated context. It’s especially useful for tasks that resist decomposition into independent subagent subtasks, where the work is genuinely sequential and each step builds on the previous one.
The cost is information loss. Summarization inevitably discards detail. A fact that seemed unimportant at compaction time may prove critical later. The remedy is to keep compaction summaries thorough about decisions and state, even at the expense of verbosity, and to maintain a progress log outside the conversation that preserves a durable record.
Related Patterns
- Depends on: Context Window — compaction addresses context window limits.
- Contrasts with: Thread-per-Task — starting fresh is the alternative to compacting.
- Enables: Progress Log — compaction summaries can feed the progress log.
- Uses: Context Engineering — compaction is a context engineering technique.
- Depends on: Harness (Agentic) — many harnesses perform compaction automatically.