Repository navigation
fix(desktop): count the paused leg's tokens and web searches - #10637
Merged
Merged
Conversation
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
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.
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 asmerge_stream_usage(message_start_usage, continuation_usage)— andmerge_stream_usagetakesoutput_tokensfrom its second argument alone. The streamed leg was therefore dropped from both the client's usage chunk and the Firestorerecord_llm_usagerow.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_deltais exactly whereusage.server_tool_use.web_search_requestslives — 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-visibleusagechunk under-reportedcompletion_tokens.AnthropicUsage::server_tool_useeven 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_usageintransport.rs, used acrosscomplete_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:message_startwith the pausingmessage_deltato make the streamed leg whole (input side comes from the former, output side from the latter);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.input 130vs230,110vs200) and pass on the fix.cargo fmt --checkandcargo clippy --all-targets -- -D warningsclean.pause_turnround trip is not driveable on demand andANTHROPIC_API_URLis 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