All posts

Charging for a stream that might not finish

A streamed completion has an awkward property for anyone who bills for it: you learn the price last. By then the tokens are gone. With a prepaid balance, that raises two questions. How do you stop fifty concurrent requests from spending the same dollar? And what do you charge when the stream breaks in the middle?

Hold first, charge later

Before a request leaves the gateway, we reserve what it may cost: its input, at the highest input price, plus the output limit it asked for. The reservation and the balance check are one atomic step, so concurrent requests cannot both win. When the response ends, the hold is replaced by the real charge.

Every amount is an integer count of billionths of a dollar. Prices are per million tokens, and a request is rounded once, on its total. No floating point is involved anywhere.

When nobody reports the usage

If you disconnect, we stop the model and charge for what had been produced, counted from the content. Those lines are marked as estimated. If the model breaks off before it produced any output, you pay nothing.

Two records that must agree

The balance the gateway checks is a fast copy. The truth is an append-only ledger that the database itself refuses to update or delete. Each charge travels to the ledger through a stream, and the position in that stream moves in the same transaction as the ledger, so a crash can neither lose a charge nor book it twice.

Every hour we check that one equation holds for every wallet: credits, minus what is available, minus open holds, minus charges still on their way, equals what the ledger says was charged. It holds exactly, while traffic is flowing. If it ever does not, we want to know before you do.