fix(gateway): model-aware overflow threshold (unhardcode 200k context)#478
Conversation
## Bug
``DEFAULT_THRESHOLD.contextWindow`` was hardwired to 200_000 tokens
— right for Claude Opus 4.x but wrong for every other model. See
``bugs/known-issues.md#context-window-hardcoded``.
Operators switching the planner to GPT-4o-mini (128k context)
saw a provider-side ``context_length_exceeded`` 400 before
compaction fired, because the threshold thought there was still
~70k of runway. Operators switching to Gemini Flash (1M) paid
for summarization runs that weren't necessary.
## Fix
``thresholdForModel(model, override?)`` in
``src/session/overflow.ts`` resolves the context window from a
lookup table keyed on ``provider/modelId``. Populated with the
models the gateway actually runs against today:
- Anthropic 4.x (Opus / Sonnet / Haiku) — 200k
- OpenAI GPT-4o / GPT-4o-mini — 128k
- OpenAI GPT-4.1 / 4.1-mini — 1_047_576
- OpenAI o3 / o3-mini — 200k
Unknown models fall back to 128k. The fallback is deliberately
smaller than every common default — triggers compaction slightly
earlier than strictly necessary, never lets the request hit a
provider-side overflow. Strictly-safer default.
Operators with exotic models can:
- Pass ``override.contextWindow`` at the call site (used by
tests, planner config overrides).
- Declare the full threshold in ``gateway.config.json`` and
route it through the existing ``compactIfNeeded({threshold})``
signature — no schema changes needed.
``compactIfNeeded`` now resolves the threshold from ``input.model``
instead of the single ``DEFAULT_THRESHOLD`` constant. One-line
change at the call site; behavior unchanged for Claude Opus.
## Tests
New: ``tests/session/overflow-threshold.test.ts`` (14 tests).
Covers:
- Lookup for every provider/model in the table
- Unknown-model fallback to the 128k default (regression guard)
- Override precedence (operator escape hatch)
- Before/after overflow math at specific token counts — the
key regression test: GPT-4o-mini @ 100k now correctly
overflows where before it would NOT have.
Full suite: 127/127 pass.
## Known-issues entry
Marked fixed in ``bugs/known-issues.md#context-window-hardcoded``
with a pointer to the new resolver + test file.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughThe pull request implements model-aware context window threshold resolution. It adds a lookup table mapping provider/model identifiers to specific context windows, introduces a Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
What this fixes
known-issues.md#context-window-hardcoded — the overflow threshold was hardwired to 200k tokens (Claude Opus 4.x's window). Operators swapping to a different model got the wrong trigger point:
anthropic/claude-opus-4-7openai/gpt-4o-miniopenai/gpt-4.1How
New
thresholdForModel(model, override?)in gateway/src/session/overflow.ts resolves from a lookup table.compactIfNeededin gateway/src/session/compaction.ts now passes the planner's actual model through.Operators with unusual models still have two escape hatches:
compactIfNeeded({threshold: {contextWindow: 2_000_000}})— one-offgateway.config.json → agent.plannerwith acontextWindowfield — persistentTests
14 new tests in gateway/tests/session/overflow-threshold.test.ts guarding both the lookup table and the overflow math itself — the key regression being GPT-4o-mini @ 100k tokens now correctly triggers compaction where it would not have before.
Full suite: 127/127 pass.
Known-issues
Marked ✅ fixed in bugs/known-issues.md with pointers to the resolver + test file.
🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Tests