Every credit system starts with the same three lines:

if balance >= estimated_cost: # 1. check response = await provider.call() # 2. act (a round trip) balance -= actual_cost # 3. debit 

It passes every test written by a human sending requests one at a time. Under concurrency it overspends — not from a typo or an off-by-one, but because every simultaneous request reaches step 1 before any of them reaches step 3. Each reads the same balance, each concludes there is room, each proceeds, each gets billed. The balance lands below zero.

Concurrency is the default traffic shape for an LLM gateway — agent swarms, batch pipelines, parallel tool calls. A spend control exercised only serially is unvalidated, not correct.

The repairs that still lose

  • Debit first, refund after — a crash between the two keeps the money.
  • Serialise everything — correct, and no longer a gateway.
  • Reconcile afterwards — detects the overspend; the provider has already billed you.
  • Shrink the window — a rarer race is a harder-to-reproduce race. Rarity is not safety.

One indivisible act

Reserve-and-settle makes the check and the commitment the same act. The maximum plausible cost is held against the balance before the provider is contacted. If the hold does not fit, the request is refused immediately (a 402, no provider called, nothing owed). After the call, the hold is replaced by the real cost and the difference returns. On any failure path, the whole hold returns.

Every hold settles or releases — no third outcome — and both ends are ledger rows, so the balance is the sum of its rows.

The operative figure becomes available balance: balance minus in-flight holds. It dips during a burst and recovers as calls settle, like a card authorisation hold. The estimate errs high on purpose: over-reserving costs an early, free refusal; under-reserving reopens the race.

The judgment calls are decided too: an unpriceable completed call settles at its hold rather than becoming free; a guardrail-blocked completion settles because the provider did the work; a fallback takes one hold for the whole attempt; a mid-stream disconnect settles what was generated.

And the property is testable the only way concurrency properties are: fire many simultaneous requests at a deliberately tiny balance, assert it never went negative, and assert the ledger rows sum to the final balance exactly.

Full post: https://nrouter.ai/blog/engineering/reserve-and-settle-credit-safety