# Reservations

> Reserve what work will probably cost, then commit the actual amount when it finishes.

Some work only knows its cost afterwards, like an AI reply your product pays for by the token. A reservation sets aside what the work will probably cost while it runs, then you commit the actual amount. The examples on this page reserve 5 cents for a user’s `ai-call` against a budget of 200 cents a month, and commit what the call cost.

## In one step

The SDKs’ `reserve` reserves `cost` and runs your code. It then commits the cost your code reports. If your code fails, it releases the reservation instead.

* TypeScript

  ```ts
  import { createClient } from "@limitry/sdk";

  // Uses the LIMITRY_API_KEY environment variable.
  const limitry = createClient();

  const reply = await limitry.reserve(
    { subject: { kind: "user", id: "user_123" }, action: "ai-call", cost: 5 },
    async (reservation) => {
      const response = await callModel(prompt);
      reservation.cost(priceCents(response.usage)); // your prices, in cents
      return response;
    },
  );
  console.log(reply);
  ```

* Python

  ```python
  from limitry import Limitry

  # Uses the LIMITRY_API_KEY environment variable.
  limitry = Limitry()

  with limitry.reserve(
      subject={"kind": "user", "id": "user_123"}, action="ai-call", cost=5
  ) as reservation:
      response = call_model(prompt)
      reservation.cost = price_cents(response.usage)  # your prices, in cents
      print(response.text)
  ```

The steps below are what `reserve` does, for when you reserve, commit and release yourself.

## 1. Reserve

Make a check with `mode: "reserve"` and what the work will probably cost as the `cost`:

* TypeScript

  ```ts
  import { createClient } from "@limitry/sdk";

  // Uses the LIMITRY_API_KEY environment variable.
  const limitry = createClient();

  const decision = await limitry.checks.create({
    subject: { kind: "user", id: "user_123" },
    action: "ai-call",
    cost: 5,
    mode: "reserve",
  });
  console.log(decision);
  ```

* Python

  ```python
  from limitry import Limitry

  # Uses the LIMITRY_API_KEY environment variable.
  limitry = Limitry()

  decision = limitry.checks.create(
      subject={"kind": "user", "id": "user_123"},
      action="ai-call",
      cost=5,
      mode="reserve",
  )
  print(decision)
  ```

* CLI

  ```bash
  limitry checks create --subject '{"kind":"user","id":"user_123"}' \
    --action ai-call --cost 5 --mode reserve
  ```

* curl

  ```bash
  curl -X POST https://api.limitry.com/v1/checks \
    -H "Authorization: Bearer $LIMITRY_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{ "subject": { "kind": "user", "id": "user_123" }, "action": "ai-call",
          "cost": 5, "mode": "reserve" }'
  ```

Limitry decides it like any other check. On a yes, Limitry reserves the cost, and other checks for that subject see it as used. The response includes the reservation.

```json
{
  "allowed": true,
  "limits": [{ "limitId": "lmt_…", "remaining": 195, "…": "…" }],
  "reservation": { "id": "rsv_…", "expiresAt": "2026-10-04T12:05:00Z" }
}
```

On a no, Limitry reserves nothing, and `reservation` is `null`.

## 2. Commit or release

When the work finishes, commit what it actually used. Here it used 3 cents.

* TypeScript

  ```ts
  await limitry.reservations.commit("rsv_…", { cost: 3 });
  ```

* Python

  ```python
  limitry.reservations.commit("rsv_…", cost=3)
  ```

* CLI

  ```bash
  limitry reservations commit rsv_… --cost 3
  ```

* curl

  ```bash
  curl -X POST https://api.limitry.com/v1/reservations/rsv_…/commit \
    -H "Authorization: Bearer $LIMITRY_API_KEY" \
    -H "Content-Type: application/json" -d '{ "cost": 3 }'
  ```

If the work used less than it reserved, Limitry gives the rest back at once. The user’s budget shows 197 cents left, not 195. If the work used more, Limitry records the actual cost in full, even past the limit, because the work did happen. The next check then sees the true total.

If the work didn’t happen, release the reservation instead. `reservations.release(id)` gives all of it back. On the CLI, that’s `limitry reservations release <reservation-id>`.

A commit or release is safe to retry, and the same one gets the same response. Committing again with a different cost gets a `400`. Releasing a committed reservation, or committing a released one, gets a `409` with the code [`conflict`](/docs/errors/#conflict).

## Expiry

A reservation lasts the `ttlSeconds` you set on the check. The default is 300, and the most is 3,600. A reservation that nobody commits or releases gives itself back when it expires, so a crashed worker never keeps a subject blocked. If the work did finish after all, a late commit within 24 hours still charges the full cost it reports.

For longer work, extend the reservation as a heartbeat. Call `reservations.extend(id, { ttlSeconds: 600 })` to push its expiry to 600 seconds from now. Inside `reserve`, both SDKs give you `reservation.extend()` for this. You can only extend a reservation that’s still held. Extending one that’s committed, released or expired gets a `409` with the code `conflict`. A reservation can also take a slot of a [concurrent limit](/docs/rolling-and-concurrency/).

## Good to know

* A subject can have at most 1,000 open reservations. Beyond that, the check gets a no with `reason.code: "too_many_reservations"`.
* A reservation counts as one check on your plan when you commit it. A reservation that you release, or that expires, doesn’t count.
* `reservations.get(id)` shows where a reservation stands: held, committed, released or expired. It also shows what it reserved and what it finally used. Reading it changes nothing. On the CLI, that’s `limitry reservations get <reservation-id>`.
* Reservations need the same `checks:write` scope as checks.
