Context and Compaction
The context window is the amount of text the model can read at one time. When a session gets close to the limit, Claude Code compacts it. It asks the model for a summary. Then it continues from the summary. You can set when this happens. You can also say what the summary must keep.
What Fills the Context
Sources: How Claude Code Works: The Context Window and Explore the Context Window.
| Content | Status | Note |
|---|---|---|
| System prompt and tool names | Documented: An official docs page names this item. | MCP tool schemas stay deferred until a tool search loads them. |
CLAUDE., rules, and auto memory | Documented: An official docs page names this item. | The first 200 lines or 25 KB of MEMORY. load. |
| Skill descriptions | Documented: An official docs page names this item. | The body of a skill loads only when the skill runs. |
| Messages, tool results, and file reads | Documented: An official docs page names this item. | This is the part that grows. |
| Attachments that Claude Code adds | Undocumented: Only the program shows this item. It can change without notice. | A transcript of 2.1.305 from a stub run shows types such as total_, output_, nested_, and skill_. Transcripts of 2.1.305 from a real machine also showed queued_, edited_, deferred_, mcp_, credential_, and silent_. |
The mod event prompt.attachment can rewrite or drop one attachment.
For measured token counts of an empty session and ways to shrink it, see Context Budget.
When Compaction Runs
Let window be the window of the model, or the window you set.
Let out be the smaller of the model output limit and 20000.
| Level | Status | Formula | Example for a 1,000,000 window |
|---|---|---|---|
| Effective window | Undocumented: Only the program shows this item. It can change without notice. | window - | 980,000 |
| Compact threshold | Documented: An official docs page names this item. | effective - | 967,000, which the docs call "about 967K" |
| Warning | Undocumented: Only the program shows this item. It can change without notice. | threshold - | 947,000 |
| Blocked | Undocumented: Only the program shows this item. It can change without notice. | window - | 977,000 |
| Percent override | Documented: An official docs page names this item. | The smaller of effective * pct / and effective - | CLAUDE_ sets pct from 1 to 100 |
At the blocked level, Claude Code stops the turn with a "prompt is too long" error, unless it can recover by compacting.
CLAUDE_CODE_BLOCKING_LIMIT_OVERRIDE (
Claude Code finds the window in this order:
CLAUDE_CODE_AUTO_COMPACT_WINDOW- The setting
autoCompactWindow(for one model inmodelSettings) - Client data from the server
- An experiment
- The default of the model
The accepted window is from 100K to 1M tokens.
Since 2.1.312, the autoCompactWindow of a subagent also lowers a window that CLAUDE_CODE_AUTO_COMPACT_WINDOW sets. Before, the variable ended the search and the subagent value had no effect.
The subagent value is a ceiling. If the window found is larger than the ceiling, Claude Code uses the ceiling (limited by the model window). It never raises the window.
Source: the program (chunk-r5cyd28m.js line 83915 and chunk-1wgxyh0t.js line 6088).
Sources: Model Configuration: Context Window and Auto-Compaction and the program.
Controls
Sources: Model Configuration: Set the Auto-Compact Window and Environment Variables.
| Item | Status | Effect |
|---|---|---|
/ | Documented: An official docs page names this item. | Compacts now. DISABLE_ removes it. |
/ | Documented: An official docs page names this item. | Saves the window for the current model. |
-- | Documented: An official docs page names this item. | Sets the window for one launch. |
auto, auto | Documented: An official docs page names this item. | Settings. auto also works in the frontmatter of a subagent. |
CLAUDE_ | Documented: An official docs page names this item. | Sets the window. It wins over the command, the flag, and the setting. Accepts k and M suffixes since 2.1.304. |
CLAUDE_ | Documented: An official docs page names this item. | Sets the percent of the effective window. |
DISABLE_, DISABLE_ | Documented: An official docs page names this item. | Turn off automatic compaction, or all compaction. |
CLAUDE_ | Documented: An official docs page names this item. | Sets the window for a model ID that Claude Code does not know. |
CLAUDE_ | Documented: An official docs page names this item. | Compacts only after the API says the prompt is too long. |
CLAUDE_ | Documented: An official docs page names this item. | Turns off the 1M choice. |
idle, CLAUDE_ | Documented: An official docs page names this item. | Compact a conversation that is idle. |
/, then Summarize from here or up to here | Documented: An official docs page names this item. | Compacts one part of the conversation. |
/ | Documented: An official docs page names this item. | Starts with an empty context. |
Pre, Post hooks | Documented: An official docs page names this item. | Run around a compaction. The trigger field is auto or manual. |
Kinds of Compaction
| Kind | Status | How it works |
|---|---|---|
| Automatic | Documented: An official docs page names this item. | It starts at the compact threshold. The hooks Pre and Post get trigger:. |
| Manual | Documented: An official docs page names this item. | It starts with /. The trigger is manual. |
| Reactive | Undocumented: Only the program shows this item. It can change without notice. | The API refuses a request as too long, and Claude Code compacts and tries again. Since 2., this also covers a standard-window session that goes over 200K tokens and gets "Usage credits required for 1M context" (chunk-). It compacts and continues. |
| Precomputed | Undocumented: Only the program shows this item. It can change without notice. | A summary made ahead of time replaces the conversation. |
| Larger-model retry | Undocumented: Only the program shows this item. It can change without notice. | Claude Code can retry a failed compaction on a model with a larger window. |
| Server side | Undocumented: Only the program shows this item. It can change without notice. | The beta context- lets the API edit the context. |
Since 2.1.311, when the model's reply to a compaction request has no <summary> block, Claude Code usually asks once more for the summary before it replaces the conversation (chunk-2ht8z368.js:108786).
The second request adds a short note that says the reply must hold the summary in <summary> tags (chunk-2ht8z368.js:108553).
If the second reply has none either, the compaction fails with no <summary> block in the second reply either (chunk-2ht8z368.js:108779).
A gate in the program decides whether the second ask runs, so "usually" is the right word (chunk-2ht8z368.js:108784).
Source: the Claude Code changelog and the program.
Auto compaction stops with an error when the context refills within 3 turns of the last compaction, 3 times in a row.
The message starts Autocompact is thrashing.
Sources: How Claude Code Works: When Context Fills Up and Troubleshooting.
The Summary Prompt
The compaction request asks the model for a summary in <summary> tags with nine parts.
Source: the program (
- Primary Request and Intent
- Key Technical Concepts
- Files and Code Sections
- Errors and Fixes
- Problem Solving
- All User Messages
- Pending Tasks
- Current Work
- Optional Next Step
The prompt has three forms. One is short ("in 2000 words or fewer"). One asks for an <analysis> first. One covers only the recent messages.
The request uses the thinking setting of the session (v2.1.198 or later).
See the captured compaction prompt.
What Survives
Source: Explore the Context Window: What Survives Compaction.
| Content | Status | After compaction |
|---|---|---|
| System prompt and output style | Documented: An official docs page names this item. | They still apply. |
Root CLAUDE., unscoped rules, and auto memory | Documented: An official docs page names this item. | Claude Code reads them again from disk. |
| The plan from plan mode | Documented: An official docs page names this item. | Claude Code reads it again from disk. |
Rules with paths: and nested CLAUDE. files | Documented: An official docs page names this item. | They load again when needed. |
| Files that the session read or edited | Documented: An official docs page names this item. | Claude Code reads up to five again, newest first. A file over 5,000 tokens comes back as a path. |
| Skills that ran | Documented: An official docs page names this item. | Each keeps 5,000 tokens, up to 25,000 in total. The oldest go first. |
| Background commands and subagents | Documented: An official docs page names this item. | They keep running. |
Session hooks with the compact matcher | Documented: An official docs page names this item. | They run again. |
Example
Your session is long. You start a new task.
Run /compact focus on the auth bug fix to keep what matters.
Or compact earlier next time by setting a smaller window.
claude --autocompact 500000
{
"$schema": "https://clawdipedia.cc/schemas/claude-code-settings.json",
"autoCompactWindow": 500000
}
Errors
Each row is a family of messages. The backticked text is the start that every message in the family shares. A row marked Various gathers the messages of one feature that do not share one start.
| Start of Message | Status | What It Means | Cases |
|---|---|---|---|
| Various | Undocumented: Only the program shows this item. It can change without notice. | Compaction. A compaction did not run or failed. | There are no messages to compact, or not enough. The compaction was canceled. Claude Code could not reduce the conversation below the limit. Attached media exceeds size limits. The model policy blocks the swap. |
Related Pages
- Sessions and Storage
- Request and Cache
- Models
- The Agent Loop
- Hooks and Settings pages in Extend and Configure.