Lesson 60.2 · 2 of 4 · 4 min read · Reviewed 24 Nov 2026
Compaction
By the end, you can
- Explain compaction in agent runs.
- Steer what compaction keeps.
CompactionCompaction: Summarising older conversation history so a long session fits in the context window while keeping what matters. is summarisation for agentsAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done.: when the context nears its limit, the run’s history is condensed into a summary, and the agent carries on from there.
In Claude Code
- Automatic when the context is nearly full.
- Manual with
/compact, plus instructions:/compact keep the plan, the list of changed files and the failing tests.
In your own agents
You can do the same: when input tokensInput tokens: The tokens you send in a request: system prompt, tool definitions, conversation history and attached content. pass a threshold, ask the model to write a structured summary of the run, then start the next request with that summary instead of the full history. Pair it with clearing old tool results (58.3) and a notes file (58.5).
Good compaction prompts
Summarise this session so another engineer could continue it. Include:
- the goal and the agreed plan
- files changed, with one line each on what changed
- the current failing tests and the leading hypothesis
- decisions made and anything we promised not to change
Signs compaction went wrong
The agent redoes finished work, forgets a constraint, or asks a question it already had answered. Fix it with better compaction instructions and a notes file.
Key points
- Compaction replaces the conversation so far with a summary so the agent can keep working.
- Claude Code compacts automatically near the limit, and on demand with /compact.
- Give compaction instructions: what must survive, such as the plan, failing tests or file paths.
Compact a real coding session with instructions
Use a coding agent on your own machine through a long session, compact it with explicit instructions, and check what survived.
You will have: A before and after note: the compaction instruction you used, what the agent remembered, and anything it redid.
Steps
- Open a coding agent such as Claude Code in a small project and work on a multi-step change until the session is long.
- Before compacting, write down the plan, the files changed and the failing tests yourself.
- Compact with instructions, for example: /compact keep the plan, the list of changed files with one line each, the failing tests and any decisions.
- Ask the agent what the plan is, which files it changed and what is still failing, and compare with your notes.
- Repeat on another session with a bare /compact and note the difference.
Done when
- After the instructed compaction, the agent lists every changed file correctly.
- It does not redo finished work in the next three steps.
- You have a written comparison of instructed and bare compaction.
Docs: Slash commands (Claude Code docs) · Claude Code overview (Claude Code docs)