Skip to content

fix(desktop): count the paused leg's tokens and web searches - #10637

Merged
kodjima33 merged 1 commit into
mainfrom
watchdog/paused-turn-usage-accounting
Jul 26, 2026
Merged

kodjima33 merged 1 commit into
mainfrom
watchdog/paused-turn-usage-accounting

Conversation

@kodjima33

@kodjima33 kodjima33 commented Jul 26, 2026 •

Copy link
Copy Markdown
Collaborator

Bug

A streaming chat turn that Anthropic pauses (stop_reason: pause_turn) is finished by a non-streaming continuation whose result is spliced back into the open SSE body. The splice reported the turn's usage as merge_stream_usage(message_start_usage, continuation_usage) — and merge_stream_usage takes output_tokens from its second argument alone. The streamed leg was therefore dropped from both the client's usage chunk and the Firestore record_llm_usage row.

Why it matters

The dropped leg is the leg that ran the web search. A turn pauses because a server tool ran, so the pausing message_delta is exactly where usage.server_tool_use.web_search_requests lives — and those are billed per request (compute_cost → WEB_SEARCH_COST_PER_REQUEST), on top of every output token spent before the pause (thinking plus the search query). Every paused turn was accounted for as if its first half never happened: cost under-reported per turn, and the user-visible usage chunk under-reported completion_tokens.

AnthropicUsage::server_tool_use even carries the comment "billed per request, so it must survive into cost computation" — the streaming splice was the one path where it did not.

Root cause / fix

The non-streaming lane already sums every leg (accumulate_anthropic_usage in transport.rs, used across complete_anthropic_server_tool_turn's own continuations). Only the streaming splice had its own, lossy totalling rule.

Extract paused_turn_total_usage() so the splice totals the same way:

  1. merge message_start with the pausing message_delta to make the streamed leg whole (input side comes from the former, output side from the latter);
  2. accumulate the continuation's usage on top, reusing accumulate_anthropic_usage.

A pause that reported no usage at all falls back to message_start, so the fix can never report less than before.

Tested

  • cargo test — 382 passed, 0 failed.
  • Both new regression tests fail on the pre-fix expression (input 130 vs 230, 110 vs 200) and pass on the fix.
  • cargo fmt --check and cargo clippy --all-targets -- -D warnings clean.
  • The live Anthropic pause_turn round trip is not driveable on demand and ANTHROPIC_API_URL is a constant with no test seam, so the fix is proven through the production usage-totalling function rather than against the provider.

Product invariants

none

Failure-Class: none

🤖 automated by hourly watchdog — tested and merged

Review in cubic

A streaming turn that Anthropic pauses (`stop_reason: pause_turn`) is finished
by a non-streaming continuation whose result is spliced into the open SSE body.
That splice reported usage as `merge_stream_usage(message_start, continuation)`,
which takes output tokens from the continuation alone — so the streamed leg was
dropped from both the client's usage chunk and the Firestore `record_llm_usage`
row.

The dropped leg is not a rounding error: it is the leg that ran the web search.
A turn pauses *because* a server tool ran, so its `usage.server_tool_use`
carries the `web_search_requests` Anthropic bills per request (see
`compute_cost`, `WEB_SEARCH_COST_PER_REQUEST`), plus every output token spent
before the pause (thinking and the search query). Every paused turn was
therefore billed as if its first half never happened.

The non-streaming lane already sums every leg (`accumulate_anthropic_usage` in
transport.rs). Extract `paused_turn_total_usage()` so the streaming splice
totals the same way: merge `message_start` with the pausing `message_delta` to
make the streamed leg whole, then accumulate the continuation on top. A pause
that reported no usage falls back to `message_start`, never to less than
before.

Verified: cargo test 382 passed; both new tests fail on the pre-fix expression
(input 130 vs 230, 110 vs 200) and pass on the fix; cargo fmt --check and cargo
clippy --all-targets -D warnings clean. The live Anthropic pause_turn round
trip is not driveable on demand and the upstream URL is a constant with no test
seam, so the fix is proven through the production usage-totalling function
rather than against the provider.

Failure-Class: none
@kodjima33
kodjima33 merged commit 3a75ca0 into main Jul 26, 2026
31 of 34 checks passed
@kodjima33
kodjima33 deleted the watchdog/paused-turn-usage-accounting branch July 26, 2026 05:38
@undivisible undivisible added the AI label Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants