Skip to content

[FEATURE] Surface token usage and cost in the Claude Code stream adapter #649

Closed
inference-gateway/docs
#279

Description

@edenreich

Summary

The CLI's Claude Code mode stream adapter discards token usage and cost, so any
consumer that renders run metrics from the JSON-line stream reports 0 in / 0 out / 0 total and no cost when running in Claude Code mode. The raw numbers already exist in
Claude's stream-json output; they are dropped on the floor.

In internal/infra/adapters/claude_code_client.go:

  • buildResponse() builds the response from accumulated content + tool calls only and
    never sets response.Usage.
  • the streaming createDeltaEvent() only emits choices deltas, never a usage-bearing
    chunk.
  • transformMessage's case "result" collapses Claude's result message - which
    carries total_cost_usd, cumulative usage, num_turns, and duration_ms - into a
    bare message_stop / done event, throwing the numbers away. ClaudeCodeMessage.TotalCostUSD
    is parsed but referenced nowhere else.

Net effect: in cmd/agent.go, response.Usage == nil on every turn, so
processSyncResponse never accumulates tokens, requestCount stays 0, assistant lines
carry no token_usage, and emitSessionStats() returns early - no session_stats
line is emitted
. (Tool-call metrics are unaffected because they do not depend on usage.)

Proposed fix

  • Parse the usage object off each Claude assistant message (input_tokens,
    output_tokens, cache_creation_input_tokens, cache_read_input_tokens) and attach
    it as response.Usage, so processSyncResponse accumulates tokens and each assistant
    line carries token_usage.
  • On the result message, surface the cumulative usage and total_cost_usd so
    requestCount > 0 and emitSessionStats() emits a session_stats line. For a
    subscription run the cost can be reported as Claude's total_cost_usd (an
    equivalent/retail value) or 0 with a "subscription" marker - but the token counts
    should be the real values
    .

Context. This is the upstream dependency for full run-metrics parity in
inference-gateway/infer-action's new Claude Code subscription mode
(inference-gateway/infer-action#132). The action already degrades gracefully (tool-call
counts/failures render; token usage + cost are simply omitted) and will render identical
token/cost footers in Claude Code mode with no further action-side change once this
lands.

Source references: internal/infra/adapters/claude_code_client.go (buildResponse,
createDeltaEvent, transformMessage case "result", ClaudeCodeMessage.TotalCostUSD);
cmd/agent.go (processSyncResponse, emitSessionStats).

Acceptance Criteria

  • In Claude Code mode, each assistant stream line carries token_usage with the
    real input/output (and cache) token counts parsed from Claude's usage.
  • processSyncResponse accumulates tokens so requestCount > 0 and a session_stats
    line is emitted at the end of a Claude Code run.
  • The session_stats line carries cost (Claude's total_cost_usd, or 0 with a
    subscription marker) so downstream consumers can render per-session cost.
  • inference-gateway/infer-action's result footer shows non-zero token usage (and
    cost) for a Claude Code run with no action-side change.

Activity

  1. added
    enhancement
    on Jun 24, 2026
  2. moved this to Todo in 2026on Jun 24, 2026
  3. edenreich commented on Jun 24, 2026

    @edenreich
    ContributorAuthor

    Filed alongside inference-gateway/infer-action#133, which adds the action-side Claude Code subscription mode. The action degrades gracefully (tool-call metrics render; token usage + cost omitted) until this lands.

  4. inference-gateway-releaser commented on Jun 24, 2026

    @inference-gateway-releaser
    Contributor

    🎉 This issue has been resolved in version 0.123.0 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

  5. moved this from Todo to Done in 2026on Jun 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementreleased

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions