# The ledger

> Every model call reserves its maximum cost first and settles on what it actually used, so a budget is never overspent.

Nexus Gateway keeps one ledger per organization. Every model call, whether made by a product, a coding agent or your
own code, goes through it in two steps.

## Reserve, then settle

1. **Reserve.** Before the call reaches a provider, Gateway reserves its maximum possible cost: the request's size
   counted as input tokens, plus the most output it may produce (`max_tokens`). If the reservation doesn't fit in
   what is left of the organization's budget, a cap on the call's team, the person's
   [spend limit](/docs/concepts/spend-limits) or a product's cap, the call is refused with `402` and nothing is sent to
   the provider.
2. **Settle.** When the provider answers, Gateway charges what the call actually used, as the provider reported it, and
   releases the rest of the reservation. A call is never charged more than it reserved. A provider error charges
   the usage the provider reported for it, else nothing.

So the budget can't be overspent, even with many calls running at once: each one holds its maximum until it settles.

## What it means for you

- **Reservations are generous.** A call that may produce a lot of output reserves a lot, even if it ends up short.
  Your budget must cover the largest call you make, or that call is refused.
- **`nexus spend` shows both.** *Spent* is what settled; *reserved* is held by calls still running; *remaining* is
  the budget minus both. A member sees only what is left for them, and when it resets, never the organization's
  budget, spent or reserved.
- **Streams settle too.** For a streamed call, Gateway settles on the usage the provider reports at the end of the
  stream; if a stream is cut short, it settles on its input and an estimate of the output streamed so far.
- **A settled call is never charged twice.** With an [idempotency key](/docs/concepts/idempotency), a model call that
  settled is not run again. A retry after a failure runs again, and each attempt is charged for the tokens it used.
- **An unpaid invoice stops the calls Nexus pays for.** Past its grace period, such a call is refused with `402`
  and code [`payment_past_due`](/docs/api/errors#codes) before it reserves. Calls on your own provider keys keep
  running. An owner pays the invoice, and the other calls run again.
- **Who pays depends on the key.** A call on your own provider key costs you only what the provider bills you. A call
  on Nexus's own provider accounts costs the provider's price plus 8%. The free start pays first, for cheaper models
  only. Then your credits pay, then your card if an owner turned on paying after the fact. When none of them can pay,
  the call is refused with `402` and a code such as `credit_exhausted` or `usage_limit_reached`.

A call settles on what the provider reported: its cost, or its token counts at the provider's list prices.
