Claude Code auto-compacts at 400K in claude -p: how to keep the 1M window#
Published by Alex Sorokoletov
Since v2.1.334, the auto-compact window on most current 1M-token models is near 400,000 tokens, not 1M, and the change applies to claude -p and Agent SDK runs as well as the terminal.[1] A session now compacts at about 367K tokens. To keep the full window everywhere, set CLAUDE_CODE_AUTO_COMPACT_WINDOW=1m in the environment. /autocompact 1m, or the Auto-compact window row in /config, keeps it for the current model only.[2]
What changed in v2.1.334#
The v2.1.334 changelog says: "Changed auto-compact's per-model auto setting to near 400K on most current 1M models, -p and SDK runs too (rolling out; on now with third-party providers or telemetry off); see /autocompact".[1] The window is the conversation size that the compaction threshold is measured against. Claude Code keeps 20,000 tokens for the reply and a further buffer of about 13,000, so a 400K window compacts at about 367K. Before v2.1.334, the same model compacted at about 967K.
The new default applies when all of these are true:
| Condition | Detail |
|---|---|
| The model is a 1M-context model that this version knows | An unknown model ID or a 200K model is not changed |
The run comes from the CLI, claude -p, the GitHub Action, Claude Desktop, VS Code or the Agent SDK (CLI, Python, TypeScript) | Headless and SDK runs are included |
| No window is set for the model | No CLAUDE_CODE_AUTO_COMPACT_WINDOW, no autoCompactWindow in settings, no "Keep" answer to the one-time prompt |
| The rollout reaches you | On at once with Bedrock, Vertex or another third-party provider, or with telemetry off; on Anthropic's API, a server-side rollout decides |
v2.1.335 keeps the same default and fixes two side effects: the one-time prompt opening over a history search or menu, and the VS Code context indicator showing full after a session went back from the 400K window to 1M.[1]
Why headless runs are hit hardest#
In the terminal, Claude Code shows a one-time prompt: "Autocompact now defaults to tokens. Auto will now compact near tokens instead of 1M. Quality should stay the same for most tasks at lower usage and cost, especially on long sessions." Its "Keep current settings" answer records the old window for that model. A claude -p run, a CI job or an SDK agent never shows the prompt, never opens /config, and does not print a notice. Its sessions start to compact at about 367K with no other sign.
An agent whose working set is near 367K tokens fills the context again a few turns after each compaction. After three such refills in a row, a breaker trips: Claude Code skips the compaction and shows this message:
Autocompact is thrashing: the context refilled to the limit within 3 turns of the previous compact, 3 times in a row. A file being read or a tool output is likely too large for the context window. Try reading in smaller chunks, or use /clear to start fresh.
An orchestrator that restarts a failed session with fresh context starts the same cycle again. Large repository reads, long logs and big tool outputs are the usual causes. In this case the cause is the smaller window, not a single large file.
How to keep the 1M window#
Claude Code reads the window in this order, and the first match wins:
- The
CLAUDE_CODE_AUTO_COMPACT_WINDOWenvironment variable. modelSettings.<model>.autoCompactWindowin settings, which/autocompactand/configwrite, then the top-levelautoCompactWindow. The value"auto"does not count as a window.- The default: the server's value, else the 400K client default.
| Method | Scope | Use it for |
|---|---|---|
CLAUDE_CODE_AUTO_COMPACT_WINDOW=1m | Every model, every session that gets the variable | Fleets, CI, containers, SDK agents |
The same variable in the env block of settings.json (example below) | Every session that loads that settings file | A machine or a project |
"autoCompactWindow": 1000000 in settings.json | Every model that has no per-model value | A default that /autocompact can still change per model |
claude -p --autocompact 1m | That run | One script or one job |
/autocompact 1m or the Auto-compact window row in /config | The current model only, saved in user settings | Interactive use on one model |
The environment variable is the only method that wins over every saved setting and covers models that do not exist yet. /autocompact saves the size under the model's name, so the next model release starts on the 400K auto setting again. For an Agent SDK agent, pass the variable in the process environment or in the SDK's env option. For the full rules of the variable (accepted values, the 100K floor, suffixes), see CLAUDE_CODE_AUTO_COMPACT_WINDOW.
{
"env": {
"CLAUDE_CODE_AUTO_COMPACT_WINDOW": "1m"
}
}
How to check the window a session uses#
Run /context in the session: it shows the window and the point where auto-compact starts. The Auto-compact window row in /config shows the saved size for the current model. In a headless run there is no screen to check, so set the variable explicitly instead of relying on the default. Issue #113545 asks Claude Code to put the effective window in hook input, so that scripts can read it.[3]
Should you keep 400K?#
The prompt says quality should stay the same for most tasks at lower usage and cost. A smaller window sends fewer tokens per request, and the summary replaces old turns that a long session no longer needs. Keep 400K when sessions are long and their working set is small. Set 1M when an agent must hold a large codebase, long logs or many files at once, or when you see the thrashing message. Issue #113413 asks Anthropic to restore 1M as the default and to publish the evaluation behind 400K.[3]
FAQ#
Why does Claude Code compact at about 370K tokens now? v2.1.334 changed the auto window on most current 1M models to near 400K. Claude Code keeps 20,000 tokens for the reply and a buffer of about 13,000, so compaction starts at about 367K.
Does the change apply to claude -p and the Agent SDK?
Yes. The changelog names -p and SDK runs, and the default covers the CLI, the GitHub Action, Claude Desktop, VS Code and the Python, TypeScript and CLI SDK entry points.
How do I restore the 1M auto-compact window for all models?
Set CLAUDE_CODE_AUTO_COMPACT_WINDOW=1m in the environment or in the env block of settings.json. /autocompact 1m changes only the current model.
What does "Autocompact is thrashing" mean? The context filled to the limit within 3 turns of the previous compaction, 3 times in a row, so Claude Code skipped the next compaction. A larger window or smaller reads stop it.
Why do I see it now and not before? On Anthropic's API the change is a rollout. With Bedrock, Vertex or another third-party provider, or with telemetry off, it is on at once.
Sources: [1] Claude Code changelog, v2.1.334 and v2.1.335. [2] Lydia Hallie on X: keep 1M with /autocompact 1m or /config. [3] GitHub issues #113413 and #113545.
Related: CLAUDE_AUTOCOMPACT_PCT_OVERRIDE and the auto-compact window · "Auto-compact will soon summarize older messages" · "Compacted while idle" · Claude Code v2.1.334 · Claude Code env-var glossary