fix(claude): forward the billed dollar receipt off the result line - #159
Merged
Conversation
The claude backend declared `total_cost_usd` on `ClaudeStreamResult` but yielded only `usage`. The figure is a sibling of `usage`, not a member, so the one genuinely provider-billed number the bridge receives never left the backend and every claude turn reached the caller with no dollar cost. Forward it through the lane the bridge already uses for a known cost: `cost` with `cost_known: true` and `provider-receipt` provenance, at `cost_scope: total` because the figure covers the whole `claude -p` invocation. A run that reports no figure keeps `cost_known: false` and sends no amount. A negative or non-finite figure is refused rather than billed. The catalog-estimate lane is untouched. Pi reports `cost_known: false` by contract because neither Pi nor the proxy reports billed dollars.
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.
Problem
ClaudeStreamResultdeclarestotal_cost_usd(src/backends/claude.ts:74), but the terminal yield sent only{ finish_reason, usage, internal_session_id }.total_cost_usdis a sibling ofusage, not a member, so the one genuinely provider-billed number the bridge receives never left the backend.Measured consequence downstream: across 292 fleet runs,
spentTotal.usd > 0on zero runs. The dollar channel is dead, and this is one of the two reasons.Change
Forward the figure through the lane the bridge already uses for a known cost (
src/usage.ts:59-67,src/streaming/sse.ts:108-121):total_cost_usdpresent, finite,>= 0cost,cost_known: true,cost_provenance: provider-receipt,cost_scope: totalcost_known: false, no amountNaN/Infinitycost_known: false, no amountcost_scope: totalis correct because Anthropic's figure covers the wholeclaude -pinvocation, including every internal model call the harness made.It is a real provider receipt, so it goes in the receipt lane, not the estimate lane. The catalog-estimate lane is untouched: Pi keeps
costKnown: falseby contract (src/backends/types.ts:138) because neither Pi nor the proxy reports billed dollars.A run reporting no figure sends no dollar amount at all. A zero would read as a measured free turn.
Proof
New
tests/claude-cost-receipt.test.ts(5 tests). 4 of the 5 fail without the source change — verified by stashingsrc/backends/claude.tsand re-running:The 5th (catalog estimate stays out of the receipt lane) passes both before and after — it is the regression guard for the lane this change must not touch.
Full suite, after:
pnpm typecheckclean.tests/pi-backend.test.tsgains an explicit guard that no Pi delta carriescost_known: trueor acostamount.Docs
Two README bullets: the two-lane dollar contract, and what the
claudeharness now reports.