# Checks

> Ask whether a subject may do an action at a cost, and what to do with the result.

A check is the question your code asks before it does something that costs money: may this subject do this action, at this cost? Limitry looks at every enabled [limit](/docs/limits/) that matches, and decides. Here, user `user_123` wants to make an `ai-call` that costs 5 cents, and has a budget of 200 cents a month.

* 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,
    },
    { idempotencyKey: "7f9c…" },
  );
  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,
      idempotency_key="7f9c…",
  )
  print(decision)
  ```

* CLI

  ```bash
  limitry check user:user_123 ai-call --cost 5
  ```

* curl

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

To check and do the work in one step, use `guard` in the [TypeScript](/docs/typescript/) or [Python](/docs/python/) SDK. It runs your code only when Limitry allows the check. Here is the response for a user with 3 cents left this month:

```json
{
  "allowed": false,
  "reason": {
    "code": "limit_reached",
    "limitId": "lmt_3f0c…",
    "limitName": "AI budget",
    "subject": { "kind": "user", "id": "user_123" },
    "message": "Refused by AI budget for user user_123: 3 cents left, resets 2026-11-01T00:00:00.000Z. Try again in 1123200 s."
  },
  "limits": [
    {
      "limitId": "lmt_3f0c…",
      "limitName": "AI budget",
      "unit": "cents",
      "window": "month",
      "rolling": false,
      "failMode": "closed",
      "amount": 200,
      "remaining": 3,
      "resetsAt": "2026-11-01T00:00:00.000Z"
    }
  ],
  "reservation": null,
  "retryAfterSeconds": 1123200
}
```

`cost` is how many of the limit’s units this action uses, such as cents, tokens or 1 per job. It defaults to 1. A check that gets a no uses nothing, so the user’s 3 cents are still there for a cheaper call.

## Reading the response

Branch on `allowed`: every decision returns HTTP 200, whether it’s a yes or a no. An error status means the request itself failed, for example because of a bad key, an invalid body, or calling too fast.

`limits` lists every limit that applied, with its `amount` and what is left on it in `remaining`. You can show “3 cents left of 200 this month” without another call. When Limitry refuses the check, `reason` says why.

| `reason.code`           | Why                                                                        |
| ----------------------- | -------------------------------------------------------------------------- |
| `limit_reached`         | The subject has used up a limit. `reason.limitId` and `limitName` name it. |
| `too_many_reservations` | The subject already holds the most open reservations (1,000) at once.      |

`reason.subject` names whose limit refused the check. It’s the check’s own subject, or one of its `shared` subjects when that subject’s limit refused it, such as a team’s budget.

`retryAfterSeconds` says when to try again. It’s `null` when waiting won’t help, such as with an empty balance.

`reason.message` says the same thing in one sentence, for your logs or your users. The CLI and the SDKs show it as it is. Show it, but don’t parse it, because its wording may change. Your code should branch on `allowed` and `reason.code`.

## Several limits and shared pools

When several limits match, the check gets a yes only if the cost fits all of them, and then it counts against all of them.

To draw from a shared pool as well, name up to 4 more subjects:

```json
{
  "subject": { "kind": "user", "id": "user_123" },
  "shared": [{ "kind": "team", "id": "team_42" }],
  "action": "ai-call",
  "cost": 5
}
```

Every limit on every subject must fit. Limitry charges all of them, or none. See [Pools, balances and budgets](/docs/pools-and-budgets/).

## Modes

| `mode`    | What it does                                                                                           |
| --------- | ------------------------------------------------------------------------------------------------------ |
| `consume` | The default. Decides and counts.                                                                       |
| `preview` | Decides without counting: “would this get a yes?”                                                      |
| `reserve` | Decides and reserves the cost until you commit or release it. See [Reservations](/docs/reservations/). |

## Good to know

* Retries are safe with an `Idempotency-Key` header. For an hour, a repeat with the same key gets the same response and isn’t counted again. The SDKs and the CLI send one for you. See [Idempotency](/docs/idempotency/).
* If your code can’t reach Limitry, follow each limit’s `failMode`. The SDKs do this for you. They treat a `429`, a `5xx`, a timeout and a `402 plan_limit_reached` alike, as Limitry being unavailable, and let the fail mode decide. See [How Limitry works](/docs/how-it-works/).
* The key needs the `checks:write` scope.
* `GET /v1/checks` lists the checks of the last 3 days, newest first. On the CLI, it’s `limitry checks list`. See [Subjects, names and links](/docs/subjects/#from-your-code-or-the-cli).
* Limit changes reach checks at once.
* A check uses the limits and counts of its key’s environment. A test key (`lim_test_…`) never touches live counts. See [Test and live](/docs/test-and-live/).
* Checks with a live key that get a yes count toward your [plan](/docs/plans/), and those that get a no don’t. Test checks have their own allowance, and we don’t bill them.
* Every response has a `Server-Timing` header that says where the time went inside Limitry, in milliseconds. See the table below.
* To see two more values, send the header `limitry-timing: trip`. The header adds an extra round trip, so use it for measuring, not in production.
* Compare `total` with what your code measures to see how much of the time is network.

| Value           | What it measures                                                                                   |
| --------------- | -------------------------------------------------------------------------------------------------- |
| `auth`          | Checking your key.                                                                                 |
| `layout`        | Looking up where Limitry keeps the subject’s counts.                                               |
| `limiter`       | The trip to where Limitry keeps your counts, and the count itself.                                 |
| `limiter_alarm` | Part of `limiter`: scheduling the work that follows the count, such as copying it to our database. |
| `limiter_save`  | How long the last completed save of these counts took. That save may belong to an earlier check.   |
| `limiter_n`     | Not a time. It numbers the place that keeps the subject’s counts, for support.                     |
| `total`         | All of the time inside Limitry.                                                                    |
| `edge`          | Not a time. It names the Cloudflare location and the copy of Limitry that responded, for support.  |
| `trip`          | With `limitry-timing: trip` only. One round trip to your counts with nothing counted.              |
| `limiter_at`    | With `limitry-timing: trip` only. The Cloudflare location where Limitry keeps your counts.         |

The same operation is `limitry check` on the [CLI](/docs/cli/), and a tool on the [MCP server](/docs/mcp/).
