In one sentence
Replacing the older part of an AI agent's conversation with a written summary when its context window nears full, so a long session can keep going.
Context compaction is how an AI agent keeps working after its conversation grows too long for the model's context window. Near the limit, the tool asks a model to summarize the session so far, starts a fresh window with that summary plus the most recent messages, and continues. To the user it looks like one long session. To the model it is a new start with notes.
Why it matters
Long agent runs, such as a coding task that takes hours or a support agent working a queue, produce far more text than any window holds. Anthropic's engineering team calls compaction the first lever for keeping long runs coherent. Claude Code, OpenCode and other agent tools compact automatically, and most also let you trigger it by hand.
What gets lost
A summary is shorter than what it replaces, so something is always dropped. Anthropic warns that overly aggressive compaction can lose subtle details whose importance only shows up later. In practice the losses we see most are exact identifiers such as commit SHAs and URLs, rules the user stated once early on, the list of files that were changed, and work still running in the background. After a bad compaction, an agent may redo finished work or break a rule it was given.
Ways to make it safer
Keep important state outside the chat in a plan file, task list or notes the agent rereads, which Anthropic calls structured note-taking. Clear old tool results, which are rarely needed again, before summarizing the conversation itself. Use a fixed summary format with required sections, and fill in facts the system can read directly, such as git status or tool history, from the source instead of from the model's memory. After a compaction, ask the agent to restate its goal and next step before it continues.
Our take
We built an open-source OpenCode Smart Compaction plugin around those ideas. The model writes a checked six-section checkpoint, and the plugin adds exact identifiers, touched files, git state and running subagents. It is listed on our projects page with the rest of our tools.
Keep following the system
Subagent
A separate AI agent session that a main agent starts for one task, with its own context window, instructions and tools, which returns a short report when done.
AI Agent
A system that uses a model and tools to pursue a goal across multiple steps within defined permissions and review boundaries.
Agentic Workflow
A workflow where an AI model decides some of the steps at run time, inside limits set by the people who built it.
Semantic Code Search
Searching a codebase by meaning rather than exact text, using embeddings, often combined with keyword search, so a question like "where do we validate refunds" finds the right functions.
API Key Rotation
Replacing or cycling the API keys an application uses, either on a schedule for security or across several keys for failover, without breaking the apps that depend on them.
Model Context Protocol (MCP)
An open standard for connecting compatible AI applications to tools, resources, and prompts.