Context compaction

Context compaction is the practice of summarising the older part of an AI agent's conversation or work history and replacing it with that summary, so that a long task can carry on within the model's context window.

Every model has a context window, a limit on how much text it can take in at once. An agent working on a long task fills it quickly with messages, tool outputs and file contents. When it is full, something has to go. Even before that point, quality suffers: in a September 2025 engineering post, Anthropic wrote that as the number of tokens in the context grows, a model's ability to recall information from it declines, an effect seen across all models.

Compaction deals with this by summarising. When the history nears the limit, the model is asked to condense it, and a fresh context starts from the summary. Anthropic describes how Claude Code does it: the model summarises the most important details, keeping architectural decisions, unresolved bugs and implementation details and dropping redundant tool outputs, then carries on with the summary plus the five files it accessed most recently.

Lighter techniques sit alongside it. Tool result clearing removes the raw output of tool calls made long ago, which rarely needs to be seen again. Structured note-taking has the agent write notes, such as a to-do list or a notes file, outside the context window and read them back when needed, so important facts survive any summary.

Model providers now build compaction into their APIs. As of December 2026, Anthropic's Claude API offers compaction in beta, in which Claude writes the summary on the server either when the developer asks or when the input reaches a set number of tokens. OpenAI's Responses API offers it through a compact_threshold setting or a separate compact endpoint; its compaction item is encrypted and not meant to be read by people.

Compaction loses information by design. Anthropic warns that compacting too aggressively can drop details whose importance becomes clear only later, and advises tuning a summary to capture everything relevant first and trimming afterwards. A detail left out of the summary is gone for the rest of the task unless it was saved somewhere else, which is why long-running agents pair compaction with notes and external memory.

Common questions

What is compaction in Claude Code?

When a session's history nears the context limit, Claude Code has the model summarise the conversation, keeping key decisions, open bugs and implementation details while dropping redundant tool output. Work then continues from that summary along with the most recently used files.

Why not just use a bigger context window?

Larger windows delay the problem but do not remove it, and models tend to recall information less accurately as context grows. Very long contexts also cost more per request. Compaction keeps the working context small and focused, at the price of losing some detail.

Does context compaction lose information?

Yes. A summary is shorter than what it replaces, so some detail is always dropped, and an over-aggressive summary can discard something that matters later. Agents reduce the risk by saving important facts to notes or files outside the context window.

What is tool result clearing?

It is a light form of context management that removes the full output of old tool calls, such as long search results or file listings, from the history while keeping the record that the call happened. It frees space without summarising the conversation itself.

Finding more

Search for "context compaction", "context engineering for agents", "tool result clearing" or the compaction feature of the API you use. Engineering posts and API documentation from model providers are the most reliable sources on how compaction works in their products, and they change often. Research on long-context performance explains why models struggle as context grows.

  • Effective context engineering for AI agentsArticleAnthropic's September 2025 post on compaction, tool result clearing, note-taking and context rot.
  • Compaction overviewOfficialHow server-side compaction works in the Claude API, on demand or at a token threshold.
  • CompactionOfficialOpenAI's guide to server-side compaction and the standalone compact endpoint in the Responses API.

Working out what this means for your business?

Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.