Skip to content

Fix Claude context usage normalization for UI meter, Fixes #2034#2551

Open
amitbet wants to merge 3 commits intopingdotgg:mainfrom
amitbet:fix/claude-context-meter
Open

Fix Claude context usage normalization for UI meter, Fixes #2034#2551
amitbet wants to merge 3 commits intopingdotgg:mainfrom
amitbet:fix/claude-context-meter

Conversation

@amitbet
Copy link
Copy Markdown

@amitbet amitbet commented May 6, 2026

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:
image
after:
image

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes
  • I included a video for animation/interaction changes

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.updated events.

Overview
Fixes Claude token-usage normalization so accumulated result.usage totals are no longer clamped to the model context window unless a context-accurate used_tokens/last_used_tokens field is present.

normalizeClaudeTokenUsage now prefers direct context-usage fields (and only then clamps and includes maxTokens), while result-only snapshots keep oversized usedTokens totals (optionally surfaced as totalProcessedTokens when 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

  • normalizeClaudeTokenUsage now distinguishes between context-accurate usage (from direct usedTokens/lastUsedTokens fields) and accumulated totals from result.usage, which can exceed the context window.
  • When context-accurate usage is available, usedTokens is clamped to contextWindow and maxTokens is included in the snapshot. When only accumulated totals exist, they are preserved unclamped and maxTokens is omitted.
  • In ClaudeAdapter.ts, the call to normalizeClaudeTokenUsage for accumulated result.usage no longer passes a contextWindow, so those totals are never clamped.
  • Behavioral Change: thread.token-usage.updated events for accumulated-only usage will no longer include maxTokens, and usedTokens may exceed the context window size.

Macroscope summarized 8a1013a.

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>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 6, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 72468eb2-5dc9-485c-8e56-2104ef3b8ab0

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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.

@github-actions github-actions Bot added size:M 30-99 changed lines (additions + deletions). vouch:unvouched PR author is not yet trusted in the VOUCHED list. labels May 6, 2026
Comment thread apps/server/src/provider/Layers/ClaudeAdapter.ts Outdated
@macroscopeapp
Copy link
Copy Markdown
Contributor

macroscopeapp Bot commented May 6, 2026

Approvability

Verdict: 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.

@amitbet amitbet changed the title Fix Claude context usage normalization for UI meter Fix Claude context usage normalization for UI meter, Fixes #2034 May 6, 2026
Copy link
Copy Markdown
Contributor

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ 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 } : {}),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 8a1013a. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M 30-99 changed lines (additions + deletions). vouch:unvouched PR author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants