Editor's note: This is an AI-assisted English adaptation of a research note on Claude Code v2.1.220. The six paths below are the note’s taxonomy, not six supported commands or a guarantee about current internals. Where current public documentation differs, it takes precedence.
Suppose Claude has spent an hour debugging a migration. It has read the schema, chased two false leads, and found the failing assumption. Now the context window is nearly full. You want Claude to keep the diagnosis and next step, not every copy of the schema it read along the way.
That is what makes compaction more than a shorter transcript: the runtime has to decide what to summarize, what to reload from disk, and what to leave behind. Part 2 distinguished the stored transcript from the messages sent on the next request. Part 3 explained why replacing those messages changes the cache. Here we put the two together.
1. Six paths solve different problems
In a normal turn, the agent adds messages. The runtime can still deliberately rewrite the request when it needs room. If it retains client-side tool calls and results as structured blocks, it must keep them properly paired; strict alternation of every message role is not an additional API rule.
| Path in the research note | Problem it addresses | What to rely on |
|---|---|---|
Manual /compact
|
The user wants room for the next phase of the same task | A documented summary command that accepts optional focus instructions |
| Automatic compaction | The conversation is approaching its context limit | Documented automatic summarization; the trigger depends on the model and configuration |
| Micro-compaction | An older tool result is redundant even though the window is not yet full | A historical low-cost pruning path; not a public guarantee of which results are removed |
| Reactive compaction | A request has already exceeded the available input budget | A recovery strategy described by the note, not a guaranteed retry sequence |
| Session-memory compaction | Useful context should survive beyond this conversation | A historical path in the note; persistent auto memory and same-session compaction are separate concepts |
| Context collapse | A normal recovery pass may not free enough context | An experimental path described by the note, not a documented feature to count on |
The distinction is the trigger. Redundant evidence, a nearly full window, a rejected request, and a new session aren’t the same problem. Applying the same kind of summary to all four would either waste work or discard information too early.
2. Manual compaction: tell the summary what matters
The command reference documents an optional focus for /compact. For the migration example:
/compact Keep the schema assumptions, the failing migration, and the next verification step. Omit the CSS discussion.
The note described a separate summarization request using the session’s main model, and a nine-section output covering user intent, technical concepts, files, errors and fixes, work tried, user messages, pending tasks, current work, and a possible next step. It described an internal draft section that was discarded before the summary was carried forward. The model selection and output format are implementation observations, not a stable interface.
The design lesson is worth keeping: organize a summary around the decisions it must support. A chronological miniature of a long investigation is less useful than a record of which hypotheses failed and what to try next. Nor should you assume the command preserves every user message verbatim. The Claude Code overview warns that early details can be lost. Keep durable requirements in project instructions and give the summary a focus when a particular detail matters.
3. Automatic compaction: reserve room for the answer
The historical note modeled the trigger as the effective context window minus output reserve and a safety buffer. Its illustrative values were 20,000 and 3,000 tokens: with a 200,000-token window, the result is 177,000, or 88.5%. With a different window, the same reserves would produce a different percentage.
That arithmetic explains why “always at 80%” is a poor mental model. It does not establish a current default. The context-window guide says the trigger varies by model and configuration. On versions that support /autocompact, you can inspect or adjust the window there. Use /context if you want to see what is consuming the space before you compact.
4. Pruning, recovery, and carrying information forward
Pruning can avoid a model call. If a conversation contains two overlapping reads of the same file, an agent may be able to replace the redundant result with a short note pointing to the retained evidence. The historical research described that style of micro-compaction. The important part is to preserve the result’s tool-call ID when keeping the structured record, and to point to evidence the model can still actually see. A plain-language pointer is more useful than an unexplained ellipsis.
Recovery is different. The research note reported a bounded reactive retry after an oversized request, as well as an experimental collapse path that discarded history more aggressively. Don’t depend on their precise order, retry count, or availability today. The public docs confirm that Claude Code stops rather than compacting indefinitely when context keeps refilling. See the Agent Loop articles on stop reasons, the loop’s state transitions, and error recovery for the broader control flow.
Cross-session memory addresses a different lifetime. A summary reduces the messages needed to continue this conversation. Auto memory can carry useful project facts into a later session. The note used sessionMemoryCompact for one internal path, but that name does not imply every session is summarized into MEMORY.md when it ends. The memory files can be inspected and edited.
5. What actually comes back after compaction?
A summary saying “we read the schema” is not the schema. The runtime can restore some source material, but restoring everything would undo the compaction. The current context-window guide documents the trade-off:
- Files: Claude Code re-reads up to five files it read or edited, prioritizing the most recently modified. A file over 5,000 tokens comes back as a path reference rather than full content. Re-read the relevant lines if you need exact text.
-
Project instructions: Project-root
CLAUDE.mdand unscoped rules are loaded again. NestedCLAUDE.mdfiles and path-scoped rules return when work touches a matching file, instead of taking space immediately. - Skills: Invoked skill bodies have a 5,000-token per-skill cap and a 25,000-token total cap. The historical note described not re-announcing the skill catalog after compaction. The current guide says skill descriptions are rebuilt and re-injected from v2.1.304 onward; that is distinct from restoring an invoked skill body.
-
Other context: Auto memory and a plan saved in plan mode can be reloaded from disk. Hook output from earlier turns is summarized with the conversation; applicable
SessionStarthooks can contribute new output on compaction.
The original note also listed an internal aggregate file budget. It’s not needed to decide what you can rely on: the documented file-count and per-file limits tell you why a file may be missing or reduced to a path.
Compaction has a separate cache cost. As Claude Code’s caching guide explains, the summarization request can reuse a warm prefix, while the next normal turn rebuilds the conversation layer against the new, shorter history. Those are different requests. A warm cache makes the summary cheaper to prepare; it doesn’t make old and compacted history identical.
6. Compact, clear, rewind, or fork?
| If you want to… | Use |
|---|---|
| Keep working on the same task with less history |
/compact, optionally with a focus |
| Start an unrelated task with empty conversation context |
/clear; the previous conversation can still be resumed |
| Go back to an earlier prompt, or summarize a selected portion | /rewind |
| Try a different direction without discarding the current conversation |
/branch or /fork, depending on whether you want to switch or run a copy separately |
The checkpointing guide separates restoring the conversation, restoring tracked file changes, and summarizing part of the conversation. The picker works from user prompts, not from arbitrary positions inside a tool exchange, and code restore isn’t a substitute for version control. The old note also found a feature-gated fork path; the current command reference now documents /fork and distinguishes it from /branch and /subtask. Forking copies context rather than condensing it.
The practical rule: summarize what helps the next decision, reload what must be exact, and leave the rest out. Next we’ll look at how the CLAUDE.md family decides which instructions to load in the first place.
Top comments (0)