Skip to content

feat(claude-code): surface token usage and cost in headless mode - #650

Merged
edenreich merged 8 commits into
mainfrom
feat/claude-code-token-usage
Jun 24, 2026
Merged

edenreich merged 8 commits into
mainfrom
feat/claude-code-token-usage

Conversation

@edenreich

Copy link
Copy Markdown
Contributor

Summary

In Claude Code mode the stream adapter dropped token usage, so headless infer agent
reported 0 in / 0 out / 0 total, no token_usage on assistant lines, and no
session_stats line. This surfaces the real token counts and prices the session via
infer's own pricing list.

  • Tokens (real): parse the claude stream-json result message's cumulative usage
    (internal/infra/adapters/claude_code_client.go) and attach it as response.Usage.
    Cache tokens fold into prompt_tokens (the SDK usage type has no cache tier). Once
    Usage is set, the existing processSyncResponse / emitSessionStats chain accumulates
    tokens, emits per-turn token_usage, and produces a session_stats line — no changes
    needed there.
  • Cost from infer's pricing list: session cost is computed from the accumulated tokens
    via the existing PricingService — consistent with how the official Claude Code
    Action/SDK estimate cost from a bundled price table — rather than Claude's
    total_cost_usd (a client-side estimate; a subscription has no per-call billing).
  • Model-id consistency: Claude Code model ids are now anthropic/-prefixed (canonical,
    matching gateway mode and the pricing table). Bare claude-* ids are still accepted and
    normalized (CanonicalClaudeModelID), and the adapter strips the prefix before invoking
    the claude CLI. The model list is curated to ids priced in DefaultModelPricing
    (a test enforces the invariant).
  • Fable 5 context window: claude-fable-5 was falling through to the generic
    claude = 200K matcher; corrected to 1M per Anthropic's docs.

Cross-repo impact

Unblocks run-metrics parity in inference-gateway/infer-action#133 (its
src/usage.ts::extractUsage renders the CLI's token_usage / session_stats footer) with
no action-side change — its bare claude-… model id normalizes to anthropic/… and
prices correctly. A docs note for the new anthropic/-prefixed Claude Code ids belongs in
inference-gateway/docs (alongside #133's docs ticket).

Test plan

  • task test (full suite), task lint (0 issues), and task build all pass.
  • New tests: adapter usage parse/emit + prefix stripping (claude_code_client_test.go),
    model-service prefix/curate/normalize + curate-to-priced invariant
    (model_claude_code_test.go), Claude pricing default resolution
    (pricing_service_test.go), Fable 5 = 1M (context_test.go).
  • Manual e2e (assistant lines carry token_usage, session_stats shows non-zero cost)
    requires the claude CLI logged into a Max/Pro subscription.

Notes

  • The context-window change is scoped to Fable 5 only; claude-opus-4-6 / -4-8 and
    claude-sonnet-4-6 remain 200K per the existing test, though Anthropic docs list them as
    1M GA — can bump in a follow-up.

Closes #649

…ricing list

Parse the claude stream-json result message's cumulative usage and attach it as
response.Usage, so headless `infer agent` accumulates tokens, emits per-turn
token_usage, and produces a session_stats line. Session cost is computed from
those tokens via infer's existing pricing list - consistent with how the
official Claude Code Action/SDK estimate from a bundled price table - rather
than Claude's total_cost_usd.

Claude Code model ids are now anthropic/-prefixed (canonical, matching gateway
mode and the pricing table); bare claude-* ids are still accepted and
normalized via CanonicalClaudeModelID. The list is curated to ids priced in
DefaultModelPricing, and the adapter strips the prefix before invoking the
claude CLI.

Unblocks run-metrics parity in inference-gateway/infer-action#133 with no
action-side change.

Closes #649
claude-fable-5 fell through to the generic claude=200K matcher; per Anthropic's
docs Fable 5 ships a 1M-token context window by default. Add a fable matcher
ahead of the generic claude rule.
@edenreich
edenreich requested a review from a team as a code owner June 24, 2026 19:38
Comment thread cmd/agent.go Resolved
claude-opus-4-8 supports a 1M-token context window but was falling through to
the generic claude=200K matcher. Add it to the 1M matcher alongside opus-4-7.
Add anthropic/claude-opus-4-8 to the pricing table ($5/$25 per MTok, matching
Opus 4.7) and the Claude Code subscription model list. It was missing because
the list is curated to ids the pricing table prices.
In Claude Code mode every model is accessed via the subscription, so the model
picker now buckets them under [4] Subscription instead of Pay-as-you-go,
regardless of the shared pricing table's per-token prices.
@edenreich
edenreich merged commit 4e4f846 into main Jun 24, 2026
1 check passed
@edenreich
edenreich deleted the feat/claude-code-token-usage branch June 24, 2026 20:07
inference-gateway-releaser Bot added a commit that referenced this pull request Jun 24, 2026
## [0.123.0](v0.122.2...v0.123.0) (2026-06-24)

### 🚀 Features

* **storage:** add Cloudflare D1 conversation storage driver ([#646](#646)) ([b6785ae](b6785ae)), closes [#645](#645)
* **claude-code:** surface token usage and cost in headless mode ([#650](#650)) ([4e4f846](4e4f846)), closes [#649](#649)

### 🐛 Bug Fixes

* set git identity for semantic release ([a03ed0b](a03ed0b))

### ♻️ Code Refactoring

* tighten inline diff context to 2 lines ([adcc17b](adcc17b))

### 🔧 Build System

* **deps:** bump the github-actions group with 3 updates ([#648](#648)) ([92784cd](92784cd))
* **deps:** bump the gomod group with 3 updates ([#647](#647)) ([a319eeb](a319eeb))

### 🧹 Maintenance

* **deps:** bump infer CLI v0.121.1 -> v0.122.2, infer-action v0.15.1 -> v0.15.4 ([#644](#644)) ([6befa6f](6befa6f))
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.123.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Shipped in a release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant