- Check with lock — atomically check access and hold balance
- Do work — run your operation
- Finalize — confirm the deduction, adjust it, or release the hold
Step 1: Check with lock
Pass thelock parameter to the check endpoint. This atomically checks if the customer has enough balance and reserves it in a single call.
Lock parameters
Step 2: Do your work
Run whatever operation you reserved balance for. The held balance is guaranteed to be available — no other concurrent request can consume it.Step 3: Finalize the lock
When the operation completes, callbalances.finalize to resolve the held balance.
Confirm the full amount
If the operation used exactly the amount you reserved, confirm the lock:Release the hold
If the operation failed or was canceled, release the lock to return the held balance:Adjust the final amount
If the actual usage differs from the reserved amount (common with AI tokens), passoverrideValue to adjust:
Use cases
AI completions
Reserve a token budget before starting generation, then finalize with the actual token count:Long-running jobs
Reserve credits before queuing a job, release if the job fails:Compared to check + track
For simple operations where you know the cost upfront and the operation is fast, check withsendEvent is simpler — it deducts immediately in one call.
Use reservations when:
- The final usage amount is unknown at check time (e.g., AI token counts)
- The operation can fail after balance is deducted
- The operation takes significant time and you don’t want another request to consume the same balance