Repository navigation
fix: honor anthropic-beta context-1m to size the context window - #305
Conversation
When an Anthropic-format request carries an anthropic-beta header with a larger-context beta (e.g. context-1m-2025-08-07), the client has negotiated a 1M-token upstream window. The proxy now sizes its own context window to match (1,000,000), overriding the model-table default (200K for claude). The beta is the most direct per-request evidence of the window the upstream will serve, so it is resolved at the top of the native-window cascade and re-read on every request (the header may appear/disappear between requests of the same session). This fixes EMERGENCY / nudge / usage bands firing against the wrong denominator — e.g. 261K input was 131% of 200K (EMERGENCY) when it was really 26% of the 1M window the client negotiated. - anthropicBetaContextWindow(): parses context-Nm betas (N x 1,000,000), largest wins. - e2e: EMERGENCY suppressed at 261K input under the 1M beta; fires at the same input without the beta (200K). Plugin effectiveContextLimit is 1M with the beta and drops to 200K when the header disappears (per-request).
📦 Built Package ArtifactBranch: Option A — Install from npm PR tag (recommended)npm install -g billion-context@pr-305Each push to this PR publishes a new version under the Option B — Download artifact
tar xzf billion-context-pr305.tgz
npm install -g packageThis comment is automatically updated on each push. |
[bot] Reviewed PR #305 (branch Code review (
Pre-flight (ran on the branch):
Test meaningfulness (verified, not just claimed): I reverted the cascade change (kept the tests) → e2e test 1 (beta → no EMERGENCY) and test 3 (plugin No changes needed. Please merge when ready: #305 |
|
Model: glm-5.3 Reviewed (supervisor pass): |
What
When an Anthropic-format request carries an
anthropic-betaheader with a larger-context beta (e.g.context-1m-2025-08-07), the client has negotiated a 1M-token upstream window. The proxy now sizes its own context window to match (1,000,000), overriding the model-table default (200K for claude).Fixes #302 (reported in #292).
Why
The
anthropic-betaheader is forwarded upstream (so the provider correctly serves 1M), but bili's own window resolution missed it. The client-facing instance sized its window at the model-table 200K, so a 261K-token conversation read as 131% → EMERGENCY T1, when it was really only 26% of the 1M window the client negotiated. The inner instance (which saw the header) correctly sized 1M — the two instances disagreed.How
anthropicBetaContextWindow(headers)(src/server.ts): parses theanthropic-betaheader forcontext-Nmbetas →N × 1,000,000(largest wins). Generalizes to future larger-context betas; rejects malformed / non-context tokens.nativeFromFallbackflag (authoritative, not a low-confidence fallback, so the effective-window floor must not apply).Because the window flows into
reqConfig.modelContextLimit, this single change fixes all four surfaces the issue lists: compression thresholds, the EMERGENCY trigger, usage-percentage logs, and the window reported to clients/plugins (effectiveContextLimit).Tests
context-1m-2025-08-07→ 1,000,000; no header / no context beta → undefined (model default); mixed beta list; array header value; case/whitespace;context-Nmgeneralization + largest-wins; malformed rejection.effectiveContextLimitis 1M with the beta and drops to 200K when the header disappears on the next request of the same session (per-request resolution).Pre-flight
npm run typecheck— cleannpm test— 701 pass, 0 failnpm run build— success