CLAUDE_CODE_AUTO_COMPACT_WINDOW
Updated by Alex Sorokoletov
CLAUDE_CODE_AUTO_COMPACT_WINDOW sets the context size in tokens at which auto-compact summarizes the conversation; minimum 100,000, capped at the model's context window. Default: auto, a window tuned for the model, never above its context window. Values: Token count of 100000 or more; k and M suffixes (200k, 1M) need v2.1.304+. A bare number is tokens, not thousands. Values below 100k are raised to 100k; unparseable values are ignored. Added between v2.1.77 and v2.1.79 and still present in v2.1.332. It is listed in the official reference.
| Status | Present in v2.1.332 |
|---|---|
| Added | Added between v2.1.77 and v2.1.79 |
| Default | auto, a window tuned for the model, never above its context window |
| Values | Token count of 100000 or more; k and M suffixes (200k, 1M) need v2.1.304+. A bare number is tokens, not thousands. Values below 100k are raised to 100k; unparseable values are ignored |
| Category | Context and compaction |
| Official docs | Listed |
| Checked against | v2.1.330 |
How to set it
For one session, export it in the shell that starts Claude Code:
export CLAUDE_CODE_AUTO_COMPACT_WINDOW=400k
claude400k compact later than the default on a 1M-context model.
To apply it to every session, add it to the env object of a settings file:
// ~/.claude/settings.json
{
"env": {
"CLAUDE_CODE_AUTO_COMPACT_WINDOW": "400k"
}
}Known issues
- A bare number is read as tokens, so CLAUDE_CODE_AUTO_COMPACT_WINDOW=200 means 200 tokens and Claude Code raises it to the 100k minimum with a warning. /autocompact 200 means 200,000; for the variable write 200k or 200000. Source: docs: model config.
- The variable beats /autocompact, --autocompact and the autoCompactWindow setting, and /autocompact only reports the override. The status line's used_percentage is measured against the full model window, so it does not show when compaction will run. Source: docs: model config.
- One value applies to the main session and every subagent. A subagent's autoCompactWindow frontmatter can lower its own window but never raise it above the variable. Source: issue #90347.
Release notes that mention it
- CLAUDE_AUTOCOMPACT_PCT_OVERRIDE: Complete Guide to Claude Code Auto-Compaction Control
- Claude Code v2.1.75 — 2 New Environment Variables: Auto-Compact Window & Plugin Options
- claude-code-env-vars-v2-1-312
- claude-code-env-vars-v2-1-304
Related variables
CLAUDE_CODE_AUTO_MODE_COMPACT_THREAD_RULE: When set, enables the compact thread rule for auto-mode project threads; while off, thread handling stays on the original path.DISABLE_AUTO_COMPACT: Disable automatic compaction only (manual /compact still works).CLAUDE_AFTER_LAST_COMPACT: Request only conversation logs after last compaction.CLAUDE_CODE_AUTO_MODE_PRIOR_ASSISTANT_CONTEXT: Feeds prior assistant turns into the decision, so the classifier can see the reasoning behind an edit.CLAUDE_CODE_AUTO_MODE_SIBLING_CONTEXT: Includes sibling files (or sessions) when classifying.CLAUDE_CODE_IDLE_COMPACT_MIN_TOKENS: Sets the minimum conversation size, in tokens, before idle compaction runs.CLAUDE_CODE_SLEEP_COMPACT: When set, enables sleep compaction, which compacts the conversation while the session sits idle.CLAUDE_CODE_SLEEP_COMPACT_AFTER_MS: Milliseconds to wait after a turn before idle sleep compaction runs, clamped between 60000 and 210000; defaults to 150000.
Frequently asked questions
What does CLAUDE_CODE_AUTO_COMPACT_WINDOW do?
Sets the context size in tokens at which auto-compact summarizes the conversation; minimum 100,000, capped at the model's context window.
What is the default value of CLAUDE_CODE_AUTO_COMPACT_WINDOW?
Auto, a window tuned for the model, never above its context window.
What values does CLAUDE_CODE_AUTO_COMPACT_WINDOW accept?
Token count of 100000 or more; k and M suffixes (200k, 1M) need v2.1.304+. A bare number is tokens, not thousands. Values below 100k are raised to 100k; unparseable values are ignored.
Which Claude Code version has CLAUDE_CODE_AUTO_COMPACT_WINDOW?
Added between v2.1.77 and v2.1.79 and still present in v2.1.332.
How do I set CLAUDE_CODE_AUTO_COMPACT_WINDOW?
Export it before starting Claude Code (export CLAUDE_CODE_AUTO_COMPACT_WINDOW=400k), or add it to the "env" object in ~/.claude/settings.json to apply it to every session. 400k compact later than the default on a 1M-context model.
All variables, searchable: Claude Code environment variables.