Skip to content

A stream that fails midway keeps the usage the upstream billed - #26

Merged
fylorn merged 1 commit into
mainfrom
fix/price-failed-streams
Sep 16, 2026
Merged

fylorn merged 1 commit into
mainfrom
fix/price-failed-streams

Conversation

@fylorn

@fylorn fylorn commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

What this changes

Fixes the fourth gap listed in #24. A stream the upstream breaks, or the
tool firewall cuts, now records the usage that was already billed, with a
price.

  • RequestFailed gains optional bytes, duration_ms and usage, all
    #[serde(default, skip_serializing_if = "Option::is_none")]:
    • bytes: present once the response headers have arrived.
    • duration_ms: present for every failure.
    • usage: whatever the sniffer saw before the failure.
  • The recorder writes all three endings through one path. A failure with
    usage gets tokens and a cost marked estimated, and still counts as failed.
    RequestPriced is reported for failures too.
  • A stream cut by the tool firewall reports source denied, not
    upstream, matching the WebSocket cut from Every request that starts reports how it ended #25.

Why

By the time a stream breaks midway, the upstream has usually billed the
full input (message_start arrives first) and the output up to the break.
RequestFailed carried no usage, so the recorder wrote failures without
tokens or cost, and that money never appeared in any total.

Why the cost is estimated. It is the same reason as a cancellation: the
cumulative output count arrives only at the end of the stream, so the
amount at a break is a lower bound.

Why bytes is absent before the response headers. A failure before
them received no response at all; reporting 0 would read as an empty
response.

Why a firewall cut is denied. The upstream did not break; a policy
stopped the response. The stream now reports the error's own source, the
same change #25 made for requests every upstream refused.

How it was checked

  • tests/endings.rs:
    • A broken stream's RequestFailed carries the bytes and the 5,000 input
      tokens from message_start.
    • A stream cut for a curl | sh tool call reports denied and keeps its
      usage.
    • A request every upstream refused has a duration but no bytes or usage.
  • Recorder tests:
    • A broken stream's row has its tokens, bytes, duration, status and an
      estimated cost.
    • It still counts as failed, and its money lands in the estimated total.
    • A failure with no usage has no price but keeps its duration.
    • Its price goes back onto the bus as an estimate.
  • scripts/smoke.sh: the fake upstream's firewall stream now reports input
    usage in message_start, and the cut request must reach /history with
    those tokens and an estimated cost.

Each test was run against the broken version it guards, and each fails:

  • a failure carrying no usage;
  • a cut reported as upstream;
  • a failure priced as exact;
  • the recorder dropping a failure's usage.

Gates: cargo fmt --check, cargo clippy --workspace --all-targets -D warnings,
cargo test --workspace (1,157 passed), ./scripts/smoke.sh (48 passed; the
memory warning read 36 MB this run).

🤖 Generated with Claude Code

A stream the upstream breaks, or the tool firewall cuts, has usually
been billed by then: the full input as soon as the upstream started
answering, and the output up to the cut. RequestFailed carried none of
it, so the recorder wrote the row without tokens or cost, and the money
was never in the totals.

RequestFailed gains optional `bytes`, `duration_ms` and `usage`. The
ending fills them from what it has seen: bytes once the response headers
have arrived (a failure before them received nothing, and 0 would read as
an empty response), the elapsed time for every failure, and the sniffed
usage. The recorder now writes failures through the same path as the
other two endings, so a failure with usage gets tokens and a price, marked
estimated for the same reason as a cancellation: the output count is cut
short. It still counts as failed.

A stream cut by the tool firewall reported source `upstream`. It now
reports the error's own source, `denied`, like the WebSocket cut: the
upstream did not break, a policy stopped it.

Checked against the broken versions: a failure without usage, a cut
reported as `upstream`, a failure priced as exact, and the recorder
dropping a failure's usage each fail their tests. The smoke script's
firewall stream now reports input usage in message_start, and the cut
request must reach /history with those tokens and an estimated cost.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Thanks for the PR. One thing before review: this targets main, and it should target dev.

main is a release-only line — branch-protected, linear history, and every commit on it is a tagged release snapshot. Routine work lands on dev and reaches main with the next release. GitHub pre-filled main here because it is the default branch, so this is our defaults being unhelpful rather than anything you did wrong.

No need to close and reopen: click Edit next to the PR title and change the base branch to dev. The commits and the discussion carry over.

See CONTRIBUTING.md and CONTRIBUTING.md for the branch contract.

@fylorn
fylorn merged commit 4108acd into main Sep 16, 2026
2 checks passed
fylorn added a commit that referenced this pull request Sep 16, 2026
)

"No price" was counted four different ways, and none matched what the
interface says. The dashboard's note reads "these models are not in the
price list", but the summary counted every row without a cost on
per-token billing: each failed request, and each response that never
reported usage. Time buckets, cost groups and sessions counted every null
cost, subscription calls included. The pricing page, which exists to say
"configure a price for these models", listed failures and missing usage
too, and a price fixes neither.

Two predicates now define it, and every query uses them:

- no price: the usage is known and the model has no price. Configuring
  a price fixes it.
- no usage: a response the upstream accepted (a 2xx, a WebSocket 101,
  or a cancellation) arrived without usage. The money is missing from
  the total, and no price can fix it.

A failure without usage is neither: a request that failed before any
response is not billed, and one that failed midway carries its usage
since #26. A 4xx response is not billed either. Subscription calls stay
outside both, as the summary already had them.

Summary, CostBucket and CostGroup gain `no_usage_requests`, and the
pricing page lists only models a price would fix.

Sessions could present an estimate as exact. TurnView had no
`cost_estimated`, so the waterfall showed a cancelled turn's lower bound
exactly like a measured amount, and SessionView's total mixed the two.
TurnView gains `cost_estimated`. SessionView gains
`cost_micros_estimated`, and `priced_turns` and `no_usage_turns`, so the
desktop app stops deriving "no turn had a price" from
`turns - unpriced_turns`, a subtraction that is wrong once failures and
subscription turns no longer count as unpriced.

Workspace version 0.1.2, for the release the desktop app will pin.

Each new test was run against the broken version it guards: the
summary's old predicate counts a failure as unpriced; counting a 4xx as
missing usage, buckets counting subscription rows, the pricing page's
old filter, and a session hiding its estimated part each fail their test.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
@fylorn
fylorn deleted the fix/price-failed-streams branch September 16, 2026 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant