## Context compaction

*Context and knowledge*

*Last edited: 15 October 2026*

Compaction replaces the old part of a conversation’s history with a summary, so that a long agent session fits in the context window. It is cheap when the same model writes the summary on the same prompt prefix before the cache expires: the history is then read from the cache, and you pay mostly for generating the summary.

**In plain words:** Notes from a long meeting. They are easiest to write straight away, while someone who was at the meeting still has all the material in front of them. Anyone who wasn’t at the meeting, or who comes back to it after the weekend, first has to reread all the rough notes. The next person reads only the notes and learns nothing that isn’t in them.

*Interactive widget on the page.*

### When the harness compacts

- At a threshold. When the context nears the limit, the harness summarises the history before the next turn and leaves room in the window for the instruction and the summary itself. Claude Code compacts near the window limit, with the exact threshold depending on the model and configuration; Codex does so by default at 90% of the window (as of October 2026). The Claude API (in beta) and OpenAI can compact on the server, at a threshold you set or on request.
- By hand, once a task is done: you choose the moment instead of waiting for auto-compaction to arrive mid-work. `/compact` in Claude Code takes an instruction on what to keep. If you are abandoning a dead end, `/rewind` to an earlier turn is cheaper than compaction, because it returns to a prefix that is already in the cache.
- Before the cache expires. Claude Code has idle compaction, announced in version 2.1.299 (14 October 2026): a conversation of 200k tokens or more (the default threshold) with no request for about 54 minutes is summarised just before the one-hour cache ends, as long as the terminal session is open and the computer is awake. The next message then carries the summary instead of the whole history. It works in the terminal and only with the one-hour cache, which Claude Code turns on by default on a subscription. On a 200k window auto-compaction arrives earlier, so in practice it applies to models with a larger window. Idle compaction depends on the agent’s implementation: the T3 Code app runs Claude through the Claude Agent SDK, which gives it auto-compaction at the threshold, but it does not compact an idle conversation itself (as of 15 October 2026).

### How to compact without paying for the whole history

- The summarisation call is a separate request on the same prefix as the conversation: the same model, the same tools and system prompt, the same history, with the summarisation instruction appended at the end. To the cache it looks almost like the previous turn’s request, so the history is read from the cache. A request with a different prompt and no tools differs from the very first token and pays full price for the whole conversation. A cheaper model starts from an empty cache, because each model has its own, and whether it can read the earlier thinking depends on the pair of models.
- Codex builds its compaction call the same way (code as of October 2026). OpenAI lets you control caching for compaction too and reports cached tokens for it, but does not guarantee hits: it advises measuring them and comparing total input cost before and after compaction.
- After compaction the system prompt with the tools still hits the cache, in the Claude API provided a `cache_control` marker (breakpoint) sits at its end (see “Prompt caching”). Claude Code reloads CLAUDE.md from disk, and it hits the cache only if it hasn’t changed. The summary is written anew.
- Compact before the cache expires. After a longer absence the summarisation call pays full price for the whole history, which is why `/compact` after resuming an old session costs the most. It usually still pays off, because every later turn reads a short context instead of the whole history.
- Replace the old part of the history with one summary instead of cutting individual messages out of it. After every cut, everything that follows it has to be written to the cache again: after compaction that is a short summary, after removing results right after the start it is almost the whole history. So clear old tool results rarely and in large batches, or leave it to the API (see “Context engineering and memory”).

### What survives compaction

- Claude Code first clears old tool results and only then summarises. It keeps the summary and the latest messages, and reloads the project’s main CLAUDE.md, the plan and up to five files that were read or edited, most recently changed first. Details from early in the conversation can be lost, so keep rules that always apply in CLAUDE.md.
- In the Claude API the summary comes back as readable text that you send back unchanged in later requests. OpenAI’s Responses API returns a compacted context window with an encrypted compaction item, which according to OpenAI preserves the model’s understanding of the conversation, and possibly retained items of the history. You pass the whole returned window on unchanged. In both cases only evals on long sessions show what the summary lost.
- A model’s thinking comes back from the API encrypted, so only the provider can summarise it: on-demand compaction in the Claude API reads it, also with your own summarisation prompt. If you summarise yourself and send Claude models their thinking back, replace the whole history with one message or keep the latest turns without their thinking blocks. Models are trained for this: OpenAI trains Codex models to work across several context windows linked by compaction (GPT-5.1-Codex-Max, November 2025), and Anthropic writes that it trains Claude models on long tasks with a summary of the whole session in a single message.

### Check yourself

**Question:** An agent session is nearing the window limit. When and how would you compact the history so you don’t pay for it all over again?

**Short answer:** Compact rarely and in large chunks: at a threshold close to the window limit, or by hand once a task is done, and between compactions only append to the history. Send the summarisation call on the same prefix as the conversation: the same model, tools, system prompt and history, with the summarisation instruction at the end, so the history is read from the cache. Compact before the cache expires, because after a longer absence the summary pays full price for the whole history. Replace the old part of the history with one summary instead of cutting pieces out of it. Keep rules that always apply in the project file, because a summary loses details.

### Follow-up questions

- **You return to a session whose cache has expired. Compact straight away?** Usually yes, if you will keep working. The first request reads the whole history without the cache anyway, and after compaction every later turn reads tens of thousands of tokens instead of hundreds of thousands. Claude Code on the Pro and Max plans offers this itself when you resume a large session.
- **How does clearing old tool results differ from compaction?** Clearing keeps calls and messages verbatim and replaces old results with a short placeholder, so you pay nothing for a summary. But from the first cleared result onwards everything has to be written to the cache again, and the context often stays larger than after a summary. That is why clearing is done rarely and in large batches, or left to the API.
- **Server-side compaction or your own?** Server-side, when the provider offers it. In the Claude API on-demand compaction also reads the encrypted thinking, accepts your own summarisation prompt and can keep the latest turns with their thinking, and you send the returned block back unchanged. Your own works with any provider and model but cannot see encrypted thinking: with Claude models that you send thinking back to, it should replace the whole history with one message or keep the latest turns without their thinking blocks.

### Sources

- [Claude Code: prompt caching and compaction (docs)](https://code.claude.com/docs/en/prompt-caching)
- [Thariq Shihipar: Lessons from building Claude Code: Prompt caching is everything (April 2026)](https://claude.dev/blog/lessons-from-building-claude-code-prompt-caching-is-everything/)
- [Claude docs: Compaction on demand](https://platform.claude.com/docs/en/build-with-claude/compaction-on-demand)
- [OpenAI: Unrolling the Codex agent loop (January 2026)](https://openai.com/index/unrolling-the-codex-agent-loop/)

Interactive page: https://howaiworks.dev/context-compaction/
