[BUG] CLAUDE_AUTOCOMPACT_PCT_OVERRIDE=50 does not trigger autocompact — context reaches 100% requiring manual /compact

Status Open
Maintainer reply None cached
Activity 11 comments · opened Apr 23, 2026

Bug

CLAUDE_AUTOCOMPACT_PCT_OVERRIDE set to 50 in settings.json does not trigger automatic compaction. The context window reaches 100% and Claude Code shows the hard limit prompt (Context limit reached · /compact or /clear to continue), requiring manual /compact every time.

Version

Claude Code v2.1.118, Sonnet 4.6 (200k context), macOS Darwin 25.4.0

Reproduction

Settings (~/.claude/settings.json)

{
  "env": {
    "CLAUDE_AUTOCOMPACT_PCT_OVERRIDE": "50"
  }
}

Steps

  1. Start a Claude Code session with the above setting
  2. Run a moderately long session with multi-agent tasks and file reads (e.g., 5–10 subagent spawns)
  3. Observe that autocompact never fires at 50% — context grows to 100%
  4. Claude Code shows: Context limit reached · /compact or /clear to continue
  5. Must manually type /compact to continue

Expected behavior

Autocompact should trigger when context usage reaches 50%, preventing the hard limit.

Actual behavior

Autocompact never fires. Context reaches 100% regardless of the configured threshold.

Notes

  • Related to #42394 (same env var unreliable, but that report is about firing too early; this is the opposite: never fires)
  • The PreCompact hook (~/.claude/hooks/pre-compact.sh) runs correctly when manual /compact is executed, confirming the hook setup itself is not the issue
  • The problem appears to be that CLAUDE_AUTOCOMPACT_PCT_OVERRIDE is not being read or applied at all in v2.1.118 for Sonnet 4.6 sessions

View original on GitHub ↗

11 Comments

github-actions[bot] · 4 months ago

Found 3 possible duplicate issues:

  1. https://github.com/anthropics/claude-code/issues/36381
  2. https://github.com/anthropics/claude-code/issues/42817
  3. https://github.com/anthropics/claude-code/issues/42394

This issue will be automatically closed as a duplicate in 3 days.

  • If your issue is a duplicate, please close it and 👍 the existing issue instead
  • To prevent auto-closure, add a comment or 👎 this comment

🤖 Generated with Claude Code

wazionapps · 4 months ago

Running into the same territory from the opposite end — we want frequent compactions (our sessions average 59+ per run) and had to build our own compaction lifecycle management because relying solely on CLAUDE_AUTOCOMPACT_PCT_OVERRIDE was unreliable.

Two things we found:

  1. The env var seems to be ignored in certain session states — specifically when subagents are active or when the context is dominated by tool results rather than conversational turns. Our hypothesis is that the autocompact trigger checks context size on the main conversation loop, but subagent dispatches and large tool outputs can push past the threshold between checks.
  1. PreCompact/PostCompact hooks are the reliable path — instead of depending on the env var threshold, we built a checkpoint system around the PreCompact hook. Before compaction (whether manual or auto), we save a full session checkpoint (active task state, calibration, mental state). After compaction, we restore from that checkpoint. This makes compaction safe regardless of when it fires, which is arguably more important than controlling when it fires.

The deeper issue is that compaction timing and compaction safety are conflated — if compaction were always safe (preserving identity, task state, and relational context), the threshold would matter less. We documented our approach to making compactions non-destructive in NEXO Brain (open-source MCP server) — see the PreCompact/PostCompact hook system and the checkpoint/diary machinery.

junaidtitan · 4 months ago

Ran into the same frustration with unreliable autocompact thresholds. Cozempic's guard daemon bypasses this entirely — it monitors the session JSONL directly and applies 18 pruning strategies at 4 progressive thresholds before context ever hits 100%. No reliance on CLAUDE_AUTOCOMPACT_PCT_OVERRIDE. pip install cozempic && cozempic init — hooks wire via SessionStart. https://github.com/Ruya-AI/cozempic

stevenfreedman · 3 months ago

Confirming this regression persists in v2.1.138 on Linux (Ubuntu, kernel 6.17, bash) — 20 patch releases past the v2.1.118 originally reported, same behavior with CLAUDE_AUTOCOMPACT_PCT_OVERRIDE=40. Tested in v2.1.133, .136, .137, .138 — env var is correctly visible to hooks (process.env.CLAUDE_AUTOCOMPACT_PCT_OVERRIDE === "40") but auto-compaction does not fire until ~78–85% regardless.

Binary MD5s of v2.1.136/.137/.138 differ (genuine new builds), but no v2.1.123→v2.1.138 release note mentions auto-compact behavior.

Sibling issues still OPEN that share this root cause: #18264 (autoCompact:false ignored, fires ~78%), #18152 (off-flag won't allow continue), #19018 (hard "Context limit reached"). Closed-without-fix priors: #36381, #31806, #56666.

Local mitigation: Stop hook reads CC's native context_window.used_percentage from the hook stdin payload and emits a manual-compact prompt at target − 2pp. The native field is the only reliable signal source we've found.

WillyDrucker · 3 months ago

Might be useful.

I had my CLAUDE_AUTOCOMPACT_PCT_OVERRIDE=55 set for a few weeks and working. Changed it to 80 recently and never worked thereafter. Even setting back to 55, restarting sessions, new sessions. Everything fires at 100% now, full 1M tokens to trigger autocompact. All I did was change the value.

Sn3th · 3 months ago

use "CLAUDE_CODE_AUTO_COMPACT_WINDOW=1000000" this sets and unsets auto compaction... hope that helps
usage
export CLAUDE_AUTOCOMPACT_PCT_OVERRIDE=80 <-- set to whatever you want...
export CLAUDE_CODE_AUTO_COMPACT_WINDOW=1000000 <-- must exist

WillyDrucker · 3 months ago

Thanks brother. The CLAUDE_CODE_AUTO_COMPACT_WINDOW=1000000 sets the ceiling which I just set to 550000 and works. Will have to test to the CLAUDE_AUTOCOMPACT_PCT_OVERRIDE={value}. May not work anymore. This setting appears to be demoted to TestPCT. Manually setting the window does work though as a workaround.

junaidtitan · 3 months ago

We hit the same 'override is ignored, context just climbs to 100%' issue and worked around it in cozempic (github.com/Ruya-AI/cozempic): rather than relying on the native auto-compact threshold, its guard daemon prunes + reloads at configurable token thresholds, so you get a reliable early trigger regardless of whether the override is honored. Open-source, auto-runs (pipx install cozempic). Would love feedback if you give it a shot — does it give you the early-compaction behavior the override was supposed to?

WillyDrucker · 3 months ago

Cozempic, keep the code lean, love it.

Just as Sn3th said, both values have to be set. I had a few issues using only. CLAUDE_CODE_AUTO_COMPACT_WINDOW=550000 (55%).

Instead I've been testing
CLAUDE_CODE_AUTO_COMPACT_WINDOW=1000000
along with
CLAUDE_AUTOCOMPACT_PCT_OVERRIDE=55
and it seems to be working reliably to achieve autocompacts. Still some loose issues there, when it actually honors this, before the session starts, after a compact, etc, but better.

junaidtitan · 3 months ago

Thanks @WillyDrucker — 'keep the code lean' is the whole ethos (zero deps, stdlib only), so that means a lot. And yeah, the env-var dance is exactly what the guard sidesteps: it prunes + reloads at its own thresholds, so you get the early trigger regardless of whether AUTOCOMPACT_PCT_OVERRIDE / AUTO_COMPACT_WINDOW are honored that session. If you end up running both, genuinely curious which feels more reliable for you.

ElliotDrel · 1 month ago

Same here on v2.1.187 (headless claude -p / SDK path): with CLAUDE_AUTOCOMPACT_PCT_OVERRIDE set alone, autocompact never fires and context runs to the limit.

The override reaches the subprocess -- it echoes inside tool calls -- but the autocompact logic never acts on it. Set, visible, no effect.

What does work: setting CLAUDE_CODE_AUTO_COMPACT_WINDOW alongside it. The pct override does nothing on its own. Once the window var is set, the pct is applied as a multiplier -- effective trigger ~= WINDOW x (pct/100). With WINDOW=500000 and pct 60 I get a deterministic compact at ~300k tokens.

One caveat worth documenting: if the effective trigger falls below the session's baseline injected context, autocompact compacts, the context immediately refills past the limit, and the built-in thrash guard aborts the whole turn:

Autocompact is thrashing: the context refilled to the limit within 3 turns of the previous compact, 3 times in a row.

So the window var is a real lever, and it can be set too low.

Summary: the pct override being silently ignored on its own is the bug. The window var is the current workaround.