Repository navigation
feat(claude-code): surface token usage and cost in headless mode - #650
Merged
Merged
Conversation
…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
commented
Jun 24, 2026
edenreich
commented
Jun 24, 2026
edenreich
commented
Jun 24, 2026
edenreich
commented
Jun 24, 2026
edenreich
commented
Jun 24, 2026
edenreich
commented
Jun 24, 2026
Co-authored-by: Eden Reich <[email protected]>
Co-authored-by: Eden Reich <[email protected]>
edenreich
commented
Jun 24, 2026
Co-authored-by: Eden Reich <[email protected]>
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.
4 tasks
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))
Contributor
|
🎉 This PR is included in version 0.123.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
In Claude Code mode the stream adapter dropped token usage, so headless
infer agentreported
0 in / 0 out / 0 total, notoken_usageon assistant lines, and nosession_statsline. This surfaces the real token counts and prices the session viainfer's own pricing list.
claudestream-jsonresultmessage's cumulativeusage(
internal/infra/adapters/claude_code_client.go) and attach it asresponse.Usage.Cache tokens fold into
prompt_tokens(the SDK usage type has no cache tier). OnceUsageis set, the existingprocessSyncResponse/emitSessionStatschain accumulatestokens, emits per-turn
token_usage, and produces asession_statsline — no changesneeded there.
via the existing
PricingService— consistent with how the official Claude CodeAction/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).anthropic/-prefixed (canonical,matching gateway mode and the pricing table). Bare
claude-*ids are still accepted andnormalized (
CanonicalClaudeModelID), and the adapter strips the prefix before invokingthe
claudeCLI. The model list is curated to ids priced inDefaultModelPricing(a test enforces the invariant).
claude-fable-5was falling through to the genericclaude = 200Kmatcher; corrected to 1M per Anthropic's docs.Cross-repo impact
Unblocks run-metrics parity in inference-gateway/infer-action#133 (its
src/usage.ts::extractUsagerenders the CLI'stoken_usage/session_statsfooter) withno action-side change — its bare
claude-…model id normalizes toanthropic/…andprices correctly. A docs note for the new
anthropic/-prefixed Claude Code ids belongs ininference-gateway/docs(alongside #133's docs ticket).Test plan
task test(full suite),task lint(0 issues), andtask buildall pass.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).token_usage,session_statsshows non-zero cost)requires the
claudeCLI logged into a Max/Pro subscription.Notes
claude-opus-4-6/-4-8andclaude-sonnet-4-6remain 200K per the existing test, though Anthropic docs list them as1M GA — can bump in a follow-up.
Closes #649