Enforce a per-run cost ceiling at dispatch. Before an attempt starts, check whether settled cost plus open reservations plus the new attempt’s maximum can fit under the limit. Reserve that amount in the same operation that admits the attempt. Settle the reservation from authoritative usage when it arrives. Keep it open for an ambiguous timeout until you can account for that attempt.
Define what the ceiling can actually cover
Choose a currency and cost categories. For each type of call, identify controllable bounds on billed units, including input, generated output, retries and any separate tool charges covered by the cap. Attach the rate version and unit limits to the reservation. Put any attempt with an unknown maximum outside the capped path or bring it under an enforceable limit. A number calculated from typical usage can estimate cost. A hard ceiling depends on the maximum you can impose.
The cap check and attempt reservation need to commit together, so workers racing for the same balance see a consistent admission decision. PostgreSQL’s transaction documentation describes the all-or-nothing and visibility properties that such an implementation can use. Include the appropriate concurrency condition, such as updating the budget row under a lock before inserting the attempt reservation. PostgreSQL transaction tutorial
Walk a parallel run through a small budget
Suppose a synthetic catalog cleanup run has a 100-cent ceiling for model calls. Completed attempts have cost 30 cents and another attempt holds 30 cents. Two workers independently request new reservations of 25 cents each.
| Event, in committed order | Settled | Open reservations | Available |
|---|---|---|---|
| Before either request | 30¢ | 30¢ | 40¢ |
| Worker A receives a 25¢ reservation | 30¢ | 55¢ | 15¢ |
| Worker B requests 25¢ and is refused | 30¢ | 55¢ | 15¢ |
| The original open call settles at 18¢ | 48¢ | 25¢ | 27¢ |
| Worker B receives a 25¢ reservation | 48¢ | 50¢ | 2¢ |
Use separate identities for a logical workflow action and each billable attempt. A restarted worker can look up its existing attempt reservation before dispatching. An additional provider call needs its own accounting unless the provider can establish that it reuses the original billed attempt. A timeout with absent usage cannot release money safely if the provider might still have billed the call. Route the reservation into reconciliation and show the operator how much remains held. For evaluating longer-run economics, include rework and human review in a separately defined cost view. sinc-LLM guide to workflow cost components
Force competing requests and a delayed usage record
Fire two authorized test workers against a ledger with room for only one new request. Keep the requested amount fixed and assert that exactly one reservation commits before either provider call. Then simulate a response loss followed by late usage. The reservation should stay visible through the gap, settle once against that attempt, and leave enough evidence to explain the balance after a worker restarts.
Retain a settlement reference unique to the attempt. Replay the same usage record after restart and confirm it does not debit the ledger again. If the provider can bill above the reserved amount, report that limitation before treating this design as a hard ceiling.
Start with one work queue and one cost category whose rates and maximum units you can pin. The resulting ledger gives operators a checkable cost limit and an explicit place for unknown usage. Extend it to other charges only when those charges can be bounded and observed.
sinc-LLM