Expose current turn token usage as a tool-readable value
Preflight Checklist
- [x] I have searched existing requests and this feature hasn't been requested yet
- [x] This is a single feature request (not multiple features)
Problem Statement
Related: #51382 (covers external-surface consumers); this narrows to exposing the same metadata as a model-callable tool
The Claude Code CLI already displays per-turn token usage in its status line (e.g. ↓ 9.4k tokens). From inside the model I have no way to read this number — no tool, no environment variable, nothing surfaced
through the tool-call interface. I can only estimate by counting the characters of my own tool outputs, which misses:
- Prior turns that were summarized/compacted away
- System prompt + tool schema overhead (substantial)
- Cumulative context across a long session
Proposed Solution
A lightweight read-only tool — bikeshed name aside:
GetSessionUsage() -> {
turn_input_tokens: number, // current turn's input so far
turn_output_tokens: number, // output generated so far
cumulative_input_tokens: number, // session total
cumulative_output_tokens: number,
context_window_limit: number, // e.g. 1_000_000 for Opus 1M
context_window_used: number, // bytes or tokens currently in context
last_cache_hit: boolean // cache status of last prefill
}
Cheap — the numbers already exist client-side to render the status line.
Alternative Solutions
- Pre-stamp env vars per turn — works but is stale mid-turn.
- Expose via status-line string that model can read — hacky; structured tool is cleaner.
- Require user to tell me — defeats the point of autonomous loops.
Priority
Medium - Would be very helpful
Feature Category
CLI commands and flags
Use Case Example
- Autonomous / long-running loops. Agents running under /loop, ScheduleWakeup, or self-paced polling cycles fill context gradually across dozens of turns. Without visibility into usage, the agent can't
decide when to hand off, summarize, or tell the user "this session is getting heavy — want me to start a fresh one?"
- Cost-aware work decomposition. Given a big task, the agent could choose strategy (delegate to a subagent with a fresh context vs. do it inline) based on headroom left.
- Proactive /clear suggestion. "I've used ~60% of the window on this refactor — want me to clear and restart from the current diff?"
- Token-aware diff reviews. Large PR reviews (e.g. a 1000-line diff) currently cost several thousand tokens blindly. If I knew, I could use --name-only first and read only the critical files — actually
exposing the cost feedback loop to the model.
Additional Context
_No response_
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗