Skip to content
Search
AgenticGlossary

Context Compaction

Anthropic's guide to context engineering for AI agents defines compaction as taking a conversation that is nearing the context window limit, summarizing its contents, and starting a new context window with the summary. It describes it as typically the first lever in context engineering for long-running agents. The lightest form is clearing old tool results: once the agent has used a file listing or a search result, it rarely needs the raw text again.

Compaction fights both the hard limit of the context window and the softer problem of context rot. The risk is that a summary drops a detail that turns out to matter, so decide what must survive: the goal, decisions and their reasons, open questions, file paths and anything a person said. Write important facts to files or agent memory before compacting so they can be read back exactly. Handing side work to a subagent avoids the bloat in the first place. Qurtoo's article on keeping tool outputs out of context shows the same idea from the tool side.

Example

You're about to run out of context. Write a handoff summary for yourself that replaces the conversation so far. Include:
- The goal, word for word as the user stated it.
- Decisions made, each with its reason.
- Files changed, with paths.
- What's left to do, in order.
- Anything the user asked you not to do.
Leave out raw tool output and dead ends, but name any approach that failed so you don't try it again.

When to use it

  • Long agent sessions that approach the context limit
  • Conversations full of tool output that's already been used
  • Agents that start repeating work or forgetting earlier decisions

When not to use it

  • Short tasks that fit comfortably in context
  • Compacting before you've saved exact details, such as figures or quotes, that a summary might blur