Skip to content

feat: replace hardcoded 80% usable context ratio with autocompact formula#319

Draft
Koriit wants to merge 1 commit intosirmalloc:mainfrom
Koriit:feat/autocompact-threshold
Draft

feat: replace hardcoded 80% usable context ratio with autocompact formula#319
Koriit wants to merge 1 commit intosirmalloc:mainfrom
Koriit:feat/autocompact-threshold

Conversation

@Koriit
Copy link
Copy Markdown

@Koriit Koriit commented Apr 18, 2026

Summary

  • Replace the flat 0.8 (80%) usable context ratio with Claude Code's actual autocompact threshold formula: effectiveWindow - bufferTokens
  • This changes usable tokens from 160k → 167k (200k windows) and 800k → 967k (1M windows), matching when Claude Code actually triggers compaction
  • Add support for CLAUDE_AUTOCOMPACT_PCT_OVERRIDE resolved from shell env → Claude Code settings.json env block → null

Test plan

  • All 750 tests pass (bun test)
  • Lint and type check clean (bun run lint)
  • Verify usable context percentage display with piped input
  • Verify with CLAUDE_AUTOCOMPACT_PCT_OVERRIDE set in environment
  • Verify with CLAUDE_AUTOCOMPACT_PCT_OVERRIDE set in ~/.claude/settings.json env block

🤖 Generated with Claude Code

…mula

Replace the flat 0.8 ratio used to estimate usable context tokens with
Claude Code's actual autocompact threshold formula:

  effectiveWindow = contextWindow - 20,000  (reserved for summary output)
  threshold       = effectiveWindow - 13,000 (buffer)

This changes usable tokens from 160k to 167k (for 200k windows) and
from 800k to 967k (for 1M windows), matching what Claude Code actually
triggers autocompact at.

Add support for CLAUDE_AUTOCOMPACT_PCT_OVERRIDE which allows users to
lower the threshold via a percentage of the effective window. The value
is resolved from the shell environment first, then Claude Code's
settings.json env block, with invalid values silently ignored.

- New src/utils/autocompact.ts resolver with env > settings.json cascade
- computeUsableTokens() with Math.max(1, ...) floor guard for small windows
- Updated CLAUDE.md documentation

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Koriit Koriit marked this pull request as draft April 18, 2026 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant