short · 3 min
Context compaction
Shrinking the old part of a long agent run, by summary or by hiding old tool output, so it keeps fitting in the window.
updated 29 Sept 2026
Fifty steps into fixing a bug, a coding agent's context is mostly junk it already read: whole files, long test logs, directory listings. Every step of the agent loop added more, every call pays for all of it again, and a fuller context window makes the model less precise. We need to shrink the old part so the run can keep going. That's compaction, the "compress" move from context engineering.
The obvious idea is to have a model summarize it. Read the old turns, write a summary, swap the summary in, keep the last few turns word for word. Claude Code does roughly this: it summarizes the history and carries on with the summary plus the five files it opened most recently. Claude's API can now do it on the server too (in beta as of 2026-09), either when you ask or once input passes a token threshold (150,000 by default).
So that's settled? Let's look at a cheaper idea first.
Just hide the old output
Leave the agent's own reasoning and tool calls alone. Only replace tool output older than, say, 10 turns with a one-line placeholder like "Previous 8 lines omitted for brevity". The agent still sees what it did and why. It just can't reread raw output it already used.
JetBrains researchers pitted the two against each other on a coding benchmark. Both cut cost by roughly half compared with doing nothing. And masking solved about as many tasks as summaries while costing a bit less. On one model, masking solved 54.8% at $0.61 a task and summaries 53.8% at $0.64.
Why would the fancy version lose? Two reasons came out. Writing the summary is itself a model call, billed like any other (up to 7.2% of a task's cost), and each one reads a different stretch of history, so prompt caching barely helps. And summaries made runs longer. With one model, runs averaged 52 turns with summaries against 44 with masking. The authors' guess is that a tidy summary nudges the agent to keep going.
Best of all was a mix: mask as you go, summarize only as a last resort.
Don't stretch this too far, though. It's coding tasks, where tool output is long and wordy, and the authors say it may not hold when output is short. OpenAI has since stopped reporting that benchmark, so trust the comparison more than the solve rates.
What a summary throws away
Summaries are hard to get right. Cognition went as far as fine-tuning a smaller model whose only job is to compress histories into key details, events and decisions. If you use Claude's default, you can swap in your own prompt when it keeps dropping something you need.
And some things don't survive at all. Images, documents and fetched pages in the summarized turns are gone. So are instructions that came in mid-conversation, because they get summarized along with everything else. There's a quiet plumbing trap too: keep the old messages after the summary block and they get sent again; drop the block and the model gets no summary. Neither one throws an error.
Remember our agent fifty steps in? Say at step 3 we told it "don't touch the migrations folder". After compaction, that rule might be a line in a summary, or not there at all. Anything that has to hold for the whole run belongs in the system prompt, and anything that has to outlive the session belongs in memory files, since compaction only keeps one run going.
Further reading
- Compaction overview, Anthropic docs. How server-side summaries work and what they lose. Read the limits section before you turn it on.
- The Complexity Trap: Simple Observation Masking Is as Efficient as LLM Summarization for Agent Context Management, Lindenbauer et al. (JetBrains), 2025. The masking vs summary test, with costs and run lengths.
- Don't Build Multi-Agents, Walden Yan (Cognition), 2025. Mostly about something else, but has the case for a dedicated compression model.
- Effective context engineering for AI agents, Anthropic, 2025. How Claude Code compacts, and why clearing old tool results is the lightest touch.