fix(statusline): use current_usage instead of total_input_tokens for context display#923
Conversation
📝 WalkthroughWalkthroughStatusline now prefers Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@apps/ccusage/src/commands/statusline.ts`:
- Around line 475-479: The branch that reads
hookData.context_window.current_usage is unreachable because
statuslineHookJsonSchema lacks current_usage and summing unspecified token
fields can yield NaN; update the schema (statuslineHookJsonSchema) to add an
optional context_window.current_usage object with numeric fields input_tokens,
cache_creation_input_tokens, and cache_read_input_tokens (all optional but
numeric), and then change the arithmetic in statusline.ts where you build the
Result (the block that references hookData.context_window.current_usage) to use
nullish coalescing on each token field (e.g., input_tokens ?? 0,
cache_creation_input_tokens ?? 0, cache_read_input_tokens ?? 0) so the sum never
becomes NaN and the branch is actually reachable when validated JSON contains
current_usage.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 68afcd1d-d8ef-416d-90dd-7417b347991b
📒 Files selected for processing (1)
apps/ccusage/src/commands/statusline.ts
There was a problem hiding this comment.
🧹 Nitpick comments (1)
apps/ccusage/src/_types.ts (1)
195-196: Consider removing or consuming percentage fields to avoid schema/consumer drift.Line 195-196 accepts
used_percentageandremaining_percentage, butapps/ccusage/src/commands/statusline.ts(Line 472-481) currently ignores them and only readscurrent_usage. If a hook payload includes percentages withoutcurrent_usage, it still falls back to transcript-derived values and can reintroduce mismatch. Prefer either using these fields as a fallback or dropping them until supported.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@apps/ccusage/src/_types.ts` around lines 195 - 196, The schema includes optional fields used_percentage and remaining_percentage but the consumer in commands/statusline.ts only reads current_usage; either remove these two fields from the payload schema in _types.ts (delete used_percentage and remaining_percentage from the v.optional list) or update the consumer in statusline.ts to consume them as a fallback when current_usage is missing (e.g., use used_percentage/remaining_percentage to compute current_usage or to populate display values). Update the symbol references: modify the schema entries used_percentage / remaining_percentage in the type definition and/or change the logic in the statusline handler that currently only reads current_usage to accept and correctly map percentage fields as fallback.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@apps/ccusage/src/_types.ts`:
- Around line 195-196: The schema includes optional fields used_percentage and
remaining_percentage but the consumer in commands/statusline.ts only reads
current_usage; either remove these two fields from the payload schema in
_types.ts (delete used_percentage and remaining_percentage from the v.optional
list) or update the consumer in statusline.ts to consume them as a fallback when
current_usage is missing (e.g., use used_percentage/remaining_percentage to
compute current_usage or to populate display values). Update the symbol
references: modify the schema entries used_percentage / remaining_percentage in
the type definition and/or change the logic in the statusline handler that
currently only reads current_usage to accept and correctly map percentage fields
as fallback.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 19e117d8-3359-4229-b5da-94332a081830
📒 Files selected for processing (1)
apps/ccusage/src/_types.ts
There was a problem hiding this comment.
♻️ Duplicate comments (1)
apps/ccusage/src/commands/statusline.ts (1)
478-481:⚠️ Potential issue | 🟠 MajorGuard
current_usagetoken math againstNaN.If either cache field is missing, this sum becomes
NaNand breaks context token/percentage display. Please default each operand to0.Suggested fix
Result.succeed({ inputTokens: - hookData.context_window.current_usage.input_tokens + - hookData.context_window.current_usage.cache_creation_input_tokens + - hookData.context_window.current_usage.cache_read_input_tokens, + (hookData.context_window.current_usage.input_tokens ?? 0) + + (hookData.context_window.current_usage.cache_creation_input_tokens ?? 0) + + (hookData.context_window.current_usage.cache_read_input_tokens ?? 0), contextLimit: hookData.context_window.context_window_size, })🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@apps/ccusage/src/commands/statusline.ts` around lines 478 - 481, Guard the token-sum calculation in inputTokens by defaulting each operand to 0 before adding: when computing inputTokens from hookData.context_window.current_usage, ensure you coalesce current_usage.input_tokens, current_usage.cache_creation_input_tokens, and current_usage.cache_read_input_tokens to 0 if missing/undefined/NaN so the sum never becomes NaN; update the expression that sets inputTokens to use these safe defaults (referencing inputTokens and hookData.context_window.current_usage and the specific fields cache_creation_input_tokens and cache_read_input_tokens).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@apps/ccusage/src/commands/statusline.ts`:
- Around line 478-481: Guard the token-sum calculation in inputTokens by
defaulting each operand to 0 before adding: when computing inputTokens from
hookData.context_window.current_usage, ensure you coalesce
current_usage.input_tokens, current_usage.cache_creation_input_tokens, and
current_usage.cache_read_input_tokens to 0 if missing/undefined/NaN so the sum
never becomes NaN; update the expression that sets inputTokens to use these safe
defaults (referencing inputTokens and hookData.context_window.current_usage and
the specific fields cache_creation_input_tokens and cache_read_input_tokens).
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 58b6cc66-b57d-4c8d-8566-b0b9a21863b4
📒 Files selected for processing (2)
apps/ccusage/src/_types.tsapps/ccusage/src/commands/statusline.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- apps/ccusage/src/_types.ts
|
@ryoppippi the PR is ready for review |
|
@coderabbitai resume |
✅ Actions performedReviews resumed. |
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
I'm sorry to open this pull request without an attached issue to it. My organization recently switched to Claude Code and I installed your project to track my token usage. The regular
ccusageworks as expected, but then I installed it to the Claude Code status line (viaccusage statusline) and saw the incorrect token calculations near the 🧠 emoji.So here is the PR with the fix for this issue. After the fix, the tokens in statusline match the tokens with
/config -> Verbose output (set to true).Summary
This fix resolves an issue where the statusline command (
ccusage statusline) was displaying incorrect context window token counts and percentages.See the testing section for before/after.
The Problem
The statusline was reading
hookData.context_window.total_input_tokens, which represents the cumulative session input tokens rather than the actual tokens currently in the context window. This led to wildly inaccurate displays.The Solution
Now calculates actual context window usage by summing three components from
current_usage:input_tokens- current input in contextcache_creation_input_tokens- tokens written to cachecache_read_input_tokens- tokens read from cacheThis matches how Claude Code itself calculates and displays context usage, providing users with accurate real-time visibility.
Additional Changes
statuslineHookJsonSchemato properly validate thecurrent_usage,used_percentage, andremaining_percentagefields from Claude Code's statusline hookcurrent_usageisnull)current_usageis unavailable but context window size is known, displays0 (0%)instead of falling back to transcript calculation or showing "N/A"Testing
Verified that statusline output now matches Claude Code's context display:
Summary by CodeRabbit