Skip to content

fix(gateway): model-aware overflow threshold (unhardcode 200k context)#478

Merged
raahulrahl merged 1 commit into
mainfrom
fix/context-window-per-model
Apr 19, 2026
Merged

fix(gateway): model-aware overflow threshold (unhardcode 200k context)#478
raahulrahl merged 1 commit into
mainfrom
fix/context-window-per-model

Conversation

@raahulrahl

@raahulrahl raahulrahl commented Apr 19, 2026

Copy link
Copy Markdown
Member

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:

Model Actual window Before this PR After
anthropic/claude-opus-4-7 200k 200k ✓ 200k ✓
openai/gpt-4o-mini 128k 200k (too late → provider 400) 128k ✓
openai/gpt-4.1 ~1M 200k (too early → wasted compaction) ~1M ✓
unknown model 200k (unsafe) 128k (conservative default)

How

New thresholdForModel(model, override?) in gateway/src/session/overflow.ts resolves from a lookup table. compactIfNeeded in 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-off
  • gateway.config.json → agent.planner with a contextWindow field — persistent

Tests

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

    • Context-window thresholds are now model-dependent, with appropriate limits for each AI model and a conservative fallback for unknown models.
  • Tests

    • Added comprehensive test suite validating model-specific threshold resolution and compaction behavior across various model families.

## 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>
@coderabbitai

coderabbitai Bot commented Apr 19, 2026

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 19438610-a2b8-4583-b337-6ef95651d892

📥 Commits

Reviewing files that changed from the base of the PR and between 3e74b90 and 1db970e.

📒 Files selected for processing (4)
  • bugs/known-issues.md
  • gateway/src/session/compaction.ts
  • gateway/src/session/overflow.ts
  • gateway/tests/session/overflow-threshold.test.ts

📝 Walkthrough

Walkthrough

The pull request implements model-aware context window threshold resolution. It adds a lookup table mapping provider/model identifiers to specific context windows, introduces a thresholdForModel() function for threshold resolution, reduces the conservative fallback from 200k to 128k tokens, and updates compaction logic to leverage the planner's actual model instead of a hardcoded default.

Changes

Cohort / File(s) Summary
Documentation
bugs/known-issues.md
Updated known-issue entry marking "context-window-hardcoded" as fixed, documenting that thresholdForModel() in overflow.ts resolves thresholds via model lookup with 128k fallback, and noting test coverage in overflow-threshold.test.ts.
Model-aware Threshold Resolution
gateway/src/session/overflow.ts
Added CONTEXT_WINDOW_BY_MODEL lookup table covering Anthropic Claude and OpenAI GPT models; introduced thresholdForModel() exported function that prioritizes explicit overrides, applies model-specific lookup, and falls back to DEFAULT_THRESHOLD; lowered DEFAULT_THRESHOLD.contextWindow from 200k to 128k.
Compaction Logic
gateway/src/session/compaction.ts
Updated threshold selection to call thresholdForModel(input.model) instead of DEFAULT_THRESHOLD, enabling model-dependent compaction triggers; adjusted imports accordingly.
Test Coverage
gateway/tests/session/overflow-threshold.test.ts
Added 115-line Vitest suite validating thresholdForModel() returns correct context windows for known models (200k for Claude 4.x, 128k for GPT-4o, ~1.04M for GPT-4.1), verifies 128k fallback for unknown models, confirms override behavior, and tests isOverflow triggering at correct token thresholds per model.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A model's mind, once locked in stone at two-hundred—
Now sees the world through context windows bright,
Each Claude and GPT finds its rightful perch,
While strangers rest in cautious one-two-eight.
The planner knows its kin, at last, by name! 🎯

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/context-window-per-model

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@raahulrahl
raahulrahl merged commit f3007b0 into main Apr 19, 2026
3 of 4 checks passed
@coderabbitai coderabbitai Bot mentioned this pull request Jul 15, 2026
3 tasks
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