claude-code - 💡(How to fix) Fix Feature: expose session token usage to hooks and statusline commands [2 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#52089Fetched 2026-04-23 07:36:52
View on GitHub
Comments
2
Participants
3
Timeline
6
Reactions
0
Author
Timeline (top)
labeled ×4commented ×2

Code Example

{
     "session_id": "abc123",
     "usage": {
       "input_tokens": 45231,
       "output_tokens": 8120,
       "cache_read_tokens": 12000,
       "cache_write_tokens": 3000
     }
   }

---

{
     "session_id": "abc123",
     "input_tokens": 45231,
     "output_tokens": 8120,
     "total_tokens": 53351,
     "updated_at": "2026-04-22T10:30:00Z"
   }
RAW_BUFFERClick to expand / collapse

Feature Request

Problem

The statusline command (settings.json → statusLine.command) runs an external shell script, but there is no way to display cumulative session token usage (input + output tokens) in that badge. Token counts are rendered internally by Claude Code and not written to any accessible file or passed to hooks via stdin.

Similarly, hook events (PreToolUse, PostToolUse, UserPromptSubmit, Stop) receive session context but no token usage data.

Requested Change

Expose per-session token usage to:

  1. Hooks — include a usage object in the stdin JSON payload for Stop (and optionally PostToolUse):

    {
      "session_id": "abc123",
      "usage": {
        "input_tokens": 45231,
        "output_tokens": 8120,
        "cache_read_tokens": 12000,
        "cache_write_tokens": 3000
      }
    }
  2. Statusline — write a small JSON file to $CLAUDE_CONFIG_DIR/.session-usage.json (or similar) after each response turn, so statusline scripts can read it:

    {
      "session_id": "abc123",
      "input_tokens": 45231,
      "output_tokens": 8120,
      "total_tokens": 53351,
      "updated_at": "2026-04-22T10:30:00Z"
    }

Use Case

Plugin authors and power users want to show live token spend in the statusline — e.g. a badge like [CAVEMAN] 53k tok — to monitor costs and context usage without switching away from the terminal.

Alternatives Considered

  • Scraping CLI output — not reliable
  • Estimating tokens client-side — inaccurate
  • Neither Stop hook stdin nor any disk file currently contains this data

Environment

  • Claude Code CLI (macOS)
  • Custom statusline plugin using statusLine.type: "command"

extent analysis

TL;DR

To display cumulative session token usage in the statusline, Claude Code should expose token usage data to hooks and write it to a JSON file for statusline scripts to read.

Guidance

  • The current implementation does not provide a way to access token usage data, so a change to the Stop hook and statusline command is necessary.
  • To verify the fix, check that the usage object is included in the stdin JSON payload for the Stop hook and that the JSON file is written to the specified directory.
  • Plugin authors and power users can then use this data to display live token spend in the statusline.
  • The proposed JSON file format includes session_id, input_tokens, output_tokens, total_tokens, and updated_at fields, which can be used to calculate and display the total token usage.

Example

{
  "session_id": "abc123",
  "usage": {
    "input_tokens": 45231,
    "output_tokens": 8120,
    "cache_read_tokens": 12000,
    "cache_write_tokens": 3000
  }
}

Notes

The current alternatives, such as scraping CLI output or estimating tokens client-side, are not reliable or accurate, so a direct solution like exposing token usage data is necessary.

Recommendation

Apply workaround by modifying the Stop hook and statusline command to include token usage data, as this will provide the necessary information for plugin authors and power users to display live token spend in the statusline.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix Feature: expose session token usage to hooks and statusline commands [2 comments, 3 participants]