Skip to content
Source
Sign inSign up
pingdotgg/t3code

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

Closedamitbet(GitHub profile, opens in a new tab)wants to merge 3 commits intomain fromrefs/pull/2551/headopened View on GitHub

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](https://cursor.com/bugbot) for commit 8a1013a9f83414ca5f6a661b23966fe25c11cb5f. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).

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.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      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.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    3. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      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](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    4. amitbet(GitHub profile, opens in a new tab) changed the title

    5. 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](https://www.cursor.com/dashboard/bugbot). Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 8a1013a9f83414ca5f6a661b23966fe25c11cb5f. Configure [here](https://www.cursor.com/dashboard/bugbot).
      @@ -348,10 +364,17 @@
      348- const usedTokens =
      349- maxTokens !== undefined ? Math.min(totalProcessedTokens, maxTokens) : totalProcessedTokens;
      364+ const hasDirectContextUsedTokens = directUsedTokens !== undefined && directUsedTokens > 0;
      365+ const usedTokens = (() => {
      366+ if (hasDirectContextUsedTokens) {
      367+ return maxTokens !== undefined ? Math.min(directUsedTokens, maxTokens) : directUsedTokens;
      368+ }
      369+ return totalProcessedTokens ?? 0;
      370+ })();
      350371
      351372 return {
      352373 usedTokens,
      353374 lastUsedTokens: usedTokens,
      354- ...(totalProcessedTokens > usedTokens ? { totalProcessedTokens } : {}),
      375+ ...(totalProcessedTokens !== undefined && totalProcessedTokens > usedTokens
      376+ ? { totalProcessedTokens }
      377+ : {}),
      355378 ...(inputTokens > 0 ? { inputTokens } : {}),
      356379 ...(outputTokens > 0 ? { outputTokens } : {}),
      357- ...(maxTokens !== undefined ? { maxTokens } : {}),
      380+ ...(maxTokens !== undefined && hasDirectContextUsedTokens ? { maxTokens } : {}),
      cursor[bot](GitHub profile, opens in a new tab)commented

      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](https://cursor.com/bugbot) for commit 8a1013a9f83414ca5f6a661b23966fe25c11cb5f. Configure [here](https://www.cursor.com/dashboard/bugbot).
    6. Closing as superseded by the newer Claude usage normalization in #3027.

    7. juliusmarminge closed this

Sign in to join this conversation.