harness-claude-code: per-step usage ignores `message_delta`, so outputTokens is a placeholder on direct auth and all zeros via AI Gateway
Description
Summary
@ai-sdk/harness-claude-code takes each step's usage from the assistant message's message.usage. Claude Code fills that field from the stream's message_start usage and never updates it. The real per-call numbers arrive in message_delta, which the bridge receives as a stream_event and drops.
Two results:
- Direct Anthropic auth:
finish-stepinput and cache counts are right, butoutputTokensis themessage_startplaceholder (a few tokens). Step outputs never add up tofinish.totalUsage.outputTokens. auth: 'ai-gateway': the gateway reportsusage: { input_tokens: 0, output_tokens: 0 }onmessage_start(#17326), so everyfinish-stepis all zeros, for Anthropic models too.finish.totalUsageandcostUsdstay correct because they come from the CLI'sresult.usage.
Anything that meters per step from finish-step (observability, onStepFinish) is wrong in case 1 and reports zero in case 2.
Not a duplicate of #17326
#17326 asks the gateway to fill message_start. Its bot PRs (#17534, #17535, #17538) only change the Anthropic provider and have been in "changes-required" since July. This issue is about the bridge, which should read message_delta. That fix works independently of the gateway, also fixes case 1, and works with any Anthropic-compatible proxy.
Where
packages/harness-claude-code/src/bridge/create-emit-stream-event.ts:
- The
assistantbranch doesmapUsage(msg.message.usage)and stores it asstate.pendingStepUsage. An all-zero object is truthy, so it is kept. handleStreamEventreturns early whentypeof event.index !== 'number', somessage_deltaevents (which carryevent.usage) are never read.
packages/harness-claude-code/src/bridge/index.ts, result handling: usage: streamEventState.pendingStepUsage ?? harnessUsage. Since #19070 the snapshot wins over result.usage, which is right on direct auth but makes the final step zero via the gateway.
Reproduction (captured)
Claude Agent SDK 0.3.267, includePartialMessages: true, prompt "Reply with the single word: pong".
Direct, claude-haiku-4-5:
message_start.usage: {"input_tokens":10,"cache_creation_input_tokens":16727,"cache_read_input_tokens":0,"output_tokens":3}
assistant.message.usage: {"input_tokens":10,"cache_creation_input_tokens":16727,"cache_read_input_tokens":0,"output_tokens":3}
message_delta.usage: {"input_tokens":10,"cache_creation_input_tokens":16727,"cache_read_input_tokens":0,"output_tokens":56}
Gateway (ANTHROPIC_BASE_URL=https://ai-gateway.vercel.sh), anthropic/claude-haiku-4.5:
message_start.usage: {"input_tokens":0,"output_tokens":0}
assistant.message.usage: {"input_tokens":0,"output_tokens":0}
message_delta.usage: {"input_tokens":509,"output_tokens":52,"cache_creation_input_tokens":26857}
result.usage: {"input_tokens":509,"cache_creation_input_tokens":26857,"cache_read_input_tokens":0,"output_tokens":52}
HarnessAgent with createClaudeCode(...), a prompt that forces one Bash call (two model calls per turn), reading finish-step from fullStream:
auth: 'ai-gateway', anthropic/claude-haiku-4.5:
finish-step usage: {"inputTokens":0,"outputTokens":0,"totalTokens":0,...}
finish-step usage: {"inputTokens":0,"outputTokens":0,"totalTokens":0,...}
totalUsage: {"inputTokens":79889,"outputTokens":165,...} costUsd 0.2769
auth: 'direct', claude-haiku-4-5:
finish-step usage: {"inputTokens":25462,"outputTokens":7,...}
finish-step usage: {"inputTokens":25378,"outputTokens":1,...}
totalUsage: {"inputTokens":50840,"outputTokens":141,...}
Expected
finish-step.usage is that call's real usage on every auth path, and the steps add up to finish.totalUsage.
Suggested fix
Take step usage from the stream_event with event.type === 'message_delta', falling back to the assistant copy and then result.usage only when no delta was seen. @ai-sdk/anthropic already treats message_delta as the source of truth (#10671, #11611). A locally patched bridge with this order gave correct nonzero per-step usage on both direct and gateway auth, with steps summing to result.usage.
Related: #17326 (gateway zeros), #19068 / #19070 (same code path).
Reproduction
Captured output above. Steps: run Agent SDK query() with includePartialMessages: true and log assistant.message.usage plus the message_delta stream event, first direct and then with ANTHROPIC_BASE_URL=https://ai-gateway.vercel.sh. Then run HarnessAgent with createClaudeCode({ auth: 'ai-gateway' }) and log finish-step parts. A minimal repo can be added if useful.
AI SDK Version
- @ai-sdk/harness-claude-code 1.0.95 (same logic on main at 1.0.111)
- @ai-sdk/harness 1.0.91, ai 7.0.83
- Claude Code 2.1.263 / Agent SDK 0.3.267 for the SDK-level capture
Code of Conduct
- [x] I agree to follow this project's Code of Conduct
Identify issue type
Identified as Bug
Confidence: High
Rationale
This is a concrete defect in existing harness usage accounting: emitted per-step usage is incorrect and does not sum to the correct total usage.
Evidence
- Per-step
finish-step.usageis taken from the initial assistant/message-start usage while the authoritative usage inmessage_deltais dropped. - Direct Anthropic authentication reports placeholder output-token counts, and AI Gateway authentication reports zero usage for every step.
- The reporter provides captured usage values, affected versions and code paths, reproduction steps, expected behavior, and a locally validated fix.
Bug Reproduction - AI SDK 7
Bug was reproduced.
Branch: main
PR: successful reproduction pull request
Details
- A live direct-auth Claude Agent SDK 0.3.245 call with
claude-haiku-4-5reported 7 output tokens in the assistant message but 115 in the authoritativemessage_deltafor the same tool-use step. - Replaying the captured response through the bridge emitted
finish-step.usage.outputTokensas 7 instead of the expected 115, reproducing incorrect per-step metering. - The live response fixture is stored at
packages/harness-claude-code/src/bridge/__fixtures__/issue-20633-direct-step-usage.json, with a failing provider regression test inpackages/harness-claude-code/src/bridge/create-emit-stream-event.test.ts. - The runnable reproduction at
examples/ai-functions/src/reproduction/issue-20633.tsexits with code 1 when the bridge ignoresmessage_deltausage. - The AI Gateway variant could not be evaluated live because the configured Gateway credential was rejected as invalid; a full
HarnessAgentlive run was also blocked by a Vercel Sandbox authentication HTTP 403.
Relevant Documentation
- https://platform.claude.com/docs/en/build-with-claude/streaming
- https://vercel.com/docs/ai-gateway/sdks-and-apis/anthropic-messages-api
Bugfix - AI SDK 7
Created bugfix and opened PR: #20778
Root Cause
The Claude Code bridge discarded indexless message_delta events before reading their authoritative usage and relied on assistant message-start usage instead; the captured replay confirmed 7 tokens were emitted instead of 115.
Summary
The bridge now merges assistant usage with authoritative message_delta usage, with delta fields taking precedence regardless of event order, and includes a patch changeset.
Testing
Added regression coverage for message_delta usage arriving before or after the assistant message while preserving assistant usage fields omitted from the delta.
:rocket: Published in:
| Package | Version |
|---|---|
ai |
7.0.103 github npm |
@ai-sdk/alibaba |
2.0.47 github npm |
@ai-sdk/amazon-bedrock |
5.0.85 github npm |
@ai-sdk/angular |
3.0.103 github npm |
@ai-sdk/anthropic |
4.0.55 github npm |
@ai-sdk/anthropic-aws |
2.0.47 github npm |
@ai-sdk/assemblyai |
3.0.42 github npm |
@ai-sdk/azure |
4.0.72 github npm |
@ai-sdk/baseten |
2.1.28 github npm |
@ai-sdk/black-forest-labs |
2.0.43 github npm |
@ai-sdk/bytedance |
2.0.45 github npm |
@ai-sdk/cartesia |
3.0.37 github npm |
@ai-sdk/cerebras |
3.0.50 github npm |
@ai-sdk/code-mode |
1.0.60 github npm |
@ai-sdk/cohere |
4.0.43 github npm |
@ai-sdk/deepgram |
3.1.13 github npm |
@ai-sdk/deepinfra |
3.0.50 github npm |
@ai-sdk/deepseek |
3.0.46 github npm |
@ai-sdk/devtools |
1.0.21 github npm |
@ai-sdk/elevenlabs |
3.0.43 github npm |
@ai-sdk/fal |
3.0.43 github npm |
@ai-sdk/fireworks |
3.0.53 github npm |
@ai-sdk/fish-audio |
3.0.20 github npm |
@ai-sdk/gateway |
4.0.83 github npm |
@ai-sdk/gladia |
3.0.42 github npm |
@ai-sdk/gmicloud |
3.0.21 github npm |
@ai-sdk/google |
4.0.73 github npm |
@ai-sdk/google-vertex |
5.0.85 github npm |
@ai-sdk/groq |
4.0.43 github npm |
@ai-sdk/harness |
1.0.113 github npm |
@ai-sdk/harness-acp |
1.0.51 github npm |
@ai-sdk/harness-claude-code |
1.0.117 github npm |
@ai-sdk/harness-cline |
1.0.40 github npm |
@ai-sdk/harness-codex |
1.0.115 github npm |
@ai-sdk/harness-cursor |
1.0.26 github npm |
@ai-sdk/harness-deepagents |
1.0.113 github npm |
@ai-sdk/harness-fx |
1.0.26 github npm |
@ai-sdk/harness-github-copilot |
1.0.8 github npm |
@ai-sdk/harness-grok-build |
1.0.50 github npm |
@ai-sdk/harness-opencode |
1.0.115 github npm |
@ai-sdk/harness-pi |
1.0.115 github npm |
@ai-sdk/huggingface |
2.0.50 github npm |
@ai-sdk/hume |
3.0.42 github npm |
@ai-sdk/klingai |
4.0.44 github npm |
@ai-sdk/langchain |
3.0.103 github npm |
@ai-sdk/llamaindex |
3.0.103 github npm |
@ai-sdk/luma |
3.0.43 github npm |
@ai-sdk/mcp |
2.0.51 github npm |
@ai-sdk/minimax |
3.0.32 github npm |
@ai-sdk/mistral |
4.0.45 github npm |
@ai-sdk/moonshotai |
3.0.51 github npm |
@ai-sdk/open-responses |
2.0.46 github npm |
@ai-sdk/openai |
4.0.68 github npm |
@ai-sdk/openai-compatible |
3.0.50 github npm |
@ai-sdk/otel |
1.0.103 github npm |
@ai-sdk/perplexity |
4.0.45 github npm |
@ai-sdk/policy-opa |
1.0.103 github npm |
@ai-sdk/prodia |
2.0.43 github npm |
@ai-sdk/provider |
4.0.16 github npm |
@ai-sdk/provider-utils |
5.0.42 github npm |
@ai-sdk/quiverai |
2.0.42 github npm |
@ai-sdk/react |
4.0.106 github npm |
@ai-sdk/replicate |
3.0.43 github npm |
@ai-sdk/revai |
3.0.42 github npm |
@ai-sdk/rsc |
3.0.103 github npm |
@ai-sdk/sandbox-just-bash |
1.0.113 github npm |
@ai-sdk/sandbox-vercel |
1.0.113 github npm |
@ai-sdk/svelte |
5.0.103 github npm |
@ai-sdk/togetherai |
3.0.51 github npm |
@ai-sdk/tui |
1.0.104 github npm |
@ai-sdk/typesafe-ai |
3.0.0 github npm |
@ai-sdk/valibot |
3.0.42 github npm |
@ai-sdk/voyage |
2.0.42 github npm |
@ai-sdk/vue |
4.0.103 github npm |
@ai-sdk/workflow |
2.0.34 github npm |
@ai-sdk/workflow-harness |
1.0.113 github npm |
@ai-sdk/xai |
5.0.1 github npm |
@ai-sdk/zai |
3.0.12 github npm |
Bug Reproduction - AI SDK 6
Issue report appears invalid.
Target branch: release-v6.0
- Issue #20633 assumes
@ai-sdk/harness-claude-codeand its bridge can run onrelease-v6.0, but neitherpackages/harness-claude-codenorpackages/harnessexists on this target branch. pnpm-lock.yaml,tsconfig.json, workspace package manifests, andexamples/ai-functions/package.jsoncontain no@ai-sdk/harness-claude-codeor@ai-sdk/harnessdependency or workspace reference.- The reported incorrect
finish-step.usagebehavior therefore cannot occur on this checkout; no reproduction artifact was created because that would require adding a package from the AI SDK 7 release line.
Relevant Documentation
- https://ai-sdk.dev/docs/migration-guides/migration-guide-6-0
- https://ai-sdk.dev/docs/agents/harnesses/overview
Bug Reproduction - AI SDK 5
Issue report appears invalid.
Target branch: release-v5.0
- Issue #20633 assumes the
@ai-sdk/harness-claude-codebridge is available onrelease-v5.0, but bothpackages/harness-claude-codeandpackages/harnessare absent. packages/ai/package.jsonidentifies the checked-out release as AI SDK5.0.274.pnpm-lock.yamlcontains no@ai-sdk/harness-claude-code,@ai-sdk/harness, or Claude Agent SDK dependency, and the roottsconfig*.jsonfiles contain no harness project reference.examples/ai-functionsis absent, so no reproduction artifact was created; adding the missing AI SDK 7 harness package would change the release under test.- The reported incorrect per-step
finish-step.usagebehavior cannot occur through the unavailable Claude Code harness on this target branch.
Relevant Documentation
- https://ai-sdk.dev/v5/docs/migration-guides/migration-guide-5-0
- https://ai-sdk.dev/docs/agents/harnesses/overview