Skip to content

fix(claude): declare the model context window to Claude Code - #102

Merged
duyet merged 2 commits into
mainfrom
fix/claude-context-window
Oct 20, 2026
Merged

duyet merged 2 commits into
mainfrom
fix/claude-context-window

Conversation

@duyet

@duyet duyet commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

What

anyr claude now tells Claude Code the model's real context window through CLAUDE_CODE_MAX_CONTEXT_TOKENS.

Why

Claude Code 2.1.304 keys its model metadata on Anthropic model ids. A third-party id is an unknown model, so it opens on the built-in 200,000-token default. minimax/minimax-m3.1-flash reported 200k in /context while the catalog and the website say 1,000,000.

The CLI already resolved the number from /v1/models and carried it on BuildToolEnvInput.context_window, but never handed it over. It set CLAUDE_CODE_AUTO_COMPACT_WINDOW, which is the auto-compact threshold, not the window.

How

One insert in build_tool_env in src/spawn.rs, inside the claude branch.

if !is_virtual_preset(input.model) {
    if let Some(window) = input.context_window.filter(|n| *n > 0) {
        env.insert("CLAUDE_CODE_MAX_CONTEXT_TOKENS".into(), window.to_string());
    }
}

Guardrails.

  • An unknown window stays unset. Claude Code takes a declared value as a promise and sizes the session to it.
  • A virtual preset keeps its own window. Its hop is picked per request, and anyrouter/auto[1m] already carries the suffix Claude Code reads.
  • A window at or below 200,000 is still declared, so every surface agrees with the catalog.
  • CLAUDE_CODE_AUTO_COMPACT_WINDOW is untouched. It is the threshold, not the window.

Verification

$ anyr claude --model minimax/minimax-m3.1-flash --dry-run
ANTHROPIC_MODEL=minimax/minimax-m3.1-flash
CLAUDE_CODE_AUTO_COMPACT_WINDOW=1000000
CLAUDE_CODE_MAX_CONTEXT_TOKENS=1000000

Against the same base URL and the same model.

$ claude -p "/context"
**Tokens:** 43.7k / 1m (4%)

Before the fix the same session reported 26.3k / 200k (13%).

Tests

The first commit adds the failing test, the second the fix.

  • A concrete model with a 1,000,000 window declares 1000000.
  • An unknown window leaves the key absent.
  • A virtual preset leaves the key absent.
  • A window of 128,000 declares 128000.

cargo test passes, 416 tests across the crate. cargo fmt --check is clean.

Fixes #100

🤖 Generated with Claude Code

duyet and others added 2 commits October 20, 2026 16:35
Claude Code only knows Anthropic model ids, so a third-party id opens on its built-in 200k default. The CLI resolves the model's real window from /v1/models but never hands it over, so minimax/minimax-m3.1-flash (1M) reports 200k in /context while the website says 1M.

The test asserts the launch env declares CLAUDE_CODE_MAX_CONTEXT_TOKENS for a concrete model, leaves it unset for an unknown window and for a virtual preset, and still declares a window below 200k.

Co-authored-by: Duyet Le <me@duyet.net>
Co-authored-by: duyetbot <bot@duyet.net>
Co-Authored-By: Claude Code <noreply@anthropic.com>
Claude Code 2.1.304 keys its model metadata on Anthropic model ids. A third-party id is an unknown model, so it opens on the built-in 200,000-token default and minimax/minimax-m3.1-flash reported 200k in /context while the catalog and the website say 1,000,000.

The CLI already resolved the window from /v1/models and carried it on BuildToolEnvInput.context_window, but never handed it over. It set CLAUDE_CODE_AUTO_COMPACT_WINDOW, which is the auto-compact threshold, not the window.

The launch env now declares CLAUDE_CODE_MAX_CONTEXT_TOKENS from the resolved window. An unknown window stays unset, and a virtual preset keeps its own window because its hop is picked per request and anyrouter/auto[1m] already carries the suffix Claude Code reads.

Co-authored-by: Duyet Le <me@duyet.net>
Co-authored-by: duyetbot <bot@duyet.net>
Co-Authored-By: Claude Code <noreply@anthropic.com>
@duyet
duyet merged commit 6e969b2 into main Oct 20, 2026
14 of 16 checks passed
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.

Claude Code opens third-party models on its 200K default; the CLI never passes the gateway's context window

1 participant