You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched existing requests and this feature hasn't been requested yet
This is a single feature request (not multiple features)
Problem Statement
Currently, hooks receive session metadata (session_id, transcript_path, cwd, permission_mode) but NO token usage or cost information. This limitation prevents:
Real-time budget monitoring: Cannot trigger warnings when approaching token limits
Context-aware decisions: Cannot adapt behavior based on current token consumption
Automated session management: Cannot implement intelligent save/compact strategies based on cost
Integration with external systems: Cannot automatically log usage metrics to monitoring tools
Users must manually run /cost or /usage commands, or parse transcript JSONL files post-session, neither of which supports real-time automation.
Use Case: my tool for context monitoring
I'm building a tool (semantic memory system for engineering context) that needs to monitor Claude Code's 200k token window. A PostToolUse hook could automatically:
#!/bin/bash# PostToolUse hook for my tool context monitoring
input=$(cat)
current_tokens=$(echo "$input"| jq -r '.usage.total_tokens')
session_cost=$(echo "$input"| jq -r '.usage.total_cost_usd')# Alert if approaching context limit (80% threshold)if [ "$current_tokens"-gt 160000 ];thenecho'{"type": "warning", "message": "Context usage at 80% ('"$current_tokens"'/200k tokens). Consider /compact or saving."}'fi
Log to external monitoring system
curl -s -X POST http://localhost:8081/context/monitor \
-H 'Content-Type: application/json' \
-d "{\"current_tokens\": $current_tokens, \"cost\": $session_cost}"
Currently impossible because hooks don't receive token/cost data.
Extending hook inputs to include this data should be straightforward
All hook types would benefit, but PostToolUse and PreCompact are highest priority
Benefits
Proactive context management: Hooks can trigger compaction before hitting limits
Budget enforcement: Organizations can implement hard spending caps
Usage analytics: Stream metrics to external monitoring/observability platforms
Intelligent session management: Auto-save expensive sessions to memory systems
User experience: Automated warnings prevent surprise context window errors
Alternative Workarounds (Why They're Insufficient)
Parse transcript JSONL: Requires file I/O, complex parsing, only works post-session
Status line API: Display-only, not accessible to hooks
Manual /cost command: Requires user intervention, not automatable in hooks
Community Interest
Multiple issues (#8861, #10593, #7111) demonstrate strong demand for better token visibility. Exposing this data to hooks would enable the community to build powerful automation on top of Claude Code.
Preflight Checklist
Problem Statement
Currently, hooks receive session metadata (session_id, transcript_path, cwd, permission_mode) but NO token usage or cost information. This limitation prevents:
Users must manually run
/costor/usagecommands, or parse transcript JSONL files post-session, neither of which supports real-time automation.Use Case: my tool for context monitoring
I'm building a tool (semantic memory system for engineering context) that needs to monitor Claude Code's 200k token window. A
PostToolUsehook could automatically:Currently impossible because hooks don't receive token/cost data.
Related Issues
None of these specifically address exposing token data TO hooks as input.
Proposed Solution
Add token usage and cost fields to the JSON input passed to hooks via stdin:
{ "session_id": "abc123", "transcript_path": "/path/to/transcript.jsonl", "cwd": "/project/path", "permission_mode": "ask", "hook_event_name": "PostToolUse", "usage": { "total_tokens": 45230, "input_tokens": 38000, "output_tokens": 7230, "cache_read_tokens": 12000, "cache_creation_tokens": 5000, "total_cost_usd": 0.87, "percentage_of_context_window": 22.6 }, "limits": { "context_window_tokens": 200000, "session_remaining_tokens": 1250000, "weekly_remaining_tokens": 8500000 } }Implementation Notes
/costand/usage)PostToolUseandPreCompactare highest priorityBenefits
Alternative Workarounds (Why They're Insufficient)
Community Interest
Multiple issues (#8861, #10593, #7111) demonstrate strong demand for better token visibility. Exposing this data to hooks would enable the community to build powerful automation on top of Claude Code.
Alternative Solutions
No response
Priority
Medium - Would be very helpful
Feature Category
CLI commands and flags
Use Case Example
No response
Additional Context
No response