Skip to main content
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

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: So a signup bonus or a pack works before any plan is attached, for credits features, and a free plan 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): 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.

Spend order for credits

When track spends credits, Ledger spends whatever would be lost soonest first, then overage:
  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, 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 and referrals first, so free credits are used before paid ones; then credit packs, top-ups (by hand or automatic) and adjustments, oldest first. A refunded pack takes back its own unspent credits.
  4. Overage, on a plan with overage 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: 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 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

Check and track

Gate features on these balances and record each use once.

Usage events

The record each tracked use leaves.