Guide · Claude Code context
When does Claude Code auto-compact, and how do you set the window?
Claude Code auto-compacts when the conversation reaches its auto-compact window, which by
default is the model's context limit, or about 967K tokens on a model with a native 1M window.
You set the window with /autocompact, the autoCompactWindow setting,
the --autocompact flag or CLAUDE_CODE_AUTO_COMPACT_WINDOW, and the
variable beats the other three. Behind a gateway, Claude Code cannot see a lower limit, so
someone has to give it the right window. Through Selan, the launch sets it from what the
gateway knows.
When does Claude Code compact by default?
With no window set, Claude Code compacts when the conversation reaches the model's context limit. Anthropic lists the exceptions:
- Models with a native 1M window compact at about 967K tokens. These are the Fable models, Sonnet 5 and later, Haiku 5.5, and Opus 4.7 and later.
- Opus 4.6 and Sonnet 4.6 compact at 200K unless you picked their
[1m]variant. CLAUDE_CODE_DISABLE_1M_CONTEXT=1holds native 1M models at 200K. A larger auto-compact window does not lift that hold.- A model id Claude Code does not recognize, such as a gateway's alias, compacts at the window Claude Code assumes for it, and a warning at startup names that window.
- Cloud sessions compact as the conversation approaches the model's limit.
Compaction clears older tool output, then summarises the conversation. Claude Code reloads the project-root CLAUDE.md and auto memory after it, and re-reads up to five recent files.
How do you set the auto-compact window?
Claude Code reads the window from four places:
| Where | What it covers | What overrides it |
|---|---|---|
/autocompact 500k | The current model, saved to your user settings under modelSettings | The flag, the variable, or a window in a higher settings scope such as managed settings |
"autoCompactWindow": 500000 | Every model, in any settings file | The flag, the variable, or a window /autocompact saved for a model in the same file |
claude --autocompact 500k | One launch, without changing what is saved | Only the environment variable |
CLAUDE_CODE_AUTO_COMPACT_WINDOW | Every launch started with it set | Nothing |
Each takes a window from 100K to 1M tokens. The command and the flag take 500k,
500000 or a bare 500, meaning thousands. Claude Code caps whatever
you set at the model's context window.
- In the variable, a bare number is tokens.
CLAUDE_CODE_AUTO_COMPACT_WINDOW=200reads as 200 tokens, so Claude Code warns and treats it as the 100K minimum. CLAUDE_AUTOCOMPACT_PCT_OVERRIDEcompacts at a share of the window. Set from 1 to 100, it can only lower the point.DISABLE_AUTO_COMPACT=1turns automatic compaction off./compactstill works by hand.
used_percentage still measures against the model's full window, so it no longer
tells you when compaction will run. /context shows what fills the window.
Why does a gateway or another model get the wrong window?
Behind ANTHROPIC_BASE_URL, Claude Code gives each model it recognizes the window
that model has on the Anthropic API. It cannot see a lower limit that the gateway, or the
provider behind it, enforces.
If your gateway refuses requests above 200K tokens, Anthropic's advice is to set
CLAUDE_CODE_AUTO_COMPACT_WINDOW=200000 where Claude Code starts. Otherwise a long
session outgrows what the gateway accepts before Claude Code compacts.
For a model id Claude Code does not recognize, Anthropic lists four fixes:
- A model newer than your Claude Code: run
claude update. - A gateway's name for a Claude model: map it with
modelOverrides, or give amodelPickerrowbehavesAsthat model. - A model with a 1M window: append
[1m]to the id where you set the model. - Any other window: set
CLAUDE_CODE_MAX_CONTEXT_TOKENS, such as400000, where Claude Code starts.
Claude Code knows each Claude model's window. It does not know your gateway's.
Should a team compact earlier to spend less?
Often, yes. Each turn sends the whole conversation again, so a session held at 600K sends less on every turn than one that runs close to 1M.
On the Anthropic API, the 1M window has no premium for tokens beyond 200K. Haiku 5.5 is the exception: its prompts over 100K tokens cost more per token.
For a whole team, put autoCompactWindow in managed settings. A developer's
/autocompact then saves its value but keeps the managed window. The
--autocompact flag and the environment variable still override managed settings
for a launch.
/compact with a focus, such as /compact focus on the auth bug fix, keeps
what you choose.
How does Selan set the window for a team?
Selan is a hosted gateway that runs Claude Code and Codex on the accounts your company already
pays for. selan claude can set CLAUDE_CODE_AUTO_COMPACT_WINDOW when it
launches Claude Code, the variable that overrides every other source.
- An owner can set a company context limit. In the control app's Settings, under Behaviour, Context limit offers Early (600K), Balanced (800K), Full or Let Claude decide.
- A model named with
--modelgets its real window. Selan tells Claude Code how big it is, so a 262K model compacts at 262K and not at an assumed 200K. If the company limit is smaller, the session compacts there instead. - A model picked in
/modelcompacts against one figure per launch: the company's context limit if an owner set one, otherwise Claude Code's 1M ceiling. When a mid-size window matters, name the model with--model. - Your own
--autocompactwins. Selan then sets no window for that launch and says so when Claude Code starts.
How a picked model compacts is in the docs on the picker, and naming any model is in run any model. What a repository's sessions cost, and what to cut, is in usage practices. Limits per person are on spend limits per developer.
Free for up to three seats, and the Free plan needs no card.
When is Claude Code's own setting the better answer?
When you run Anthropic's own Claude models straight on the Anthropic API. Claude Code knows
their windows, and /autocompact or autoCompactWindow is all a
developer needs to compact earlier.
A gateway earns its place once a team runs models from more than one provider, or behind a limit Claude Code cannot see. Which Claude Code gateway a team needs compares the kinds, and prompt caching covers the other half of what a long session costs.
The short version
- By default Claude Code compacts at the model's context limit, or about 967K tokens on a native 1M model.
CLAUDE_CODE_AUTO_COMPACT_WINDOWbeats--autocompact,/autocompactandautoCompactWindow; in the variable, a bare number is tokens.- Behind a gateway, Claude Code uses Anthropic's window for a Claude model and cannot see a lower limit.
- An unrecognized id needs
modelOverrides,behavesAs,[1m]orCLAUDE_CODE_MAX_CONTEXT_TOKENS. - Through Selan, a model named with
--modelcompacts at its real window, or at the company's context limit when that is smaller.
When Claude Code compacts, and how its settings, flag and variables set the window, is as Anthropic's model configuration page, context window page, environment variable reference and settings reference (code.claude.com/docs/en/model-config, /context-window, /env-vars and /settings-reference) stated it on 15 November 2026. It changes with nearly every release: check the date before relying on it. What Selan does is as its CLI, control app and docs stated it on that date.