> ## 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.

# Reservations

> Hold credits or metered units before expensive work starts and settle with the real amount, so parallel jobs never overspend a balance.

Reservations make sure expensive work is paid for before it starts. The [check](https://docs.integrateledger.com/customers/check-and-track.md) holds the credits or units up front, so ten parallel GPU jobs, video renders or agent runs can never spend the same balance twice, and the track settles with the real amount once you know it. For work that takes a while, costs you real money, or whose size you only know at the end, **check reserves and track settles**:

1. **Check reserves.** [Check](https://docs.integrateledger.com/api-reference/usage/check.md) with a `request_id` and `reserve`. Allowed, it holds the amount so no other check or track can spend it, and returns a `reservation`. Refused, it is a normal check's `allowed: false` with a `reason`, and nothing is held.
2. **Run the work.**
3. **Track settles.** [Track](https://docs.integrateledger.com/api-reference/usage/track.md) with the same `request_id` and the real amount. It charges that and lets the rest of the hold go. If the work failed, [release](https://docs.integrateledger.com/api-reference/reservations/release.md) the reservation instead.

Reserve on credits and metered features.

```mermaid theme={"system"}
%%{init: {"theme": "base", "themeVariables": {"fontFamily": "IBM Plex Sans, sans-serif", "fontSize": "14px", "primaryColor": "#f7f6f3", "primaryBorderColor": "#c4bfb5", "primaryTextColor": "#1a1816", "lineColor": "#6b655c", "clusterBkg": "#ffffff", "clusterBorder": "#dbd7cf", "edgeLabelBackground": "#ffffff"}}}%%
sequenceDiagram
    participant App as Your backend
    participant L as Ledger
    App->>L: check(requestId, reserve: 250 seconds)
    alt allowed is false
        L-->>App: refused with a reason, nothing held
        Note over App: Don't start. Offer an upgrade
    else allowed is true
        L-->>App: reservation held
        Note over App: Run the work on the held 500 credits
        alt the work succeeded
            App->>L: track(requestId, quantity: 160)
            L-->>App: 320 charged, 180 back
        else the work failed
            App->>L: release reservation
            L-->>App: nothing charged, 500 back
        end
    end
```

## Reserve credits

`video_render` is a credits feature priced by the second of video, `creditCost: 2`. The check reserves an estimate in seconds, and the track settles with the seconds that rendered:

```typescript TypeScript theme={"system"}
// 1. Check and reserve an estimate before the GPU starts.
const { allowed } = await billing.checkCredits({
	customerId: user.id,
	featureId: "video_render",
	requestId: job.id,
	reserve: { quantity: 250, expiresInSeconds: 1800 }, // 500 credits
});
if (!allowed) {
	return showUpgradeOffer();
}

// 2. Run the job. If it fails, give the whole hold back.
let render;
try {
	render = await runRender(job);
} catch (error) {
	await billing.releaseReservation({ requestId: job.id, customerId: user.id });
	throw error;
}

// 3. Track settles: charge the real seconds, and the rest of the hold goes back.
await billing.trackUsage({
	customerId: user.id,
	featureId: "video_render",
	requestId: job.id,
	quantity: render.seconds, // 160 seconds: 320 credits
});
```

```python Python theme={"system"}
# 1. Check and reserve an estimate before the GPU starts.
decision = billing.check_credits(
    customer_id=user.id,
    feature_id="video_render",
    request_id=job.id,
    reserve={"quantity": 250, "expires_in_seconds": 1800},  # 500 credits
)
if not decision["allowed"]:
    return show_upgrade_offer()

# 2. Run the job. If it fails, give the whole hold back.
try:
    render = run_render(job)
except Exception:
    billing.release_reservation(customer_id=user.id, request_id=job.id)
    raise

# 3. Track settles: charge the real seconds, and the rest of the hold goes back.
billing.track_usage(
    customer_id=user.id,
    feature_id="video_render",
    request_id=job.id,
    quantity=render.seconds,  # 160 seconds: 320 credits
)
```

```bash cURL theme={"system"}
# 1. Check and reserve an estimate before the GPU starts.
curl https://api.integrateledger.com/v1/check \
  -H "Authorization: Bearer $LEDGER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"customerId": "user_123", "featureId": "video_render", "request_id": "job_123", "reserve": {"quantity": 250, "expiresInSeconds": 1800}}'

# 3. Track settles with the real seconds.
curl https://api.integrateledger.com/v1/track \
  -H "Authorization: Bearer $LEDGER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"customerId": "user_123", "featureId": "video_render", "request_id": "job_123", "quantity": 160}'

# Or, if the job failed, release the hold:
curl -X POST "https://api.integrateledger.com/v1/reservations/job_123/release?customerId=user_123" \
  -H "Authorization: Bearer $LEDGER_API_KEY"
```

Both amounts are `quantity`, in the feature's own units, and Ledger applies `creditCost`. The check holds `reserve.quantity × creditCost` credits (`reserve.quantity` defaults to the check's `quantity`, else 1). The track charges `quantity × creditCost`, or the reserved quantity when you leave `quantity` out, and returns `settled: true`.

When you price the work yourself, such as model tokens at cost or GPU time, give the feature `creditCost: 1` and send the cost in credits as `quantity` on both calls. [Prepaid dollar balance](https://docs.integrateledger.com/pricing/dollar-balance.md) shows it.

## Reserve metered units

A [metered feature](https://docs.integrateledger.com/pricing/metered.md) (500 messages a month) reserves from its own allowance, not from credits:

* **Check reserves** `reserve.quantity` units, or the check's `quantity` when you leave it out. Other checks and tracks then see that many fewer `remaining` in `balance`, and the credit balance doesn't move. Past the remaining allowance, an item with an [overage price](https://docs.integrateledger.com/pricing/overage.md) holds the rest as overage under the customer's one cap, which counts it until the hold ends. Without one, or past the cap, the check returns `allowed: false` with `limit_reached` or `overage_cap_reached` and holds nothing.
* **Track settles** with the same `request_id` and `quantity`, the real units (the reserved units when you leave it out). It returns `settled: true` and `chargedQuantity`.

```typescript TypeScript theme={"system"}
await billing.checkCredits({
	customerId: "user_123",
	featureId: "messages",
	requestId: "batch_7",
	reserve: { quantity: 10 },
});

// ... send the messages, then charge the 6 that went out:
await billing.trackUsage({
	customerId: "user_123",
	featureId: "messages",
	requestId: "batch_7",
	quantity: 6,
});
```

```python Python theme={"system"}
billing.check_credits(
    customer_id="user_123",
    feature_id="messages",
    request_id="batch_7",
    reserve={"quantity": 10},
)

# ... send the messages, then charge the 6 that went out:
billing.track_usage(
    customer_id="user_123",
    feature_id="messages",
    request_id="batch_7",
    quantity=6,
)
```

```bash cURL theme={"system"}
curl https://api.integrateledger.com/v1/check \
  -H "Authorization: Bearer $LEDGER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"customerId": "user_123", "featureId": "messages", "request_id": "batch_7", "reserve": {"quantity": 10}}'

# ... send the messages, then charge the 6 that went out:
curl https://api.integrateledger.com/v1/track \
  -H "Authorization: Bearer $LEDGER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"customerId": "user_123", "featureId": "messages", "request_id": "batch_7", "quantity": 6}'
```

## How a reservation ends

| What happens | Call | Charged | Back to the balance |
| - | - | - | - |
| The work succeeded | `track` with the real `quantity` | the real amount | the rest of the hold |
| The work failed | release the reservation | nothing | the whole hold, at once |
| Nobody calls anything | none: it expires after `expiresInSeconds` | nothing | the whole hold |

## Rules

* **The reservation is the gate.** A reserve is decided like a track of that amount, in one transaction, after what other reservations already hold, in [spend order](https://docs.integrateledger.com/concepts/balances.md#spend-order-for-credits) for credits, and the allowance, then [overage](https://docs.integrateledger.com/pricing/overage.md), for metered. Two checks reserving the last of a balance at once: one is allowed, the other refused.
* **Settling up to the hold always succeeds**, because the work already ran. More than the hold must fit like a track; otherwise it returns `409 insufficient_credits`, `limit_reached`, `overage_cap_reached` or `entity_cap_reached`, and the reservation stays held, so you can track again for at most the held quantity.
* **Settling at nothing.** `quantity: 0` settles a held reservation without charging, for work that cost nothing (a cached answer, say), and needs no release. On a track with no held reservation, `quantity: 0` is `400 invalid_request`.
* **Uncovered amounts.** If the grants under a hold ended while it was held (a cancellation, say), the part nothing could cover is returned as `uncoveredCredits` and not charged. A metered settle past the allowance with no overage is returned the same way.
* **Which credits it holds:** the ones that last longest (a credit pack before a plan allowance that ends with the period). Other tracks during the work still spend the credits the customer would lose soonest, and the hold outlasts a plan period.
* **Expiry.** A reservation that is neither settled nor released lapses after `reserve.expiresInSeconds` (an hour by default, at most 7 days), counted in the customer's time, and its amount comes back by itself. A track after that charges like one without a reservation and returns `reservationExpired: true`.
* **Period.** A settle is charged when it happens. A reservation held across a renewal is charged, and counted toward the overage cap, in the new period.
* **Idempotent.** The same check with the same `request_id`, feature, entity and amount returns the same reservation with `idempotent: true`; other values return `409 request_id_conflict`. A repeated settling track replays its result; one with another `quantity` returns `409 reservation_already_settled`. Tracking a released reservation's `request_id` returns `409 reservation_released`, and releasing a settled one returns `409 reservation_already_settled`.
* **Busy customers.** Reserving, settling and releasing wait their turn behind the customer's other billing changes, as track does. `503 customer_busy` means that wait passed 1.5 seconds; nothing was held or charged, so retry after `Retry-After` (1 second) with the same `request_id`.
* **Reserving checks raise when Ledger is unreachable.** Unlike a plain check, which [fails open](https://docs.integrateledger.com/fail-open.md), a check with `reserve` raises, because a reservation guards expensive work: start the work once Ledger answers. A settling track fails open and is replayed with the same `request_id`, like any track.
* **A reserve holds at least 1.** `reserve.quantity` is a whole number above 0; `0` is `400 invalid_request`. To end a hold without charging, settle it with `quantity: 0` or release it.
* **Reserve on credits and metered features.** Held and boolean features return `400 reserve_not_supported`.
* **Balances.** While a reservation holds credits, `availableCredits` everywhere is the balance after holds (a hold that crosses your low-balance threshold sends `balance.low`), and [Get a customer](https://docs.integrateledger.com/api-reference/customers/get.md) also returns `heldCredits`. A hold larger than what's left holds overage under the cap, which counts toward the cap until it ends.
* **Entities.** Pass `entityId` on both the check and the track to reserve credits for one [entity](https://docs.integrateledger.com/customers/entities.md): its allowance first, then the shared credits under its cap.
* **Permissions.** Reserving needs the **Credits** permission at Track (which includes Check). [Reading](https://docs.integrateledger.com/api-reference/reservations/get.md) one needs Check; releasing needs Track.


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