fix(claude): use used_percentage instead of cumulative total tokens#7287
fix(claude): use used_percentage instead of cumulative total tokens#7287ahmetb wants to merge 1 commit intoJanDeDobbeleer:mainfrom
Conversation
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>
There was a problem hiding this comment.
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 usecontext_window.used_percentagewhen available and otherwise compute fromCurrentUsageonly. - Removes the previous fallback path that used cumulative
TotalInputTokens + TotalOutputTokensfor 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 cumulativeTotalInputTokens + TotalOutputTokens, but this change removes that fallback entirely (and returns 0 whenCurrentUsageis nil/zero). Please either reintroduce the fallback inFormattedTokens()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)
| @@ -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 | |||
| }, | |||
There was a problem hiding this comment.
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).
|
@JanDeDobbeleer please take a look, assuming you have a Claude setup probably easier to verify on your side as well. |
|
@ahmetb are you sure this wasn't already fixed on latest? |
|
Indeed, my bad for not testing HEAD. |
Prerequisites
Description
TokenUsagePercent()had a fallback path that calculated context window usage fromTotalInputTokens + TotalOutputTokens. These are cumulative session totals that never reset on/clearor 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:
used_percentage/CurrentUsagefrom Claude's statusline JSON (correctly resets on/clearand compact)Cumulative session totals are still accessible via
.ContextWindow.TotalInputTokens/.TotalOutputTokensfor users who want them in custom templates.Fixes #7286