Fix Claude context usage normalization for UI meter, Fixes #2034#2551
Fix Claude context usage normalization for UI meter, Fixes #2034#2551amitbet wants to merge 3 commits intopingdotgg:mainfrom
Conversation
Treat Claude accumulated totals as processed-token telemetry unless a context-accurate used-token field is present, so context percentage indicators no longer pin to 100% on result-only snapshots. Co-authored-by: Cursor <cursoragent@cursor.com>
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. 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 |
ApprovabilityVerdict: Approved Straightforward bug fix for UI token meter display. Changes are limited to normalization logic for how token usage is shown, with corresponding test updates confirming the expected behavior. You can customize Macroscope's approvability policy. Learn more. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 8a1013a. Configure here.
| ...(inputTokens > 0 ? { inputTokens } : {}), | ||
| ...(outputTokens > 0 ? { outputTokens } : {}), | ||
| ...(maxTokens !== undefined ? { maxTokens } : {}), | ||
| ...(maxTokens !== undefined && hasDirectContextUsedTokens ? { maxTokens } : {}), |
There was a problem hiding this comment.
Task progress events lose maxTokens when lacking explicit used_tokens
Medium Severity
The condition maxTokens !== undefined && hasDirectContextUsedTokens prevents maxTokens from appearing in the output whenever the usage object lacks an explicit used_tokens/last_used_tokens field. The task_progress and task_notification handlers still pass context.lastKnownContextWindow as contextWindow, signaling context-accurate data, but that signal is now ignored. In multi-turn scenarios where the context window is already known, intermediate usage events emitted from task_progress will omit maxTokens, so the UI meter cannot display a percentage during active processing — only at turn completion when completeTurn separately re-adds it.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit 8a1013a. Configure here.


What Changed
Treat Claude accumulated totals as processed-token telemetry unless a context-accurate used-token field is present, so context percentage indicators no longer pin to 100% on result-only snapshots.
Why
Bug Fix: when using ClaudeCode context usage is always 100%
UI Changes
before:


after:
Checklist
Note
Low Risk
Low risk bug fix limited to Claude token-usage normalization and related tests; it only affects telemetry emitted in
thread.token-usage.updatedevents.Overview
Fixes Claude token-usage normalization so accumulated
result.usagetotals are no longer clamped to the model context window unless a context-accurateused_tokens/last_used_tokensfield is present.normalizeClaudeTokenUsagenow prefers direct context-usage fields (and only then clamps and includesmaxTokens), while result-only snapshots keep oversizedusedTokenstotals (optionally surfaced astotalProcessedTokenswhen combined with a prior context-accurate snapshot). Tests were updated to reflect the new event payload behavior for oversized totals.Reviewed by Cursor Bugbot for commit 8a1013a. Bugbot is set up for automated code reviews on this repo. Configure here.
Note
Fix Claude context usage normalization to preserve oversized token totals in UI meter
normalizeClaudeTokenUsagenow distinguishes between context-accurate usage (from directusedTokens/lastUsedTokensfields) and accumulated totals fromresult.usage, which can exceed the context window.usedTokensis clamped tocontextWindowandmaxTokensis included in the snapshot. When only accumulated totals exist, they are preserved unclamped andmaxTokensis omitted.ClaudeAdapter.ts, the call tonormalizeClaudeTokenUsagefor accumulatedresult.usageno longer passes acontextWindow, so those totals are never clamped.thread.token-usage.updatedevents for accumulated-only usage will no longer includemaxTokens, andusedTokensmay exceed the context window size.Macroscope summarized 8a1013a.