Skip to content

fix(statusline): use current_usage instead of total_input_tokens for context display#923

Open
AndriiBarabash wants to merge 5 commits intoryoppippi:mainfrom
AndriiBarabash:fix/statusline-context-window-tokens
Open

fix(statusline): use current_usage instead of total_input_tokens for context display#923
AndriiBarabash wants to merge 5 commits intoryoppippi:mainfrom
AndriiBarabash:fix/statusline-context-window-tokens

Conversation

@AndriiBarabash
Copy link
Copy Markdown

@AndriiBarabash AndriiBarabash commented Mar 31, 2026

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 ccusage works as expected, but then I installed it to the Claude Code status line (via ccusage 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 context
  • cache_creation_input_tokens - tokens written to cache
  • cache_read_input_tokens - tokens read from cache

This matches how Claude Code itself calculates and displays context usage, providing users with accurate real-time visibility.

Additional Changes

  • Updated statuslineHookJsonSchema to properly validate the current_usage, used_percentage, and remaining_percentage fields from Claude Code's statusline hook
  • Made these fields nullable to handle fresh sessions where no API calls have been made yet (current_usage is null)
  • When current_usage is unavailable but context window size is known, displays 0 (0%) instead of falling back to transcript calculation or showing "N/A"

Testing

Verified that statusline output now matches Claude Code's context display:

  • Before:
Screenshot 2026-03-31 at 23 08 32
  • After:
Screenshot 2026-03-31 at 23 07 44

Summary by CodeRabbit

  • Bug Fixes
    • Status display now prefers explicit current-usage metrics when available, summing input and cached tokens for more accurate context counts. If transcript-based estimation fails but a context window size is present, context tokens are shown as 0 instead of undefined.
  • New Features
    • Added optional current-usage, used-percentage, and remaining-percentage fields to improve status reporting fidelity and clarity.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 31, 2026

📝 Walkthrough

Walkthrough

Statusline now prefers hookData.context_window.current_usage for context-token data; inputTokens is computed as the sum of current_usage.input_tokens, current_usage.cache_creation_input_tokens, and current_usage.cache_read_input_tokens (defaulting missing parts to 0). If transcript-based context calculation returns null but context_window_size exists, context is shown as 0 tokens.

Changes

Cohort / File(s) Summary
Statusline command
apps/ccusage/src/commands/statusline.ts
Consume hookData.context_window.current_usage when present; compute inputTokens = current_usage.input_tokens + current_usage.cache_creation_input_tokens + current_usage.cache_read_input_tokens (each fallback to 0). If transcript calc is null and context_window_size exists, return formatContextInfo(0, context_window_size) instead of undefined.
Schema / types
apps/ccusage/src/_types.ts
Extend statuslineHookJsonSchema.context_window with optional `current_usage?: null

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • ryoppippi

Poem

🐰 I hop through tokens, nibble and sum,
Inputs and cache — each little crumb,
When current usage scurries by,
I count it quick and show it high,
A rabbit's pulse in the status drum.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: switching from total_input_tokens to current_usage for context display in the statusline command.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

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

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.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between 61ee04d and 12abad5.

📒 Files selected for processing (1)
  • apps/ccusage/src/commands/statusline.ts

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 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_percentage and remaining_percentage, but apps/ccusage/src/commands/statusline.ts (Line 472-481) currently ignores them and only reads current_usage. If a hook payload includes percentages without current_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

📥 Commits

Reviewing files that changed from the base of the PR and between 12abad5 and ba003f5.

📒 Files selected for processing (1)
  • apps/ccusage/src/_types.ts

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
apps/ccusage/src/commands/statusline.ts (1)

478-481: ⚠️ Potential issue | 🟠 Major

Guard current_usage token math against NaN.

If either cache field is missing, this sum becomes NaN and breaks context token/percentage display. Please default each operand to 0.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 34c5c86 and ddde8cd.

📒 Files selected for processing (2)
  • apps/ccusage/src/_types.ts
  • apps/ccusage/src/commands/statusline.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/ccusage/src/_types.ts

@AndriiBarabash
Copy link
Copy Markdown
Author

@ryoppippi the PR is ready for review

@AndriiBarabash
Copy link
Copy Markdown
Author

@coderabbitai resume

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 31, 2026

✅ Actions performed

Reviews resumed.

@AndriiBarabash
Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 31, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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.

2 participants