Expose token counts to hooks and session context
Resolved 💬 3 comments Opened Apr 29, 2026 by Apethor Closed Jun 27, 2026
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:
- Hook context variables — Make token counts available to hooks (e.g., before/after prompt submission) so they can append usage info to future prompts
- Session environment variables — Track cumulative token usage across a session
- Updated after each API call
- Accessible in subsequent hooks and commands
- 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.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗