claude-code - 💡(How to fix) Fix Feature: Expose token counts and cost in session/hook lifecycle [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#50863Fetched 2026-04-20 12:10:57
View on GitHub
Comments
2
Participants
3
Timeline
6
Reactions
0
Timeline (top)
labeled ×3commented ×2cross-referenced ×1

Plugins and hooks currently have no way to know how many tokens a session consumed or what it cost. This blocks building session analytics, cost dashboards, and quota projection tools.

Root Cause

Plugins and hooks currently have no way to know how many tokens a session consumed or what it cost. This blocks building session analytics, cost dashboards, and quota projection tools.

Code Example

{
  "session_id": "...",
  "tokens_in": 24000,
  "tokens_out": 8000,
  "cost_usd": 0.35,
  "duration_seconds": 720,
  "turn_count": 8
}
RAW_BUFFERClick to expand / collapse

Summary

Plugins and hooks currently have no way to know how many tokens a session consumed or what it cost. This blocks building session analytics, cost dashboards, and quota projection tools.

Use Case

I'm building sensei — an AI-assisted development companion with a desktop observatory that shows developers their session efficiency (FTR, turns, rework rate, token burn rate). The missing piece is token/cost data.

What I want to show:

  • "This session used 24k tokens in / 8k out, cost $0.35"
  • "At current burn rate, ~3 days of quota remaining"
  • "This mindset adds 12% more tokens but improves quality by 22% — worth it"

What I have today: Session outcomes, turn counts, tool usage — everything except tokens and cost.

Proposed Solutions (any of these would unblock)

Option A: SessionEnd hook (preferred)

A new hook event SessionEnd that fires when a session closes, with payload:

{
  "session_id": "...",
  "tokens_in": 24000,
  "tokens_out": 8000,
  "cost_usd": 0.35,
  "duration_seconds": 720,
  "turn_count": 8
}

Option B: Enrich existing hooks

Add tokens_in, tokens_out fields to the PostToolUse hook payload (cumulative for the session so far).

Option C: Session metadata API

A CLI command or API endpoint: claude session stats that returns token/cost data for the current or most recent session.

Impact

Without token data, any cost-awareness feature in plugins is impossible. This affects:

  • Session cost tracking
  • Quota burn rate projection
  • Cost-per-feature analysis
  • A/B comparison of different prompt strategies
  • Identifying expensive vs efficient approaches

Environment

  • Claude Code CLI
  • macOS / Linux
  • Building a Tauri desktop companion app that queries the daemon

extent analysis

TL;DR

Implementing a SessionEnd hook with token and cost data or enriching existing hooks with this information can unblock the development of session analytics and cost dashboards.

Guidance

  • To address the issue, consider implementing one of the proposed solutions: SessionEnd hook, enriching existing hooks, or creating a session metadata API.
  • Evaluate the trade-offs between these options, such as the complexity of implementation and the frequency of data updates required for your use case.
  • When choosing an approach, consider the specific requirements of your application, including the need for real-time data updates and the potential impact on system performance.
  • Ensure that the chosen solution provides the necessary data, including tokens_in, tokens_out, and cost_usd, to support the desired analytics and cost tracking features.

Example

{
  "session_id": "...",
  "tokens_in": 24000,
  "tokens_out": 8000,
  "cost_usd": 0.35,
  "duration_seconds": 720,
  "turn_count": 8
}

This example payload illustrates the data that could be provided by a SessionEnd hook to support session analytics and cost tracking.

Notes

The choice of solution will depend on the specific requirements of your application and the constraints of the Claude Code CLI and daemon architecture. It is essential to evaluate the potential impact on system performance and the complexity of implementation when selecting an approach.

Recommendation

Apply a workaround by implementing the SessionEnd hook, as it is the preferred option and provides a clear and straightforward solution to the problem. This approach allows for the collection of necessary data for session analytics and cost tracking without modifying existing hooks or introducing a new API endpoint.

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