# What is compaction in AI agents, and how does it keep a long task going when the context fills up?

Replacing the older part of a long conversation with a summary, so an agent can keep working when its context fills up.

- Page: https://tecyfy.com/concepts/compaction
- Subject: AI agents (https://tecyfy.com/concepts)
- Level: intermediate
- Also called: Context Compaction, Conversation Summarisation, Context Compression

Every agent works inside a [context window](https://tecyfy.com/concepts/context-window): the instructions, the conversation and every tool call and result so far must fit in it. A long task, such as a coding session or a research job with dozens of searches, fills it. Compaction is what happens next. The older part of the conversation is replaced by a summary of it, and the agent carries on from the summary.

## Why not wait until it is full

Running out is not the only problem. Anthropic's engineers describe "context rot": the more tokens a model has in front of it, the worse it gets at recalling any one of them. An agent carrying 150,000 tokens of old tool output pays for all of them on every step, and is more likely to miss the one line that matters. Compacting earlier keeps it sharp, not just running.

## What a good summary keeps

The hard part is choosing. A summary that drops the reason a decision was made will see the agent make the opposite decision an hour later. Anthropic's description of Claude Code's compaction is a useful checklist: keep the decisions and why they were made, the problems still open and the details needed to continue; drop the raw tool output that has already been acted on; then carry on with the summary plus the few files touched most recently.

A lighter step comes before any summary: clear the results of old tool calls. A file read 40 steps ago and already used does not need to stay word for word, and removing it loses little.

## Doing it on the Claude API

You can write compaction yourself: count the tokens, ask a model to summarise the older messages, and replace them. The Claude API can also do it for you, in beta. Add a compaction edit to the request's `context_management`, and when the input reaches a threshold (150,000 tokens unless you set another, and no lower than 50,000) the API summarises the older context, returns the summary as a `compaction` block and continues the response. You append the response, compaction block included, to the conversation as usual. Custom instructions can say what the summary must keep.

## Other ways to stay inside the window

Compaction is one of 3 approaches Anthropic describes for long tasks. The others are notes the agent keeps outside its context, such as a progress file it reads back later ([Agent Memory](https://tecyfy.com/concepts/agent-memory)), and [subagents](https://tecyfy.com/concepts/subagent) that do the reading in their own contexts and return only short reports. Long-running agents often use all 3.

## Related

- [Context Engineering](https://tecyfy.com/concepts/context-engineering): Choosing what goes into a model's context at each step of a task, what to fetch only when needed, and what to clear, note down or summarise as the context grows.
- [Context Window](https://tecyfy.com/concepts/context-window): Everything a model can see at once while it writes an answer, counted in tokens. When the window is full, something has to be left out.
- [Agent Memory](https://tecyfy.com/concepts/agent-memory): The ways an agent keeps what it needs beyond a single context window, such as summaries, notes in files, and saved facts it reads back later. The model itself remembers nothing.
- [Subagent](https://tecyfy.com/concepts/subagent): An agent started by another agent for one part of a task. It works in its own fresh context and returns only its findings.
- [Agent](https://tecyfy.com/concepts/agent): A model that uses tools in a loop, deciding each next step from what the last one returned, until the task is done.

## Sources

- [Effective context engineering for AI agents](https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents)
- [Compaction](https://platform.claude.com/docs/en/build-with-claude/compaction-threshold)
