> ## Documentation Index
> Fetch the complete documentation index at: https://docs.integrateledger.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Balances and spend order

> Ledger tracks every allowance and credit a customer holds, and spends whatever would expire soonest first so nothing paid for is lost.

Ledger keeps an exact balance for every customer and spends it in the order that wastes the least: what would expire soonest goes first. A balance is what their unexpired **grants** leave after what they've used. Every grant comes from a plan, a credit pack, a top-up, a grant code, a referral or a manual adjustment. Credits share one balance across every credits feature; metered and held features each have their own.

## Grants

| Grant | Arrives | Ends |
| - | - | - |
| Free plan allowance | On attach, then each month from the moment it was attached | End of its period |
| Paid plan allowance | When Stripe confirms the first payment, then when Stripe creates each renewal invoice | End of its period |
| Annual plan, monthly reset | The first month with the year's invoice; each later month on the customer's first `check` or `track` in it | End of its month |
| [Lifetime plan](https://docs.integrateledger.com/pricing/lifetime.md) | When the one payment is confirmed | Never, unless the plan is replaced or fully refunded |
| [Credit pack](https://docs.integrateledger.com/pricing/credit-packs.md) | When Stripe confirms the payment | Never |
| [Balance adjustment](https://docs.integrateledger.com/customers/balance-adjustments.md) | When you add it | When you choose, or never |
| [Grant code](https://docs.integrateledger.com/billing/discounts.md#grant-codes) or [referral](https://docs.integrateledger.com/billing/referrals.md) | When the code is redeemed, or at the referral's trigger | After the reward's `expiresAfter`, or never |
| [Entity allowance](https://docs.integrateledger.com/customers/entities.md) | Each billing period, while the customer has a plan | End of its period. Only that entity spends it |

A plan's grant is its `monthlyCredits` plus each metered item's `included`. A held item's `included` is a limit, not a grant: it applies for as long as the customer holds the plan, and the count never resets. Attaching a plan is what grants; a new customer starts with an empty balance.

## Customers without a plan

Credits work with or without a plan: credit packs and balance adjustments stand on their own and outlive any plan. What a customer without a plan can do depends on the feature's type:

| Feature type | With no plan |
| - | - |
| `credits` | Allowed while their credits cover the cost, spent in the usual [spend order](#spend-order-for-credits). Nothing bills as overage. When the credits don't cover it, `check` answers `reason: "no_plan"` and `track` refuses with `409 insufficient_credits`. |
| `metered`, `held`, `boolean` | Their allowances and limits come from a plan, even a free one. With none, `check` and `track` answer `no_plan`. |

So a signup bonus or a pack works before any plan is attached, for credits features, and a [free plan](https://docs.integrateledger.com/pricing/free-plans.md) gives metered allowances from signup.

## Per-feature balances

Metered and held features answer with a `balance` on `check`, `track` and `GET /v1/customers` (its `features` list):

| Field | Metered | Held |
| - | - | - |
| `included` | Units granted this period | The most the customer may hold |
| `used` | Units used this period, overage included | The count held now |
| `remaining` | What's left, after reservations | `included` minus `used`, at least 0 |
| `overageAllowed` | Whether a use past `remaining` bills as [overage](https://docs.integrateledger.com/pricing/overage.md) | `false` |
| `resetsAt` | When the soonest grant ends | `null`: held counts never reset |

`included` and `remaining` are `null` when the item is unlimited. Credits and boolean features have no `balance`: a credits feature's balance is `availableCredits`.

## Dollar balances

With the catalog's `creditsPerUsd` (100,000 is the recommended value: one credit is \$0.00001), the shared pool is a dollar balance: responses show `availableUsd` beside `availableCredits`, and `chargedUsd` beside `chargedCredits`. An `ai_tokens` feature with `balance: "own"` has its own dollar balance, and its `balance` adds `includedUsd`, `usedUsd` and `remainingUsd`. Grants, packs, spend order and expiry work exactly as below; amounts are still stored as whole credits. See [AI usage in dollars](https://docs.integrateledger.com/pricing/ai-credits.md).

## Spend order for credits

When `track` spends credits, Ledger spends whatever would be lost soonest first, then overage:

```mermaid theme={"system"}
%%{init: {"theme": "base", "themeVariables": {"fontFamily": "IBM Plex Sans, sans-serif", "fontSize": "14px", "primaryColor": "#f7f6f3", "primaryBorderColor": "#c4bfb5", "primaryTextColor": "#1a1816", "lineColor": "#6b655c", "edgeLabelBackground": "#ffffff"}}}%%
flowchart LR
    expiring["1 · Grants that expire<br/>soonest first"] -- "used up" --> lifetime["2 · Lifetime plan credits"]
    lifetime -- "used up" --> forever["3 · Reward credits, then packs,<br/>top-ups and adjustments, oldest first"]
    forever -- "used up" --> overage["4 · Overage<br/>up to the cap"]
    overage -- "cap reached" --> refused["Refused · 409"]
    style expiring fill:#eff2fd,stroke:#2443c4
```

1. **Grants that expire**, the one that expires soonest first: plan allowances at their period end, adjustments and grant codes when you chose. An adjustment that expires in 3 days is spent before an allowance that renews in 25, so it isn't lost unused. After an [upgrade](https://docs.integrateledger.com/billing/upgrades-and-downgrades.md), the old plan's leftover credits expire first, so they're spent before the new plan's. When two expire at the same moment, the plan allowance goes first, then the oldest.
2. **A lifetime plan's credits.** They never expire, but they're spent before packs and top-ups.
3. **Other grants that never expire**: reward credits from [grant codes](https://docs.integrateledger.com/billing/discounts.md#grant-codes) and [referrals](https://docs.integrateledger.com/billing/referrals.md) first, so free credits are used before paid ones; then credit packs, [top-ups](https://docs.integrateledger.com/pricing/top-ups.md) (by hand or automatic) and adjustments, oldest first. A refunded pack takes back its own unspent credits.
4. **Overage**, on a plan with [overage](https://docs.integrateledger.com/pricing/overage.md) terms, up to the customer's monthly cap.

With an `entityId`, the entity's own allowance goes first, then this order, with the entity's cap on everything past its allowance.

A use that costs more than one grant holds is split across grants in that order. A use is never split between credits and a refusal: if the balance and overage together can't cover all of it, `track` refuses it and charges nothing. The one exception is a model call on an `ai_tokens` feature, which has already happened: it is charged as far as the balance and overage cover, and the rest is reported as `uncoveredCredits`. Metered uses follow the same all-or-nothing rule against their allowance and overage.

## Worked example

A customer on Pro (1,000 credits a month) has bought one 500-credit pack, then spends 1,200 credits this period:

| Grant | Granted | Spent | Left |
| - | - | - | - |
| Pro allowance, Nov 7 – Dec 7 | 1,000 | 1,000 | 0 |
| Top-up pack | 500 | 200 | 300 |
| **Balance** | | | **300** |

On Dec 7 Pro renews: a new allowance of 1,000 arrives and the old one expires. The balance becomes 1,300, and the pack's 300 credits wait until the new allowance is spent.

## Reservations

`track` decides each use in its own transaction, so the last unit is spent exactly once, however many requests race for it. To guarantee capacity for work that's already running, a [check with `reserve`](https://docs.integrateledger.com/customers/reservations) holds credits or metered units while work runs. Credit holds come from the grants that last longest, so ordinary tracks during the run still spend what would be lost soonest. While a hold is active, `availableCredits` and `remaining` are after holds, and `GET /v1/customers` also returns `heldCredits`.

## Next steps

> [card] [Check and track](https://docs.integrateledger.com/customers/check-and-track.md)
>
> Gate features on these balances and record each use once.

> [card] [Usage events](https://docs.integrateledger.com/concepts/usage-events.md)
>
> The record each tracked use leaves.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.