#21865·ai

harness-claude-code: the bridge sums `total_cost_usd` across results, but it's a running total, so `costUsd` is over-reported

Author: brightironCreated Sep 30, 2026Updated Sep 30, 2026
Labelstask-identify-issue-type-donetask-bug-reproduction-successfactory-activefactory-automaticharness/claude-codetask-identify-harness-labels-done

Description

costUsd in the finish metadata is over-reported whenever a turn sees more than one result, or a resumed query carries an earlier total forward. The bridge adds up total_cost_usd from every successful result in runTurn:

typescript
if (typeof msg.total_cost_usd === 'number') {
  totalCostUsd = (totalCostUsd ?? 0) + msg.total_cost_usd;
}

The Agent SDK documents that field as cumulative (SDKResultSuccess.total_cost_usd):

each result carries the running total so far, so read the latest result rather than summing across results. ... a resumed or forked session continues from the total its transcript saved, when it has one (so the first result already carries the earlier turns)

These numbers are from two runs on 1.0.139 (Claude Code 2.1.281). In each run, turn 2 was a resumed query() whose first result is Claude Code's own task-notification result: zero usage, and a repeat of turn 1's total.

run turn 2 results' total_cost_usd turn 2 finish costUsd
empty turn (see #21864) 0.1573332 (notification only) 0.1573332, for a turn with no model call
same turn with that issue's fix 0.0298164, then 0.05730615 0.08712255 (the two added)

In the second run, the SDK's running total after turn 2 is 0.05730615, and turn 2's own spend is 0.05730615 - 0.0298164 = 0.02748975. The bridge reports 0.08712255.

Expected: costUsd follows the SDK's guidance. That means the latest result's value, or the difference from the total at the start of the turn if costUsd is meant to be per turn. Which of those it's meant to be is your call.

Actual: every result's running total is added, so earlier spend is counted again.

What I did not check

  • Turns with more than one result for other reasons (steering messages, compaction). I only measured the task-notification case.
  • Claude Code versions other than 2.1.281 for these numbers.

AI SDK Version

  • @ai-sdk/harness-claude-code: 1.0.139
  • @ai-sdk/harness: 1.0.135
  • @anthropic-ai/claude-agent-sdk: 0.3.281, @anthropic-ai/claude-code: 2.1.281 (from the bridge's own package.json)

Code of Conduct

  • I agree to follow this project's Code of Conduct