claude-code - 💡(How to fix) Fix Expose session token usage to hooks (UserPromptSubmit additionalContext, env var, or hook payload field) [1 comments, 2 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#55506Fetched 2026-05-03 04:51:35
View on GitHub
Comments
1
Participants
2
Timeline
6
Reactions
0
Timeline (top)
labeled ×5commented ×1

Fix Action

Fix / Workaround

Workaround we ended up with

Hooks already run on every prompt submission, so a payload field has zero extra cost. A CLI flag still requires shelling out and parsing, and /usage being interactive-only means today's workaround has to read transcript jsonl directly.

RAW_BUFFERClick to expand / collapse

Problem

There is no first-class way for Claude (the model) to be aware of the session's token usage during a long-running conversation. /usage and /cost are interactive-only and not machine-readable. Hooks don't receive token usage in their event payload, and no env var or file is documented as carrying live session token counts.

This matters for long sessions where I want Claude to factor token spend into decisions ("we're at 600k, wrap up the investigation rather than spawning another subagent"). Today the only path is for the user to manually run /cost and paste it in.

With a functionality like this, we can ask Claude to estimate a token budget, then push it to try and stay within that budget, stop working if it thinks it needs to blow through the budget, etc. like we would with any employee / service provider etc.

Workaround we ended up with

A UserPromptSubmit hook that parses the transcript jsonl pointed to by transcript_path in the hook event, sums the usage records (input, output, cache_read, cache_creation), and emits the totals as hookSpecificOutput.additionalContext. This works but:

  • It's parsing an undocumented internal file format that could change without notice.
  • Every user has to write the same script.
  • It double-counts if multiple model providers/iterations are interleaved oddly, and is fragile across transcript schema changes.

Requests (any one would solve this)

  1. Best: include a token_usage field in the UserPromptSubmit (and ideally Stop / PreToolUse) hook event payload, with cumulative session totals (input, output, cache_read, cache_creation, plus cost if available).
  2. Good: a documented env var (e.g. CLAUDE_SESSION_INPUT_TOKENS, CLAUDE_SESSION_OUTPUT_TOKENS) the harness exports into hook subprocesses.
  3. Acceptable: a --json flag on /usage or /cost that emits machine-readable output, callable from a hook subprocess.

Why a hook field beats a CLI flag

Hooks already run on every prompt submission, so a payload field has zero extra cost. A CLI flag still requires shelling out and parsing, and /usage being interactive-only means today's workaround has to read transcript jsonl directly.

Environment

  • Claude Code (VSCode extension)
  • macOS Darwin 25.2.0
  • Model: Opus 4.7 (1M)

Filed at user request via Claude Code session.

extent analysis

TL;DR

The most likely fix is to include a token_usage field in the UserPromptSubmit hook event payload to provide Claude with session token usage information.

Guidance

  • Consider implementing a token_usage field in the UserPromptSubmit hook event payload to provide cumulative session totals for input, output, cache_read, cache_creation, and cost.
  • Alternatively, explore using a documented env var, such as CLAUDE_SESSION_INPUT_TOKENS, to export token usage information into hook subprocesses.
  • If the above options are not feasible, a --json flag on /usage or /cost could provide machine-readable output, callable from a hook subprocess.
  • Evaluate the trade-offs between these approaches, considering factors like performance, maintainability, and ease of implementation.

Example

No code snippet is provided as the issue does not contain sufficient technical details to generate a specific example.

Notes

The current workaround has limitations, including parsing an undocumented internal file format and potential double-counting issues. A more robust solution, such as including a token_usage field in the hook event payload, would address these concerns.

Recommendation

Apply a workaround, such as implementing a token_usage field in the UserPromptSubmit hook event payload, as it provides a more direct and efficient solution to the problem, allowing Claude to factor token spend into decisions during long-running conversations.

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