Skip to content

Cancelled calls keep the money that was measured - #2302

Merged
santoshkumarradha merged 25 commits into
app/v3from
c52/billed-no-cost
Nov 3, 2026
Merged

santoshkumarradha merged 25 commits into
app/v3from
c52/billed-no-cost

Conversation

@santoshkumarradha

Copy link
Copy Markdown
Member

What changes

A cancelled, failed or timed-out model call can still have been billed. This keeps what was measured.

  1. Measured usage from a stream stays as a fallback when a final provider receipt can't be recovered. It goes through the existing ledger path, so it is booked once.
  2. A final receipt with a price still wins, including an explicit zero.
  3. A receipt with tokens but no price no longer replaces measured money. Before: measured spend lost: got $0.00, want $0.07.
  4. Token-only usage is marked as having no price, distinct from a measured zero.
  5. Call log and usage rows carry the provider's generation id, helper calls included, so a later audit can match them.
  6. Failed raw stream finishes now settle.

Validation

  • Red first: TestTokenOnlyReceiptKeepsMeasuredSpend failed with the line above before the fix.
  • TestCancelledHelperRetryBooksEachAttemptOnce: a cancelled helper retry writes exactly two paid rows.
  • internal/provider, internal/session, internal/manual, cmd/codeaf, go vet: pass, apart from the known red tests on the base. TestAppChatsExistingBeforePairL1 failed once on cleanup in the full run and passed alone three times.
  • No live model run of this fix.

Limits

  • Old runs can't be repaired. Calls logged without a generation id can't be matched to a provider receipt afterwards.
  • A missing price in the call log doesn't prove missing ledger money. An audit of nine such priced gaps found each already booked once as a reconciled ledger row: $0 lost from the ledger. Compare the ledger before adding adjustments, or they double count.
  • Text estimates remain telemetry and are never booked as spend.

🤖 Generated with Claude Code

santoshkumarradha and others added 25 commits November 3, 2026 03:28
Interim usage was suppressed when session callers had no billing sink. Preserve it as the fallback for the existing reconciliation door and retain provider generation identities for helpers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Raw StreamComplete treated an error finish as a complete billed answer, suppressing the reconciliation sink. Use the same success predicate for billing and usage completion.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The archived worker finding is false as a ledger-loss claim: all nine provider adjustments already exist as reconciled usage rows, including both charges totaling $0.0167028 on 2-old. Independent scripted tests exposed the interim and raw-stream settlement defects fixed here.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Use receipt price presence to retain measured interim spend once while allowing real receipt prices, including zero, to win.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
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