Repository navigation
Compaction And Context
This page describes what fills the context of Claude Code, when it compacts the context, and what a compaction keeps.
Read it if you tune the context settings of dotclaude.
It covers Claude Code 2.1.296.
Each fact from the binary comes from a search of its strings, or from the official docs.
The page names the source of each fact.
Each inference is marked as inference.
- The default window is 200,000 tokens.
Source: the default window constant in the
2.1.296strings. - A model ID with the
[1m]suffix selects a window of 1,000,000 tokens. Source: the2.1.296strings match the suffix, and they offersonnet[1m]for a 1M window. -
CLAUDE_CODE_DISABLE_1M_CONTEXTturns off the 1M choice. Source: the2.1.296strings. -
claude-sonnet-5-5had a window of 1,000,000 tokens in the measurement of 2026-10-12. Source: Handoff Cost. - The comment on
CONTEXT_WINDOWinsrc/kernel/budget.tssays that Claude Code2.1.296warns against a 200K window forclaude-opus-5-5. It warns unlessCLAUDE_CODE_AUTO_COMPACT_WINDOWis set. I did not check this comment in the2.1.296strings. The strings have a warning for an unknown model id. That warning namesCLAUDE_CODE_MAX_CONTEXT_TOKENSandCLAUDE_CODE_DISABLE_UNKNOWN_MODEL_WINDOW_ENFORCEMENT. - The sizes of the other models are not settled, so this page gives no others.
The official docs list these items, in the order that a session loads them.
- The system prompt, which the user does not see.
- The
CLAUDE.mdfiles of the project and the user. - Auto memory, which is the first 200 lines or 25 KB of
MEMORY.md. - Environment information, such as the working directory and the platform.
- The names of MCP tools. By default, the schemas stay deferred until a tool search loads them.
- The one-line descriptions of the skills. The body of a skill loads only when Claude uses it.
- The messages of the conversation, the tool calls, the tool output, and the file reads.
The docs give a token count for some items as an illustration. This page does not repeat those counts, because they are not measured.
The threshold is the effective window minus 13,000 tokens.
The effective window is the window minus the smaller of the output limit and 20,000 tokens.
Source: the threshold function and the 20,000 constant in the 2.1.296 strings.
The threshold is an inference from these two formulas, and I did not measure it.
- With a 200,000-token window and an output limit of at least 20,000 tokens, the threshold is 167,000 tokens (inference).
- With the 300,000-token window of the dotclaude snippet, the threshold is 267,000 tokens (inference).
-
CLAUDE_AUTOCOMPACT_PCT_OVERRIDEsets a percentage of the effective window from 0 to 100. The threshold is then the lower of that value and the default threshold. - The
2.1.296strings also have a second threshold, from a field namedprecomputeBufferFraction. It is the window minus a fraction of the window. The trigger takes the lower of that value and the threshold above, so it can fire earlier. The fraction comes from a model table. This page does not examine that second threshold. - The next request is blocked at the effective window minus 3,000 tokens.
With the default output limit, that is the window minus 23,000 tokens (inference).
CONTEXT_BLOCK_MARGINinsrc/kernel/budget.tshas the same value, 23,000. Its source is 20,000 plus 3,000.
| Name | Kind | Effect | Source |
|---|---|---|---|
autoCompactEnabled |
settings key, boolean, default true
|
Turns auto-compaction on or off. | Settings schema, dossier/cc-2.1.296/settings.md
|
autoCompactWindow |
settings key, 100000 to 1000000 or "auto", per model |
Sets the window for auto-compaction of one model. | Settings schema description |
CLAUDE_CODE_AUTO_COMPACT_WINDOW |
env var | Sets the window for auto-compaction, and takes precedence over autoCompactWindow. |
2.1.296 strings, dossier/cc-2.1.296/env.md
|
CLAUDE_AUTOCOMPACT_PCT_OVERRIDE |
env var | Sets the threshold as a percentage of the effective window. |
2.1.296 strings |
DISABLE_AUTO_COMPACT |
env var | Turns off auto-compaction. |
2.1.296 strings |
DISABLE_COMPACT |
env var | Turns off auto-compaction and /compact. |
2.1.296 strings, dossier/cc-2.1.296/env.md
|
CLAUDE_CODE_MAX_CONTEXT_TOKENS |
env var | Sets the window of the model, with DISABLE_COMPACT or as a fallback for an unrecognized model id. |
2.1.296 strings, dossier/cc-2.1.296/env.md
|
The 2.1.296 strings read CLAUDE_CODE_MAX_CONTEXT_TOKENS in two places.
A search of external/cc-2.1.296/strings.txt for the name and its nearby code found both.
One read sets the window only when DISABLE_COMPACT is set.
The other read is a fallback for a model id.
The name check does not take that id as a claude- id.
The fallback runs after the model table and the 1M checks.
It does not need DISABLE_COMPACT.
So the comment in src/kernel/budget.ts is incomplete for 2.1.296.
That comment says that Claude Code honors the variable only with DISABLE_COMPACT.
A compaction asks the model for a summary of the conversation.
The summary replaces the earlier messages.
The summary prompt in the 2.1.296 strings asks for these sections.
- The primary request and intent.
- The key technical concepts.
- The files and code sections.
- The errors and fixes.
- The problem solving.
- All user messages that are not tool results.
- The pending tasks.
- The current work.
- The next step.
The binary has two variants of this prompt.
- A partial summary covers only the recent messages. The earlier messages stay intact.
- A shorter prompt asks for a summary of 2,000 words or fewer. The strings do not say when Claude Code uses it.
The official docs list what the context holds after a compaction.
- The system prompt,
CLAUDE.md, auto memory, and the names of the MCP tools reload. - Claude Code re-reads up to five of the files that were modified most recently.
- Claude Code re-injects the body of each invoked skill, up to 5,000 tokens for each skill.
- The skill descriptions do not reload.
The messages, the tool output, and the file contents that are not re-read leave the context (inference from the docs).
-
/compactstarts a compaction now. The strings describe the command as a way to free context by summarizing the conversation so far.DISABLE_COMPACTremoves the command. -
/clearends the conversation and starts a new one with no earlier messages. The strings suggest/clearfor a fresh start when a file is too large for the window. -
/compactkeeps a summary of the conversation./clearkeeps nothing of it (inference from the two commands).
- The settings snippet sets
DISABLE_COMPACTto1. It setsautoCompactEnabledtofalse. It setsCLAUDE_CODE_AUTO_COMPACT_WINDOWandCLAUDE_CODE_MAX_CONTEXT_TOKENSto300000. Source:src/plugins/dotclaude/slices/prompt/slice.ts, and the settings snippet. - With compaction off, a session that reaches its limit stops.
It does not compact, so it loses its state.
Source: the comment on
CONTEXT_WINDOWinsrc/kernel/budget.ts. - The prompt text of the
context_managementsection says that the conversation is summarized. The prompt slice replaces that text with a note that compaction is off. It does so only whenDISABLE_COMPACTis set. Source:src/plugins/dotclaude/slices/prompt/transforms/context_management.tsandsrc/plugins/dotclaude/slices/prompt/adapters/mod.ts. - The handoff is the alternative to a compaction.
The
handoffslice writes the state of the work to a file. The user runs/clear, and the next session reads the file. See Context and Handoff. - One measured run on 2026-10-12 compared the two. A handoff cost $0.0630. A compaction with its follow-up turn cost $0.2288. Both costs are at the API list price. The run used one session of about 81,000 tokens, so it has no spread. See Handoff Cost.
- The bounds of the context of a subagent are in Context-Bound.
- The
2.1.296strings, searched withgrepfor the names and the constants above. - The extracts of the compaction prompts in
external/cc-extract/2.1.296/compaction/. - The dossier of the binary,
external/dossier/cc-2.1.296/env.mdandsettings.md. - The official docs page "Explore the context window" at
code.claude.com/docs/en/context-window, read on 2026-10-13. - The kernel constants in
src/kernel/budget.ts, and the snippet insrc/plugins/dotclaude/slices/prompt/slice.ts.
- The window of each model, except the three cases above.
- The threshold for a model whose output limit is below 20,000 tokens.
- The exact content that a compaction keeps, which only the docs describe.
- Start
- How dotclaude Works
- Add-On Plugins
- How Claude Code Works
- Reference
- Rationale
- Evidence
- Development
- Archive