DEV Community

nRouter
nRouter

Posted on

Your balance is derived, not stored: reading a credit ledger

Most billing surfaces show you a number and ask you to trust it. A ledger does the opposite: the number is the arithmetic of things that happened, and every one of those things is a row you can point at.

On a gateway that keeps a credit ledger, the balance is derived, not stored — the running total of a ledger where every movement of value is a row. The practical payoff arrives the first time somebody asks where a small amount went: the answer is a lookup, not a reconstruction.

Quote available, not balance

The balance card carries four figures:

  • Available — balance minus in-flight reservations. What you can actually spend right now. Quote this one, always.
  • Lifetime purchased — everything ever bought. For reconciling against invoices.
  • Credits used — cumulative draw-down. For explaining the period.
  • Reserved — held against requests in flight. For investigating the gap between the other figures.

A dashboard that displays the raw balance overstates spending power by exactly the amount currently in flight — which under load is not a rounding error.

Five rows, one mechanism

Every row is one of five: a top-up, a reservation (a hold placed before the call is forwarded), a settlement (the true-up from the hold to the provider's real cost), a release (the hold handed back because the call never completed), and the platform-fee row — no fee on credit purchases here, and usage settles at the served model's exact list price, which is what makes the per-call figure literally true.

The ordering is the mechanism. Because the hold precedes the call, two concurrent requests cannot both fit under the same last unit of credit: the first takes it, the second is refused before any provider is contacted. That is why a spend ceiling built on reservations is exact rather than approximate.

Two consequences surprise people. One call is often two rows — hold plus settlement, netting to the real cost; sum only the negative rows and you overstate spend. A failed call leaves rows but no cost — reservation and release, cancelling — while a request refused at pre-flight (budget, rate limit, guardrail) never reaches a provider and leaves no spend row at all.

Reconcile against the ledger

Two identities: lifetime purchased − credits used = balance, and balance − reserved = available. If both hold, every unit is accounted for.

When the cost report disagrees slightly, the cause is structural: enforcement runs off the ledger, the charts are drawn from observability logs, and in-flight reservations explain most gaps. Trust the ledger for invoices and budget arguments. And the reporting trap that corrupts more dashboards than any other: when a call cannot be priced, its cost is absent, status unpriced — never a zero. Default absent to zero in your own code and your reports will quietly under-report what the ledger knows.

The ledger tells you the money moved; the request log — searchable by the request ID on every response — tells you what moved it, with the provider's own token counts. Content is never in the log; metadata is, deliberately.

Full guide: https://nrouter.ai/blog/guides/reading-a-spend-ledger

Top comments (0)