claude-code - 💡(How to fix) Fix Expose token counts to hooks and session context [1 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#54673Fetched 2026-04-30 06:39:10
View on GitHub
Comments
0
Participants
1
Timeline
3
Reactions
0
Author
Participants
Timeline (top)
labeled ×3

Root Cause

  • Cost transparency — Users want to see what their workflows are costing
  • Context management — Claude can give better reasoning if aware of remaining tokens
  • Debugging — Easier to identify unexpectedly expensive operations
  • Hooks value — Most powerful when they have access to system state; token counts are core to Claude Code's execution model

Fix Action

Fix / Workaround

Current Workaround

None — token data is not exposed outside the internal API layer.

Code Example

Before submitting, append: "(Using {CLAUDE_TOKENS_INPUT} input tokens, {CLAUDE_TOKENS_OUTPUT} output tokens so far)"
RAW_BUFFERClick to expand / collapse

Feature Request: Token Count Visibility in Hooks and Sessions

Use Case

Users want to:

  • Append token count information to prompts dynamically via hooks
  • Track token usage (input, output, cache reads/writes) within a session
  • Make Claude more aware of remaining context budget to improve reasoning

Currently, token counts are only available in Claude API response metadata and are not surfaced to the Claude Code hook execution context or environment.

Proposed Solution

Expose token counts through one or more of:

  1. Hook context variables — Make token counts available to hooks (e.g., before/after prompt submission) so they can append usage info to future prompts

  2. Session environment variables — Track cumulative token usage across a session

    • Updated after each API call
    • Accessible in subsequent hooks and commands
  3. Hook timing point — Add a post-response hook that fires after API calls complete, with token data in context

Why This Matters

  • Cost transparency — Users want to see what their workflows are costing
  • Context management — Claude can give better reasoning if aware of remaining tokens
  • Debugging — Easier to identify unexpectedly expensive operations
  • Hooks value — Most powerful when they have access to system state; token counts are core to Claude Code's execution model

Example Usage

A user could write a hook that appends to every prompt:

Before submitting, append: "(Using {CLAUDE_TOKENS_INPUT} input tokens, {CLAUDE_TOKENS_OUTPUT} output tokens so far)"

This would help Claude optimize responses given actual budget constraints, not guesses.

Current Workaround

None — token data is not exposed outside the internal API layer.

extent analysis

TL;DR

Expose token counts through hook context variables or session environment variables to enable dynamic tracking and optimization of token usage in Claude Code.

Guidance

  • Consider adding token count information to hook context variables, allowing hooks to access and append usage data to future prompts.
  • Evaluate the feasibility of tracking cumulative token usage across a session through updated session environment variables.
  • Investigate the potential of introducing a post-response hook with token data in context to provide more granular control over token management.
  • Assess the trade-offs between exposing token counts through hook context variables versus session environment variables, considering factors like performance and complexity.

Example

# Example hook code to append token usage information to a prompt
def append_token_info(prompt, context):
    input_tokens = context.get('CLAUDE_TOKENS_INPUT', 0)
    output_tokens = context.get('CLAUDE_TOKENS_OUTPUT', 0)
    prompt += f" (Using {input_tokens} input tokens, {output_tokens} output tokens so far)"
    return prompt

Notes

The proposed solution requires careful consideration of the trade-offs between different approaches to exposing token counts, as well as potential performance implications.

Recommendation

Apply a workaround by exposing token counts through hook context variables, as this approach seems to offer a good balance between flexibility and complexity.

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 Expose token counts to hooks and session context [1 participants]