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
Response
Response
checknever spends. A refusal isallowed: falsewith a200, areasonand the customer’splan, so the offer can name it (“Upgrade from Free”) without another call.- Track only what succeeded, so customers pay only for work that ran.
balanceisnullfor credits features (availableCreditsis that balance) and boolean features.remainingis after reservations.overageAllowedsays whether a use pastremainingwould bill as overage.
Credit fields on other feature types
The SDK methods arecheckCredits 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:
availableCreditsis the customer’s credit pool, plan, pack and adjustment credits together. On a credits feature, it is that feature’s balance.creditCost(check) andchargedCredits(track) are what this use spends from that pool: always 0 for metered, held and boolean features, which keep their own balances.
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_tokensfeature, send the call’smodel(anthropic/claude-sonnet-4-5) andtokens(input,output,cacheRead,cacheWrite, …) instead ofquantity. Ledger prices them at the model’s rates plus your markup; the response addschargedUsd,availableUsdand anaiblock with the cost and markup. Oncheckthey’re an estimate. A call the balance can’t fully cover is charged as far as it fits and reports the rest asuncoveredCredits. See AI usage in dollars. -
Unlimited. A plan item with
unlimited: truealways allows.unlimitedistruein the response, a credits feature’screditCostis 0, andbalancehasincludedandremainingset tonullwhileusedstill counts. -
Track gates held features itself. An add past the limit is refused with
409 limit_reachedand 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
countinstead ofquantity, to reconcile with your app. It applies even above the plan’s limit, since it reports what exists.
Request IDs
The request ID makestrack 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
trackresponse is lost, send it again with the same request ID. Ledger returns the original result with"idempotent": trueand 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.
Refusals
A refused check answers200 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
Handle a refused track
A plaincheck 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
quantityout on boolean features. - Branch on the code,
reasonon a check anderroron a track, never the message; treat an unknown one asinsufficient_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_foundorfeature_not_found.