Auto-compaction triggers at 41% actual context usage on 1M Opus 4.6 sessions (token estimation over-counting)

Resolved 💬 3 comments Opened Mar 16, 2026 by LMS927369 Closed Mar 19, 2026

Bug Description

Auto-compaction triggered at 41% actual context usage (410K/1M tokens) in a session with high tool_use volume (432 API calls). The compaction was a no-op (conversation preserved), but disrupted the session by firing PreCompact/PostCompact hooks.

Version: 2.1.76
Model: claude-opus-4-6 [1m] (1M context)
Platform: macOS Darwin 24.6.0

Evidence

API usage at time of auto-compact

From the session transcript, the last API response before compaction:

cache_read_input_tokens: 410,114
cache_creation_input_tokens: 472
input_tokens: 3
Total input: 410,589 tokens = 41.1% of 1M

Compaction was a no-op

Post-compact API responses show cache_read_input_tokens increased from 410,114 → 410,586. If the conversation had been summarized, the cache prefix would break and cache_read would drop to ~32K (system prompt only). Instead it grew — proving the conversation was preserved unchanged.

Session characteristics

  • 432 unique API calls (message IDs) before compaction
  • ~487 user messages (mostly tool_results)
  • 1,204 tool calls captured by hooks
  • Heavy tool_use pattern: 490 Bash, 347 Read, 198 Edit, 87 Grep, 28 Agent, 10 MCP
  • 5+ parallel background agents running at time of compaction

Internal estimator must have calculated ≥95%

The default CLAUDE_AUTOCOMPACT_PCT_OVERRIDE threshold is 95%. For auto-compact to fire at 41% actual usage, the internal token estimator must have over-counted by ~540K tokens (~2.3x actual). This matches the bug pattern fixed in v2.1.75 ("token estimation over-counting for thinking and tool_use blocks").

Related Fixes

  • v2.1.75: "Fixed token estimation over-counting for thinking and tool_use blocks, preventing premature context compaction" — this fix appears incomplete for 1M context sessions with high tool_use volume
  • v2.1.75: "Added 1M context window for Opus 4.6 by default" — 1M context is new as of this version
  • v2.1.76: "Fixed spurious 'Context limit reached' when invoking a skill with model: frontmatter on a 1M-context session" — confirms ongoing 1M context estimation issues
  • v2.1.76: "Fixed auto-compaction retrying indefinitely after consecutive failures" — confirms auto-compact can fire incorrectly

Reproduction

  1. Start a session with opus[1m] model
  2. Perform 400+ tool_use-heavy turns (heavy Read/Bash/Edit usage)
  3. Launch multiple parallel Agent calls
  4. Observe auto-compact triggering at ~40-45% actual usage (per status bar / API usage)
  5. Post-compact conversation is identical to pre-compact (no-op)

Workaround

CLAUDE_AUTOCOMPACT_PCT_OVERRIDE=100 disables auto-compact entirely. Manual /compact works correctly.

Expected Behavior

Auto-compaction should only trigger when actual context usage approaches the threshold (default 95%), not when the internal estimator over-counts due to thinking/tool_use block accounting errors.

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗