GuidesHosting choices

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.

November 23, 2026The Everpod team
The short answer

/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

# Compact instructions

When you are using compact, please focus on test output and code changes
Copied

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 sessionContext window in Claude CodeAuto-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 tokensAt about 967K tokens
Opus 4.6, Sonnet 4.6
200K; 1M only through the [1m] variant, which depends on your planAt the 200K boundary without [1m]
Sonnet 4.5, Haiku 4.5 and earlier models
200KWhen the conversation reaches the limit
Any model with CLAUDE_CODE_DISABLE_1M_CONTEXT=1
200KAt 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’sAs 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:

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:

WhereExampleWhat it covers
Environment variable
CLAUDE_CODE_AUTO_COMPACT_WINDOW=200000Every session started from that environment; meant for scripts and cloud environments
Launch flag
claude --autocompact 500kOne launch, saved nowhere; --autocompact auto runs at the tuned window
Command
/autocompact 500kThe 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": 500000Every model, from any settings file; a window /autocompact saved for a model in the same file wins for that model

To turn compaction off:

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 on
Copied

To 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:

WhatAfter 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):

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" }
        ]
      }
    ]
  }
}
Copied

How 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.
Copied

The 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\"'"
  }
}
Copied

It 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

CommandWhat happensUse it when
/compact [focus]
The conversation becomes a summary; same sessionThe same task goes on
/clear [name] (also /reset, /new)
A new, empty conversation; the old one is saved for /resume; costs nothingYou are switching to unrelated work
/rewind, then a Summarize option
Part of the conversation becomes a summaryOne long stretch, such as a debugging detour, is the problem
/btw <question>
Answered from what is already in context, without adding to itA side question
A subagent
Its reading happens in its own window; only its result comes backA 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.
Copied

A 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.

Run Claude Code on an always-on developer pod.

A developer pod is a permanent cloud computer (VPS), set up and secured for you, with your pick of Claude Code, Codex, OpenCode, Pi, Hermes and OpenClaw installed. Run more agents at once than your laptop can hold, and keep them working when it’s closed. From $24 a month, built in about ten minutes.