claude-code - 💡(How to fix) Fix Add token usage and cost fields to `Stop` and `UserPromptSubmit` hook events [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#50883Fetched 2026-04-20 12:10:25
View on GitHub
Comments
2
Participants
3
Timeline
5
Reactions
0
Author
Timeline (top)
labeled ×3commented ×2

Root Cause

  • Users on pay-per-use APIs (e.g., AWS Bedrock) have no programmatic way to track per-prompt costs
  • The data clearly exists in memory (accessible via /cost) but is not exposed to hooks
  • This would enable simple hook scripts to log, display, or alert on per-prompt spend

Code Example

{
  "session_id": "abc123",
  "hook_event_name": "Stop",
  "stop_reason": "end_turn",
  "token_usage": {
    "input_tokens": 1500,
    "output_tokens": 800,
    "cache_read_input_tokens": 12000,
    "cache_creation_input_tokens": 3000
  },
  "cost_usd": 0.042,
  "cumulative_cost_usd": 0.58
}
RAW_BUFFERClick to expand / collapse

Feature Request

Currently, the Stop and UserPromptSubmit hook events do not include token usage or cost data. The only hook that receives cost information is SessionEnd, which fires once at session termination.

Use Case

I'd like to calculate per-prompt cost by capturing cost before (UserPromptSubmit) and after (Stop) each prompt and reporting the delta. This would give users real-time cost visibility per interaction without needing to manually run /cost.

Proposed Change

Add token usage and cost fields to the Stop hook input:

{
  "session_id": "abc123",
  "hook_event_name": "Stop",
  "stop_reason": "end_turn",
  "token_usage": {
    "input_tokens": 1500,
    "output_tokens": 800,
    "cache_read_input_tokens": 12000,
    "cache_creation_input_tokens": 3000
  },
  "cost_usd": 0.042,
  "cumulative_cost_usd": 0.58
}

A cumulative cost field in UserPromptSubmit would also allow calculating the delta.

Why This Matters

  • Users on pay-per-use APIs (e.g., AWS Bedrock) have no programmatic way to track per-prompt costs
  • The data clearly exists in memory (accessible via /cost) but is not exposed to hooks
  • This would enable simple hook scripts to log, display, or alert on per-prompt spend

extent analysis

TL;DR

Adding token usage and cost fields to the Stop and UserPromptSubmit hook events is likely the most effective way to provide real-time cost visibility per interaction.

Guidance

  • To calculate per-prompt cost, consider modifying the Stop hook to include token usage and cost data, as proposed in the issue.
  • Adding a cumulative cost field to UserPromptSubmit would allow for calculating the delta in cost between prompts.
  • Review the existing /cost endpoint to understand how cost data is currently calculated and exposed, as this may inform the implementation of cost tracking in hooks.
  • Consider the potential impact of adding cost data to hooks on the overall system performance and user experience.

Example

No explicit code example is provided, but the proposed JSON structure for the Stop hook input suggests how token usage and cost data could be formatted:

{
  "session_id": "abc123",
  "hook_event_name": "Stop",
  "stop_reason": "end_turn",
  "token_usage": {
    "input_tokens": 1500,
    "output_tokens": 800,
    "cache_read_input_tokens": 12000,
    "cache_creation_input_tokens": 3000
  },
  "cost_usd": 0.042,
  "cumulative_cost_usd": 0.58
}

Notes

The implementation details of adding cost data to hooks are not specified in the issue, so the exact steps and potential challenges are unclear.

Recommendation

Apply workaround: Modify the Stop and UserPromptSubmit hook events to include token usage and cost data, as this would provide the necessary information for calculating per-prompt costs without requiring significant changes to the existing system.

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