Skip to main content
Ledger gates every feature and meters every use with two calls from your backend, and records each use exactly once however often you retry. check before the action asks whether the customer can use the feature now, and track after it succeeds records the use. Track is the meter: it spends credits, counts metered units, changes a held count, or logs a boolean use, depending on the feature’s type. For work that costs you real money to run, reserve with the check and settle with the track. Both calls read everything the customer holds: their plan plus every recurring add-on. A feature is on if the plan or any add-on has it, allowances and limits add up across them (an add-on’s times its quantity), and overage follows the base plan’s terms. The examples use the Quickstart’s chat app: messages is metered (Pro includes 500 a month), projects is held (Free allows 3) and sso is boolean. A credits feature, such as image_generation at 5 credits, works the same way.

Check, run, track

Check returns the decision and the feature’s balance:
Response
Track returns what it applied and the balance after it:
Response
  • check never spends. A refusal is allowed: false with a 200, a reason and the customer’s plan, so the offer can name it (“Upgrade from Free”) without another call.
  • Track only what succeeded, so customers pay only for work that ran.
  • balance is null for credits features (availableCredits is that balance) and boolean features. remaining is after reservations. overageAllowed says whether a use past remaining would bill as overage.

Credit fields on other feature types

The SDK methods are checkCredits and trackUsage for every feature type; the names come from the API’s operation IDs. Every response also carries the credit fields, whatever the feature:
  • availableCredits is the customer’s credit pool, plan, pack and adjustment credits together. On a credits feature, it is that feature’s balance.
  • creditCost (check) and chargedCredits (track) are what this use spends from that pool: always 0 for metered, held and boolean features, which keep their own balances.
So a metered track with chargedCredits: 0 did charge: the use is in balance.used, and chargedQuantity says how many units it counted. Read featureType to know which fields matter.

How each feature type answers

A use past what’s left is all or nothing, for credits and metered alike: it is allowed whole (spending what’s left and recording the rest as overage), or refused whole, with nothing charged.
  • Model calls. On an ai_tokens feature, send the call’s model (anthropic/claude-sonnet-4-5) and tokens (input, output, cacheRead, cacheWrite, …) instead of quantity. Ledger prices them at the model’s rates plus your markup; the response adds chargedUsd, availableUsd and an ai block with the cost and markup. On check they’re an estimate. A call the balance can’t fully cover is charged as far as it fits and reports the rest as uncoveredCredits. See AI usage in dollars.
  • Unlimited. A plan item with unlimited: true always allows. unlimited is true in the response, a credits feature’s creditCost is 0, and balance has included and remaining set to null while used still counts.
  • Track gates held features itself. An add past the limit is refused with 409 limit_reached and changes nothing, so a separate check before it is optional (Limits, Concurrency).
  • Held removes always work. A remove at 0 applies 0 (chargedQuantity: 0). A held count above the limit, after a downgrade say, is kept, and removes bring it back under; adds resume once it is.
  • Set a held count outright with count instead of quantity, to reconcile with your app. It applies even above the plan’s limit, since it reports what exists.

Request IDs

The request ID makes track idempotent.
  • Use an ID you already store with the action: a message ID, a job ID, or a UUID you save with it. Up to 255 characters. Over HTTP the field is request_id.
  • Retry with the same ID. If a track response is lost, send it again with the same request ID. Ledger returns the original result with "idempotent": true and charges nothing more. A new ID records a second use.
  • One ID, one feature. Request IDs are one namespace across every feature and type, so each ID names exactly one use of one feature.
Ledger stamps each use itself when it records it, and the use counts toward the billing period current then. A track sent after a renewal spends the new period’s allowance. Every request, allowed or refused, lands in the usage log. See Usage events.

Refusals

A refused check answers 200 with allowed: false and one reason. A refused track answers 409 with the same code as error, and charges nothing. Ledger asks in this order and stops at the first that fails: Treat any other reason as insufficient_credits. A refused check’s plan is { "id", "name", "status" }, or null with no_plan.
Response
Errors lists every code.

Handle a refused track

A plain check answers for that moment, so another request can spend the last of a balance between your check and your track. track decides again in its own transaction and can refuse with a 409. Nothing is charged, and the same request ID can succeed later, for example after the customer buys a pack. If a refusal after the work has run would cost you money, reserve with the check instead.

When Ledger is unreachable

The SDKs fail open by default: check is allowed and track is replayed later with the same request ID, both marked failedOpen: true, so an outage never blocks your users. Pass failOpen: false (Python fail_open=False) to handle outages yourself, for example to refuse paid work with a 503 before it runs. A check with reserve raises instead, because a reservation guards expensive work. Usage in the dashboard lists every tracked request, charged or refused, with the reason.
Other ways. Without an SDK, call POST /v1/check and POST /v1/track.

Rules

  • Call Ledger from your backend, never from a browser or mobile app, so your API key stays secret.
  • Leave quantity out on boolean features.
  • Branch on the code, reason on a check and error on a track, never the message; treat an unknown one as insufficient_credits.
  • Reusing a request ID for another feature or entity returns 409 request_id_conflict.
  • A customer or feature outside the key’s environment returns 404 customer_not_found or feature_not_found.