fix(usage): cap token estimates at the model context window - #1653
fix(usage): cap token estimates at the model context window#1653kartikkabadi wants to merge 1 commit into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (5)
📝 WalkthroughWalkthroughChangesThe token estimator now accepts an optional context window and caps valid estimates. Request logging resolves Kiro and Cursor windows, caps estimated input tokens, and keeps provider-reported usage precedence. Tests cover adapter-specific and fallback behavior. Context-window token estimation
Estimated code review effort: 3 (Moderate) | ~20 minutes Merge Risk: ⚪ Minimal · up to The change caps token estimates at known model context windows while preserving valid reported usage; no actionable merge-blocking risk remains after normal checks and review. Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 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 Warning |
|
✅ Deterministic PR hygiene checks passed. |
⏳ DRAFT
What to do
Review readiness checklist
0/4 boxes ticked. This PR stays in draft until every box above is ticked. |
Port codex-router PR lidge-jun#140: a request the provider answered cannot have exceeded its context window, so the token estimate must never claim it did. estimateTokens gains an optional contextWindow cap. The request log caps the ESTIMATE field (attempt.inputTokenEstimate and the usage-fallback inputTokens) at the routed model's window while the combined inputTokens field keeps its max(reported, estimate) behavior; provider-reported positive counts are never reduced. Kiro auto and unknown adapters/models stay uncapped - a window is never invented. Window sources: KIRO_MODEL_CONTEXT_WINDOWS (src/providers/kiro-models.ts, mirroring adapters/kiro.ts kiroUpstreamContextWindow) and inferCursorContextWindow (src/adapters/cursor/discovery.ts), picked by route adapter in request-log.ts contextWindowForModel. Tests: extended tests/token-estimate.test.ts, added tests/request-log-estimate-cap.test.ts, updated the one request-log test that asserted an above-window estimate.
9cfbb8b to
21d27b4
Compare
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
Checklist complete:\n\n- [x] Local CI green: typecheck (tsc --noEmit) clean, privacy scan passed, full suite exit 0 on a combined branch of all three ports (#1652/#1653/#1655) — the only suite failures are 18 pre-existing dev-baseline failures in untouched files (server-management-auth, lab-*-regressions, codex-shim), identical on dev.\n- [x] On latest dev (rebased onto 8b1c620).\n- [x] Focused regression tests for this behavior: see test file(s) in the diff.\n- [x] Ready for review. |
lidge-jun
left a comment
There was a problem hiding this comment.
[Repository bug audit · 2026-08-14]
The estimate-capping rule is reasonable and the implementation correctly leaves positive provider-reported usage untouched. The adapter-selected context-window lookup also avoids guessing across shared model IDs.
Keep this draft until three gaps are closed: link a concrete issue/reproduction, rebase onto current dev, and run exact-head CI (currently action_required). Add a small provider-matrix test for adapter aliases/normalized model IDs so future registry naming changes do not silently disable the cap. This can then merge independently of the calendar-window fix in #1638.
Summary
Port of codex-router PR #140: a request the provider answered cannot have exceeded its context window, so the token estimate must never claim it did.
estimateTokensgains an optionalcontextWindowcap (newcapEstimateAtContextWindowhelper).attempt.inputTokenEstimate(persisted) and the usage-fallbackinputTokens— at the routed model's window.inputTokensfield keeps its existingmax(reported, estimate)behavior; provider-reported positive counts are never reduced by the cap.autoand unknown adapters/models stay uncapped (a window is never invented).Context window source (file:function):
KIRO_MODEL_CONTEXT_WINDOWSinsrc/providers/kiro-models.ts(lookup mirrorskiroUpstreamContextWindowinsrc/adapters/kiro.ts).inferCursorContextWindowinsrc/adapters/cursor/discovery.ts.contextWindowForModel(src/server/request-log.ts).Verification
bun test tests/token-estimate.test.ts— green (extended: cap at window, below-window unchanged, invalid windows ignored, min-1 floor).bun test tests/request-log-estimate-cap.test.ts— green (new file: capped estimate, below-window passthrough, positive counts never reduced, ESTIMATE vs combined field distinction, cursor adapter, unknown adapter, kiro auto).bun test tests/request-log.test.ts— green (73 pass / 0 fail across the three files); one pre-existing test that asserted an above-window estimate now asserts the capped value.Checklist
Review readiness checklist
This PR stays in draft until every box below is ticked. Tick all four boxes once the requirements are met:
All CI tests are green on my local testing.
I pushed my PR to the latest dev commit.
I resolved all correct Codex and CodeRabbit findings.
My PR is ready for review.
Summary by CodeRabbit
Bug Fixes
Tests