Claude Code /compact, auto-compact and the context window
What /compact keeps, when auto-compact runs on each model (about 967K tokens on 1M windows), idle compaction, /autocompact, the compact hooks, and /clear.
/compact replaces the conversation so far with a summary, so a long Claude Code session can carry on with room in its context window. Add a focus to say what the summary must keep, as in /compact keep the API changes and the test commands. Claude Code also compacts by itself: at about 967K tokens on the 1M-token models that are now the default (Opus 5.5 among them), at the 200K boundary on a 200K window, and, on a Claude subscription within its included usage, when a conversation of 200,000 tokens or more has sat idle for about an hour. Afterwards it reloads your project-root CLAUDE.md, auto memory, the plan from plan mode and up to five of the files Claude was working on; instructions you gave only in the chat can be lost. /context shows what fills the window, /autocompact 500k makes it compact earlier, and /clear starts an empty conversation instead, which costs nothing.
What /compact does
Per Anthropic’s context window page (Claude Code 2.1.330, November 2026), /compact “replaces the conversation with a structured summary.” You stay in the same session, and the summary stands in for the messages it replaced. The overview of how Claude Code works is plain about the cost: “The summary keeps your requests and key code snippets, but detailed instructions from early in the conversation may be lost.”
It frees less than the conversation’s length suggests, because much of what was in context comes straight back. In Anthropic’s illustrative session, 22,225 tokens are in use before /compact and 17,095 after: the startup content (system prompt, CLAUDE.md files, auto memory, skill descriptions), a 1,725-token summary, four files Claude had read and the body of a skill it had used. What goes is the conversation itself: the messages, the tool output, and the files Claude read beyond the few it re-reads.
Writing the summary is a request of its own, which repeats the system prompt, the tools and the whole history being summarized. The prompt caching page says that while the cache is warm it “costs a fraction of what the context size suggests”, but after a break longer than the cache lifetime (about an hour on a subscription, five minutes on an API key, a cloud provider or usage credits) it reprocesses the history as uncached input, “why /compact costs the most when you resume an old session.” /usage can recommend compacting before a break, in a row such as Compact before you step away · run /compact. A long conversation also draws on your plan with every request, since each one sends all of it; reading that side is in Claude Code usage limits.
On a conversation of one exchange, /compact answers with Not enough messages to compact. There is nothing earlier to summarize, and the space is taken by that prompt and what Claude Code sends with every request. A compacted session you resume later opens at a Conversation compacted row, and Claude carries on from the last summary, the messages that compaction left unsummarized and every message since; the messages it summarized are not loaded again.
Telling the summary what to keep
- A focus after the command:
/compact focus on the auth bug fix. In the docs’ words, “The summary keeps what you choose instead of what the automatic pass guesses is important,” so it pays to compact this way before starting a long new task rather than wait for the automatic pass. - A standing section in the project’s CLAUDE.md. Anthropic’s cost guide gives this form, under its own heading:
# Compact instructions
When you are using compact, please focus on test output and code changesCopied- A
PreCompacthook, whose output becomes instructions for the summary, for instructions a script works out at the time (hooks are below). - Part of the conversation only: run
/rewind(or pressEsctwice on an empty prompt), pick a message and choose Summarize from here, which compresses the conversation from that message on, or Summarize up to here, which compresses everything before it and keeps the later messages intact. Highlight the option and type in its add context (optional) row to guide that summary. Per the checkpointing docs, files on disk don’t change and the original messages stay in the session transcript.
When auto-compact runs
Auto-compact is on by default. With no window of your own set, the point where it runs depends on the model, per the model configuration page:
| Model or session | Context window in Claude Code | Auto-compacts by default |
|---|---|---|
Opus 5.5 (the default model on Pro, Max, Ultra, Team, Enterprise and the API), Opus 4.7 and later, Sonnet 5 and later, Haiku 5.5, Fable models | 1M tokens | At about 967K tokens |
Opus 4.6, Sonnet 4.6 | 200K; 1M only through the [1m] variant, which depends on your plan | At the 200K boundary without [1m] |
Sonnet 4.5, Haiku 4.5 and earlier models | 200K | When the conversation reaches the limit |
Any model with CLAUDE_CODE_DISABLE_1M_CONTEXT=1 | 200K | At the 200K boundary |
A model ID Claude Code doesn’t recognize, such as a gateway alias | Assumed 1M since v2.1.323 (200K before) | At the window it assumes |
Cloud sessions | The model’s | As the conversation approaches the limit |
Two of those rows need a closer look. Anthropic’s API documentation lists Opus 4.6 and Sonnet 4.6 with a 1M window, but Claude Code gives them 200K unless you select the variant, as in /model claude-opus-4-6[1m]; on Max, Ultra, Team and Enterprise, Opus 4.6 at 1M is included, while Sonnet 4.6 at 1M needs usage credits on every subscription plan and Pro needs them for both. And on Amazon Bedrock and Google Cloud’s Agent Platform the sonnet and haiku aliases still resolve to Sonnet 4.5 and Haiku 4.5, both 200K models, and Microsoft Foundry’s default model is Sonnet 4.5, so a session there can be on a 200K window without anyone choosing one. Every model’s window, and the one each plan and provider starts you on, is in Claude Code models.
On the 1M models, Anthropic’s best-practices page puts it simply: “you’ll rarely run out of context.” The idle rule below starts far sooner, at 200,000 tokens.
Compacted while you were away: idle compaction
Idle compaction summarizes a long conversation you have left, shortly before its prompt cache expires, and your next message then sends the summary instead of the whole conversation. If you come back to a session and find it already summarized, look for its notice: the terminal shows a line that starts Compacted while idle, before the prompt cache expired; the VS Code chat panel and the desktop app show a notice that includes “a summary written while you were away”. Per the settings reference, it runs only when all of these hold:
- The session is interactive: the terminal, the VS Code chat panel, or the desktop app’s Code tab on your machine or over SSH.
- Auto-compact is on.
- The conversation is on the one-hour prompt cache, which Claude Code asks for by default only on a Claude subscription within its included usage.
- Its context has reached 200,000 tokens. Change that with
CLAUDE_CODE_IDLE_COMPACT_MIN_TOKENS; values under 100,000 are raised to 100,000. - About an hour has passed since the last request, and the computer stayed awake.
To stop it, set "idleCompaction": false in a settings file (Claude Code 2.1.290 and later) or switch off the Idle compaction row in /config (2.1.302 and later). Turning auto-compact off stops idle compaction too.
Compacting earlier, later or not at all
The auto-compact window is “how full the context window can get before Claude Code compacts the conversation.” There are four places to set it, and the variable overrides the other three:
| Where | Example | What it covers |
|---|---|---|
Environment variable | CLAUDE_CODE_AUTO_COMPACT_WINDOW=200000 | Every session started from that environment; meant for scripts and cloud environments |
Launch flag | claude --autocompact 500k | One launch, saved nowhere; --autocompact auto runs at the tuned window |
Command | /autocompact 500k | The current model, now and in later sessions, saved to user settings under modelSettings; /autocompact auto returns to the window tuned for the model, and the bare command shows the current window |
Setting | "autoCompactWindow": 500000 | Every model, from any settings file; a window /autocompact saved for a model in the same file wins for that model |
- Sizes: 100K tokens is the minimum, and the window is capped at the model’s context window, so it can bring compaction forward but never past the limit. The command and flag take
200000,500k,1M, or a bare number from 100 to 1000 meaning thousands. - The variable reads numbers literally:
CLAUDE_CODE_AUTO_COMPACT_WINDOW=200is 200 tokens, treated as the 100k minimum with a warning, where/autocompact 200is 200,000. Suffixes such as200kwork in the variable from 2.1.304. - Managed settings that set a window keep it in force over
/autocompact, which saves your value and says so; the flag is not overridden that way. - By percentage:
CLAUDE_AUTOCOMPACT_PCT_OVERRIDE=50triggers at half the window. It can only lower the threshold and, per the environment variable reference, “applies only in sessions that compact before the model’s context limit”, subagents included. - One subagent:
autoCompactWindow: 200000in its frontmatter (2.1.296 and later) makes that subagent compact earlier.
To turn compaction off:
"autoCompactEnabled": falsein settings, or the Auto-compact row in/config: no automatic or idle compaction, and/compactstill works.DISABLE_AUTO_COMPACT=1: the same for one session. Whichever of the two turns it off, the other cannot turn it back on.DISABLE_COMPACT=1: no compaction of any kind,/compactincluded.
With auto-compact switched off in /config, a full window stops the turn instead:
Context limit reached · /compact or /clear to continue · auto-compact is off · /config to turn it onCopiedTo keep a 1M model at 200K, CLAUDE_CODE_DISABLE_1M_CONTEXT=1 holds it there: with auto-compact on it compacts at 200K, with it off it stops there, and a window set above 200K does not lift the hold.
What survives a compaction
From the docs’ table, shortened:
| What | After compaction |
|---|---|
System prompt and output style | Still apply |
Project-root CLAUDE.md, rules without paths:, auto memory | Re-read from disk |
The plan Claude wrote in plan mode | Re-read from disk |
Skill descriptions | Rebuilt and re-injected (2.1.304 and later) |
Files Claude read or edited | Up to five re-read, most recently modified first; a file over 5,000 tokens comes back as a path only, shown as Referenced file |
Skills Claude used | Bodies re-injected up to 5,000 tokens each and 25,000 in all, oldest dropped first; a cut keeps the start of the SKILL.md |
Rules with paths:, CLAUDE.md files in subdirectories | Summarized away; they load again when Claude next touches a matching file |
Context hooks added earlier | Summarized with the rest |
Background commands and background subagents | Keep running; Claude is reminded which |
Claude’s task list | Persists |
Instructions you gave only in the chat | Only what the summary carries |
So a rule that has to hold after compaction belongs on disk: in the project-root CLAUDE.md, or in a rule file without paths: frontmatter, which the docs suggest for exactly this. The same goes for limits you state in conversation in auto mode: Anthropic warns that “don’t push until I’ve reviewed” “can get lost when the conversation compacts, so anything that matters belongs in a deny rule.”
The hooks around compaction
Three hook events fire around a compaction, per the hooks reference. The first two match manual (/compact) or auto (auto-compact and idle compaction):
PreCompactruns before. If it exits 0, its standard output becomes instructions for the summary, cut at 10,000 characters per hook. Exit 2, or return"decision": "block", to stop the compaction: a proactive auto-compact is then skipped and the conversation goes on uncompacted, but blocking one that is recovering from a context-limit error makes the request fail. Its input carriestriggerandcustom_instructions, the text typed after/compact(null for automatic runs).PostCompactruns after and receivescompact_summary, the summary just written, which is how to log or keep it. It cannot block anything.SessionStartwith thecompactmatcher runs after compaction too, and its output is added to the new context: the way to put something back every time.
In a project’s .claude/settings.json, the documented shape with commands of your own:
{
"hooks": {
"PreCompact": [
{
"matcher": "auto",
"hooks": [
{ "type": "command", "command": "echo 'Keep the failing test names and the migration plan.'" }
]
}
],
"SessionStart": [
{
"matcher": "compact",
"hooks": [
{ "type": "command", "command": "git log --oneline -5" }
]
}
]
}
}CopiedHow hooks are configured, and the other events, are in Claude Code hooks.
Seeing what fills the context
/context draws the window as a coloured grid and breaks it down by category: system prompt, system tools, MCP tools, custom subagents, memory files, skills and messages, with suggestions where something is heavy. In fullscreen mode it collapses the per-item list; /context all expands it. When the conversation is over a limit, it opens with one of two warnings:
Context exceeds the 200k-token limit by 94k tokens — run /compact or /clear to continue.
Context is 94k tokens past the 200k-token compaction window — run /compact to reduce usage.CopiedThe first means the model’s own window; with auto-compact on you can simply send your next message. The second means a compaction window that sits below the model’s own, such as the 200K boundary on a 1M model, where, per the error reference, “requests past it can still succeed.”
MCP tool definitions load on demand by default, so only tool names and server instructions take space until Claude uses a tool, and /mcp disable <name> removes a server you are not using. For a figure that stays on screen, the status line gets context_window.used_percentage, as in Anthropic’s one-line example:
{
"statusLine": {
"type": "command",
"command": "jq -r '\"[\\(.model.display_name)] \\(.context_window.used_percentage // 0)% context\"'"
}
}CopiedIt counts input tokens only and measures against the model’s full window, so it does not show where a window you set will trigger, and /context can read a little higher because it adds an estimate for messages since the last response.
Compact, clear, or something else
| Command | What happens | Use it when |
|---|---|---|
/compact [focus] | The conversation becomes a summary; same session | The same task goes on |
/clear [name] (also /reset, /new) | A new, empty conversation; the old one is saved for /resume; costs nothing | You are switching to unrelated work |
/rewind, then a Summarize option | Part of the conversation becomes a summary | One long stretch, such as a debugging detour, is the problem |
/btw <question> | Answered from what is already in context, without adding to it | A side question |
A subagent | Its reading happens in its own window; only its result comes back | A large search or a pile of logs |
Anthropic’s advice is to clear between unrelated tasks even on 1M models, since every request sends the whole conversation. Name the conversation you are leaving with /clear oauth-migration to find it again in the /resume picker; in the same Claude Code process the rewind menu also offers a /resume <session-id> (previous session) entry. When work would flood the window, a subagent keeps it out of yours, and subagents compact on their own as their windows fill.
When compaction fails or keeps repeating
Context limit reached · /compact or /clear to continue
This is how an interactive session shows Prompt is too long. With auto-compact on it is rare; if you turned auto-compact off, turn it back on or run /compact yourself before the window fills. If the line reads Prompt is too long · automatic compaction failed: <the underlying error>, automatic compaction itself failed, for example on an unavailable model or an authentication failure: /compact fails the same way until you fix the error it names.
Autocompact is thrashing
Autocompact is thrashing: the context refilled to the limit... means compaction worked but one file or tool output filled the window again several times in a row, so Claude Code stopped retrying. The troubleshooting page’s fixes: have Claude read the file in smaller pieces, run a focused /compact keep only the plan and the diff, move the large-file work to a subagent, or /clear. If it comes back after a clear, compare the Messages row in /context with the rows above it: when the others add up to more, what loads at startup (CLAUDE.md files, MCP tools, skills) leaves too little room to work in, and that is what to trim.
Other models and gateways
Since 2.1.323 Claude Code assumes a 1M window for a model ID it doesn’t recognize, where it used to assume 200K, and warns:
Claude Code doesn't recognize the model "my-gateway-opus", so it assumes a context window of 1m tokens. Update Claude Code, or see https://code.claude.com/docs/en/model-config#correct-the-window-for-a-gateway-or-custom-model-id to set the window.CopiedA model with a smaller real window can then fill before Claude Code compacts it, and the changelog warns that such sessions, once full, “can fail every turn until /clear.” Give Claude Code the real size with CLAUDE_CODE_MAX_CONTEXT_TOKENS=400000 (on its own it applies only to IDs Claude Code does not resolve to a known Claude model), hold everything at 200K with CLAUDE_CODE_DISABLE_1M_CONTEXT=1, or, if the gateway rejects requests over 200K, set CLAUDE_CODE_AUTO_COMPACT_WINDOW=200000. The rest of running Claude Code through a proxy is in Claude Code Router.