Add --max-context flag to cap context window usage

Resolved 💬 13 comments Opened Mar 15, 2026 by profff Closed Jun 6, 2026

Problem

With the recent upgrade of Opus 4.6 to 1M context, my API quota burns ~5x faster than before. I was working comfortably at 200K and have no need for 1M in most sessions. There's currently no way to limit the context window size Claude Code uses.

Proposed Solution

Add a --max-context <tokens> CLI flag (and/or a settings option) that caps the effective context window. For example:

claude --max-context 200000

This would:

  • Trigger auto-compaction at the same relative thresholds (64-75%) but relative to the cap, not the model's max
  • Prevent the context from growing beyond the specified limit
  • Allow users to control their token spend per session

A settings-level default would also be useful:

{
  "maxContextTokens": 200000
}

Use Case

  • Quota management: Users on usage-based billing who don't need the full context window
  • Consistent behavior: Researchers benchmarking across sessions want reproducible context sizes
  • Cost control: Especially relevant as context windows keep growing (200K → 1M → ?)

Alternatives Considered

  • Frequent /compact: Manual, error-prone, doesn't prevent the context from growing in between
  • Shorter sessions: Loses continuity
  • Hooks: PreToolUse hooks don't have access to current context token count, so can't build this as a workaround

Environment

  • Claude Code on Windows (MSYS2/Git Bash)
  • Opus 4.6 with 1M context
  • Previously worked well with 200K limit

View original on GitHub ↗

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