Skip to content

fix(claude): use used_percentage instead of cumulative total tokens#7287

Closed
ahmetb wants to merge 1 commit intoJanDeDobbeleer:mainfrom
ahmetb:fix/claude-token-usage-percent
Closed

fix(claude): use used_percentage instead of cumulative total tokens#7287
ahmetb wants to merge 1 commit intoJanDeDobbeleer:mainfrom
ahmetb:fix/claude-token-usage-percent

Conversation

@ahmetb
Copy link
Copy Markdown

@ahmetb ahmetb commented Feb 8, 2026

Prerequisites

  • I have read and understood the contributing guide.
  • The commit message follows the conventional commits guidelines.
  • Tests for the changes have been added (for bug fixes / features).
  • Docs have been added/updated (for bug fixes / features).

Description

TokenUsagePercent() had a fallback path that calculated context window usage from TotalInputTokens + TotalOutputTokens. These are cumulative session totals that never reset on /clear or compact. In long sessions (e.g. 3.7M+ input tokens), this caused the percentage to always show 100% even though actual context usage was only ~20%.

FormattedTokens() had the same fallback and would display inflated cumulative token counts instead of the actual current context size.

This PR removes the fallback to cumulative total tokens from both functions. They now:

  1. Use used_percentage / CurrentUsage from Claude's statusline JSON (correctly resets on /clear and compact)
  2. Return 0 if neither is available

Cumulative session totals are still accessible via .ContextWindow.TotalInputTokens / .TotalOutputTokens for users who want them in custom templates.

Fixes #7286

TokenUsagePercent() had a fallback that calculated context usage from
TotalInputTokens + TotalOutputTokens. These are cumulative session
totals that never reset on /clear or compact, causing the percentage
to always show 100% in long sessions.

Remove the fallback to cumulative totals. The function now uses
UsedPercentage from Claude's statusline JSON (which correctly resets
on /clear), with a fallback to CurrentUsage tokens.

Fixes JanDeDobbeleer#7286

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings February 8, 2026 04:30
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes the Claude segment’s context-window percentage calculation so long-running sessions don’t incorrectly show 100% usage by avoiding cumulative session totals and preferring Claude’s used_percentage (or current usage).

Changes:

  • Updates TokenUsagePercent() to use context_window.used_percentage when available and otherwise compute from CurrentUsage only.
  • Removes the previous fallback path that used cumulative TotalInputTokens + TotalOutputTokens for percentage calculation.
  • Updates Claude segment tests to reflect the new fallback behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/segments/claude.go Adjusts token usage percentage logic and removes cumulative-total fallback logic (also affects FormattedTokens() behavior).
src/segments/claude_test.go Updates unit tests to match the new token usage percent behavior and the updated FormattedTokens() expectations.
Comments suppressed due to low confidence (1)

src/segments/claude.go:159

  • PR description says FormattedTokens() should retain a fallback to cumulative TotalInputTokens + TotalOutputTokens, but this change removes that fallback entirely (and returns 0 when CurrentUsage is nil/zero). Please either reintroduce the fallback in FormattedTokens() or update the PR description to match the new behavior (and consider whether this is a breaking change for users relying on cumulative totals).
// FormattedTokens returns a human-readable string of current context tokens.
// Uses CurrentUsage (which represents actual context and resets on compact/clear).
func (c *Claude) FormattedTokens() string {
	var currentTokens int

	if c.ContextWindow.CurrentUsage != nil {
		currentTokens = c.ContextWindow.CurrentUsage.InputTokens +
			c.ContextWindow.CurrentUsage.CacheCreationInputTokens +
			c.ContextWindow.CurrentUsage.CacheReadInputTokens
	}

	if currentTokens < int(thousand) {
		return fmt.Sprintf("%d", currentTokens)
	}

	if currentTokens < int(million) {
		return fmt.Sprintf("%.1fK", float64(currentTokens)/thousand)
	}

	return fmt.Sprintf("%.1fM", float64(currentTokens)/million)

Comment on lines 271 to 318
@@ -339,19 +302,19 @@ func TestClaudeFormattedTokens(t *testing.T) {
ExpectedFormat: "500",
},
{
Case: "Fallback to total when CurrentUsage is zero",
Case: "No fallback to total when CurrentUsage is zero",
HasCurrentUsage: true,
InputTokens: 50000,
OutputTokens: 25000,
CurrentInput: 0,
ExpectedFormat: "75.0K", // Should fallback to total
ExpectedFormat: "0", // Should NOT fallback to cumulative total tokens
},
{
Case: "Nil CurrentUsage falls back to total",
Case: "Nil CurrentUsage returns 0",
HasCurrentUsage: false,
InputTokens: 50000,
OutputTokens: 25000,
ExpectedFormat: "75.0K", // Should fallback to total
ExpectedFormat: "0", // Should NOT fallback to cumulative total tokens
},
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

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

These test updates assert FormattedTokens() returns "0" when CurrentUsage is nil/zero and no longer validate the cumulative total fallback. This conflicts with the PR description which states FormattedTokens() should keep falling back to TotalInputTokens + TotalOutputTokens. Align the tests with the intended behavior (either restore fallback expectations or update the PR description if the behavior change is intentional).

Copilot uses AI. Check for mistakes.
@ahmetb
Copy link
Copy Markdown
Author

ahmetb commented Feb 8, 2026

@JanDeDobbeleer please take a look, assuming you have a Claude setup probably easier to verify on your side as well.

@JanDeDobbeleer
Copy link
Copy Markdown
Owner

@ahmetb are you sure this wasn't already fixed on latest?

@ahmetb
Copy link
Copy Markdown
Author

ahmetb commented Feb 8, 2026

Indeed, my bad for not testing HEAD.

@ahmetb ahmetb closed this Feb 8, 2026
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.

claude: Incorrectly calculates TokenUsagePercent

3 participants