What is compaction in AI agents?
Compaction is a context-management technique in which an AI agent's older conversation turns and tool results are replaced with a model-written summary, so a long task can continue inside the context window.
Checked
An agent's context grows with every tool call and every result it reads. Reliability drops as input grows, a pattern Chroma's Context Rot study found across 18 models, so without intervention a long run either hits the window limit or degrades before it gets there.
Compaction is the most common intervention. At a set point, the older part of the conversation is summarised, the summary replaces it, and the agent carries on from a shorter context.
How the labs ship it
Anthropic
The Claude API offers server-side compaction in beta, either on demand or when input reaches a token threshold. Recent turns can be kept word for word, and a custom summarisation prompt can replace the default.
OpenAI
The Responses API runs server-side compaction in-stream when context crosses a threshold, and a standalone compact endpoint gives explicit control. OpenAI advises treating compaction as a default for long runs rather than an emergency fallback.
Google
Google's context-engineering guidance for ADK recommends compacting asynchronously and keeping a stable prompt prefix so caching still works.
Compaction and its alternatives
Context editing
Clearing stale tool results by rule instead of summarising. Cheaper and more predictable, but it only removes, it does not condense.
Context reset
Starting a fresh context with a structured handoff file. Anthropic found resets worked better than compaction for long application builds, because they give a clean slate.
Notes outside the window
Progress and plan files the agent writes and re-reads, so the critical facts never depend on a summary.
What can go wrong
A summary can drop the one detail a later step needs, such as a file path, an ID or a constraint the user stated early. Compaction also rewrites the start of the context, so the prompt cache stops matching until a new prefix is established.
How Motiontic's engineers apply it
Motiontic's engineers keep durable state, such as the plan, decisions and IDs, in files or a session log outside the context window, so compaction only has to carry the narrative. Long-run evals check that the agent still meets its constraints after each compaction point.
Compaction, context editing and context reset compared
| Compaction | Context editing | Context reset | |
|---|---|---|---|
| What happens | Old turns become a summary | Stale tool results are removed | A new context starts from a handoff file |
| Keeps | A condensed narrative | Everything except cleared items | Only what the handoff file records |
| Risk | Lost detail in the summary | Removing something still needed | Handoff file missing context |
| Fits | Long conversations and agent runs | Tool-heavy runs with bulky outputs | Multi-session builds |
Common questions
What is context compaction?
Context compaction replaces the older part of an agent's conversation with a summary written by the model, so the agent can keep working without hitting the context window limit.
Is compaction the same as context editing?
No. Context editing removes items, such as old tool results, by rule. Compaction condenses the history into a summary. Many harnesses use both.
Does compaction affect prompt caching?
Yes. Compaction changes the start of the context, so later calls no longer match the cached prefix until a new one is built. Google's guidance recommends keeping a stable prefix where possible.
When is a context reset better than compaction?
For long, multi-session builds. Anthropic found that starting a fresh context with a structured handoff file kept agents more coherent than compacting the same window repeatedly.
Related
Context rot
Why agents get worse the longer they run, and what keeps context clean.
Context editing
Clear the old tool results, keep the conversation, keep the agent running.
Prompt caching
Paying less for the part of the prompt that does not change between calls.
Long-running agent
Agents that work for hours or days, and the state that lets them pick up where they stopped.
AI agent development
Agents that do real work for users, with the evals, traces and guardrails to keep them in production.
Sources
- Claude Platform docs, Compaction overview
- OpenAI, Shell + Skills + Compaction: tips for long-running agents
- Google, Architecting efficient context-aware multi-agent framework for production (2025-12-04)
- Anthropic, Harness design for long-running application development (2026-03-24)
- Claude blog, Managing context on the Claude Developer Platform (2025-09-29)
- Chroma, Context Rot (2025-07-14)
Running agents that have to hold up?
Describe the agent, the systems it touches and what it must never get wrong. The first call is with a lab engineer, who will say plainly whether it is ready for production, and whether Motiontic is the right team.