Skip to content

billed-but-unrecorded: a cut stream is provider money the ledger never sees — reconcile against the provider's receipt #161

Closed

Description

@santoshkumarradha

Context: an audit that found no bug, and one gap

Reconciling the 2026-08-31 dogfood run's $102.00 against OpenRouter's dashboard:

  • The conversation meter, the machine ledger (~/.aforge/v3/usage.jsonl), and the fold-at-close all agree to the cent: $102.00 = $74.10 glm-5.3 + $27.89 kimi across 1,333 calls in the run's workspace.
  • Costs are banked from the provider's own usage block at decode time (internal/provider/billing.go) — there is no local price table to drift.
  • The apparent day-level mismatch against the provider dashboard dissolved on inspection: the ledger file's first record is 12:44 PM that day (new file), so everything earlier on the provider's chart — including a ~$28 hour the evening before — predates the ledger and belongs to earlier sessions, not to a counting bug. Within the window both ledgers cover, the numbers agree within reading error of the dashboard's bars.

So: no bug in what the ledger records. But the audit surfaced the class it deliberately does not record.

The gap: a cut stream is provider money the ledger never sees

billing.go is explicit and principled: a response with no usage block is not written down — "guessing at a number is worse than a gap, and the gap is visible as a call the ledger did not see rather than as money the ledger invented."

The problem is that the gap is not actually visible anywhere a person looks, and it is not hypothetical. In one run:

  • Two streams ran past the 5-minute patience and were cut (76k and 79k prompt tokens each) — the provider processed those prompts and billed them; no usage block ever arrived.
  • One reply "stopped being language" and was cut mid-stream (53k prompt tokens) — same shape.
  • Hedged calls: the losing leg of a hedge is cut before its usage block; hedge_waste_usd estimates some of this ($0.70 that run) but an estimate is exactly the guessing the file forswears.

Magnitude that run: small (order $1). But the class scales with exactly the runs where money matters most — long, retry-heavy, hedged, multi-provider — and it biases the ledger in the flattering direction (under-reporting true spend).

The fix (principled, not a guess)

OpenRouter returns a generation id on every response, streams included, and serves an exact receipt at GET /api/v1/generation?id=… — actual native token counts and the actual billed cost, shortly after the call ends. So:

  • When a call ends without a usage block (cut, hedge-loser, transport failure after tokens flowed), remember its generation id and reconcile against the provider's receipt in the background; write the ledger row late, marked as reconciled, rather than never.
  • A receipt that cannot be fetched leaves today's behavior — but now the ledger writes an explicit unbilled marker row, so the gap the comment promises is visible really is: /cost and the spend place can say "N calls could not be billed" instead of silently omitting them.
  • hedge_waste_usd then stops being an estimate where a receipt exists.

Keeps the file's own law (never invent money) while closing the only path where the provider knows a number aforge refuses to learn.

Acceptance

  • A cut-stream fixture produces a reconciled ledger row with the provider's billed figure (or an explicit unbilled marker when the receipt is unreachable), and a test pins each path.
  • Hedge losers reconcile the same way where receipts exist.
  • /cost surfaces the unbilled-call count when it is nonzero (emptiness law otherwise).

Drafted with CodeAF · reviewed and owned by the author

Activity

  1. santoshkumarradha commented on Sep 1, 2026

    @santoshkumarradha
    MemberAuthor

    Check against #269 before building — its fix probably resolves the gap named here.

    This issue's audit found the ledger correct to the cent and named one gap: money on a cut stream that the ledger never sees. #269 ("one ledger, one number", live in the ledger lane now) has the same shape from the other end: spend is banked per call but journaled per turn, so an interrupted turn's calls are billed and never sealed — measured as $0.087 missing between the call log's $0.2438 and the machine ledger's $0.157 at one instant.

    A cut stream is an unsealed turn. If #269's fix seals on interrupt, the receipt gap here closes with it and the provider-reconciliation work becomes verification rather than a build.

    Recommended: do not open a lane on this. When #269 lands, re-run the reconciliation against the provider dashboard; close this if the numbers meet, keep it open with fresh evidence if they do not.

  2. added
    bug
    on Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions