---
title: "What is context compaction for AI agents?"
description: "Context compaction summarizes an agent's history as it nears the context limit and continues from the summary. How it works, vs truncation, and vs agent memory."
type: "glossary-term"
canonical_url: "https://theaiengineering.com/glossary/context-compaction"
markdown_url: "https://theaiengineering.com/glossary/context-compaction.md"
topic: "Prompt and context engineering"
author: "The AI Engineering Editors"
publisher: "The AI Engineering"
updated: "2026-11-06"
keywords: ["Context compaction","compaction","conversation summarization"]
citation: "The AI Engineering Editors. \"What is context compaction for AI agents?\" The AI Engineering, updated Nov 06, 2026. https://theaiengineering.com/glossary/context-compaction"
---

# What is context compaction for AI agents?

Also called compaction, conversation summarization.

> **Definition of Context compaction:** Context compaction is a context management technique that replaces older conversation turns with a model-written summary as the context window nears its limit, so an agent can keep working.

Context compaction is the practice of summarizing a conversation as it approaches the [context window](https://theaiengineering.com/glossary/context-window) limit and continuing in a fresh window that starts from the summary. Anthropic's post [Effective context engineering for AI agents](https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents) (September 29, 2025) lists compaction among the main techniques for long-horizon agents. An [AI agent](https://theaiengineering.com/glossary/ai-agent) that runs for hours adds tool calls and results on every step, so without compaction it either hits the window limit or carries so much stale history that [context rot](https://theaiengineering.com/glossary/context-rot) degrades its work.

## How context compaction works

Compaction works by sending the history to a model with instructions to summarize it, then replacing the summarized turns with that summary. A good summary keeps decisions, open problems, and the current plan, and drops redundant tool output. Compaction can run automatically at a token threshold, on demand when the application requests it, or in the background.

Anthropic's post describes how Claude Code does it: the message history goes to the model, which preserves architectural decisions, unresolved bugs, and implementation details while discarding redundant tool outputs, and the agent continues with that summary plus the five most recently accessed files. The post advises tuning the summarization prompt for recall first, then for precision. Claude Code's [cost documentation](https://code.claude.com/docs/en/costs) shows the user-facing side: auto-compaction near the limit, and a `/compact` command that accepts instructions such as which details to preserve.

Model providers now run compaction on the server:

- **Anthropic:** the Messages API's [compaction feature](https://platform.claude.com/docs/en/build-with-claude/compaction), in beta, replaces older turns with a summary Claude writes on the server, either on demand or when input tokens reach a trigger the developer sets.
- **OpenAI:** the Responses API [compacts server-side](https://developers.openai.com/api/docs/guides/compaction) once the token count crosses a `compact_threshold`, or through a standalone `/responses/compact` endpoint. Its compaction item is encrypted and not meant to be human-readable.

## Compaction vs truncation

Truncation drops the oldest messages outright, which is cheap and predictable but loses whatever they contained. Compaction replaces those messages with a summary, which costs a model call and can omit details, but carries decisions and state forward. Tool result clearing sits between the two: it removes old raw tool outputs while keeping the record of each call.

| | Truncation | Tool result clearing | Compaction |
|---|---|---|---|
| What it removes | The oldest turns | Old raw tool outputs | Older turns, replaced by a summary |
| Extra model call | No | No | Yes |
| What survives | Recent turns only | Every turn, minus bulky outputs | A summary plus recent turns |
| Main risk | Losing early instructions or facts | Needing a cleared output again | The summary dropping a needed detail |

Anthropic's post calls tool result clearing one of the safest, lightest-touch forms of compaction. Compaction and tool result clearing both rewrite the history, so the next request misses the [prompt cache](https://theaiengineering.com/glossary/prompt-caching) for the rewritten part; Claude Code reports that miss after compaction or tool-result clearing as an expected rebuild.

## Compaction vs agent memory

Compaction keeps a running session inside the window. [Agent memory](https://theaiengineering.com/glossary/agent-memory) stores information outside the window, in files or databases, so it survives across sessions and can be reloaded on demand. They work together when an agent writes durable notes before compaction and reloads them afterward, so the plan survives even if the summary loses a detail.

Anthropic's post calls this structured note-taking, with examples such as a to-do list or a `NOTES.md` file that the agent maintains. Compaction is lossy, so keep it restorable: when a summary drops a document's content, keep its file path or URL so the agent can fetch it again. Track how often tasks fail after a compaction to tell whether the summary keeps what later steps need.

The [context engineering guide](https://theaiengineering.com/articles/context-engineering) shows compaction alongside retrieval, note-taking, and sub-agents in one agent run.

## Related terms

- [Context engineering](https://theaiengineering.com/glossary/context-engineering): Context engineering is the practice of selecting, structuring, and pruning all the information placed in an LLM's context window at each step, including instructions, retrieved data, tool results, and memory.
- [Context window](https://theaiengineering.com/glossary/context-window): A context window is the maximum number of tokens a language model can take into account in a single request, including the system prompt, conversation history, tool results, and the generated output.
- [Context rot](https://theaiengineering.com/glossary/context-rot): Context rot is the decline in a language model's accuracy and recall as the number of tokens in its input grows, even when the information a task needs is present in the context.
- [Agent memory](https://theaiengineering.com/glossary/agent-memory): Agent memory is the set of mechanisms that let an AI agent keep and retrieve information beyond a single model call, from the current context window to external stores that persist across sessions.
- [Prompt caching](https://theaiengineering.com/glossary/prompt-caching): Prompt caching is an LLM API and inference-server feature that stores the computed attention state for a repeated prompt prefix, so later requests sharing that prefix skip reprocessing it.
- [AI agent](https://theaiengineering.com/glossary/ai-agent): An AI agent is a software system in which a large language model runs in a loop, calling tools, reading their results, and repeating until the task is complete or a stop condition is reached.

## Guides that cover context compaction

- [What is context engineering? A practical guide for AI agents](https://theaiengineering.com/articles/context-engineering): Context engineering is the practice of choosing what fills an LLM context window. Learn its components, why context rot happens, and how to measure results.
- [What are AI agents? How agents work and when to use one](https://theaiengineering.com/articles/what-are-ai-agents): AI agents are LLM systems that call tools in a loop until a task is done. Learn how the loop works, agents vs agentic AI, examples, failure modes, and limits.
- [How to build an AI agent: a step-by-step guide in Python](https://theaiengineering.com/articles/how-to-build-an-ai-agent): Learn how to build an AI agent from scratch in Python: when you need one, tool schemas, the agent loop, stop conditions, guardrails, evals, and tracing.

Part of the [Prompt and context engineering](https://theaiengineering.com/topics/prompt-engineering) topic hub.

## Cite this definition

The AI Engineering Editors. "What is context compaction for AI agents?" The AI Engineering, updated Nov 06, 2026. https://theaiengineering.com/glossary/context-compaction

- Title: What is context compaction for AI agents?
- Author: The AI Engineering Editors
- Publisher: The AI Engineering
- Updated: 2026-11-06
- URL: https://theaiengineering.com/glossary/context-compaction
- Markdown: https://theaiengineering.com/glossary/context-compaction.md
