# Limits

> How much each subject may use in each window, how several limits combine, and what you can change once a limit exists.

A limit says how much a subject may use per window. It reads as one sentence: *each user may use 200 cents per month*. Subjects are whoever acts in your product, such as your users, teams, API keys or AI agents. You choose the labels, and Limitry only matches them.

## The three kinds

| Kind            | Reads as                                            | `window`                                                                          |
| --------------- | --------------------------------------------------- | --------------------------------------------------------------------------------- |
| Per period      | each user may use 100 credits per month             | `minute`, `hour`, `day`, `week`, `month` or `year`                                |
| Balance         | each user starts with 500 credits, and you add more | `total`, a balance that never resets                                              |
| Running at once | each user may run 3 at the same time                | `concurrent`: a slot per running job (a reservation), given back when it finishes |

In the app, **Add limit** on the **Limits** page asks for the kind, then the sentence: *Each \[user] may use \[100] \[credits] per \[month].* Everything else is under **More options**, with the defaults listed below. A preview shows what a subject would see. After you save, **Add another limit** opens a new one with the same unit and period filled in.

## Fields

A limit needs only `subject.kind`, `amount` and `window`.

| Field      | Meaning                                                                                                                    |
| ---------- | -------------------------------------------------------------------------------------------------------------------------- |
| `subject`  | Who it applies to. `{"kind": "user"}` is every user. Add `"id": "user_123"` for one subject.                               |
| `amount`   | How many units per window. For `concurrent`, how many at once.                                                             |
| `window`   | `minute`, `hour`, `day`, `week`, `month`, `year`, `total` or `concurrent`. See below.                                      |
| `action`   | The action it limits. Default `*`, every action.                                                                           |
| `unit`     | What the amount counts (`calls`, `credits`, `cents`), in words. Default `units`.                                           |
| `name`     | Your label, unique among the environment’s active limits. Default: the sentence, such as “100 credits per user per month”. |
| `every`    | How many windows a period lasts: `every: 3` with `month` is a quarter. Default `1`.                                        |
| `start`    | Where a period begins: `calendar` (the default), `billing_date` or `first_use`. See [When limits reset](/docs/resets/).    |
| `rolling`  | With `minute`, `hour` or `day`: count the last `every` windows up to each check, such as any 60 minutes. Default `false`.  |
| `failMode` | What your code should do when it can’t reach Limitry: `open` (allow, the default) or `closed` (refuse).                    |
| `enabled`  | Whether checks enforce it. Default `true`.                                                                                 |

A name made from the sentence names the action when it isn’t `*`: “100 credits per user per month for ai-call”. If another active limit already has that name, the API returns `409` with `conflict` and creates nothing. Send a `name` of its own.

Limitry saves every count before it returns a decision, whatever the limit. There is no faster mode that could lose a count.

## Windows

* `minute`, `hour`, `day`, `week`, `month` and `year` reset at the end of each period. By default a period ends on a UTC boundary. It can instead end on each subject’s billing date, or a set time after its first use. A period can also be several windows long, such as every 3 months or every 5 hours. See [When limits reset](/docs/resets/).
* `minute`, `hour` and `day` can also be [rolling](/docs/rolling-and-concurrency/): they count the last 60 seconds, minutes or 24 hours.
* `total` is a balance that never resets, for prepaid credits. See [Pools, balances and budgets](/docs/pools-and-budgets/).
* `concurrent` allows at most `amount` at once. See [Rolling windows and concurrency](/docs/rolling-and-concurrency/).

When several limits match a subject and action, all of them apply. That is how you give each of your users a $2 AI budget a month and at most 50 cents a day, so one day can’t use up the month. You make two limits, and a check has to fit both.

There’s one exception. A limit set for one subject, with `subject.id`, replaces the limit for its whole kind that has the same action and window. Use it to give one customer more or less than everyone else. The kind’s other limits still apply to that customer. A subject with its own `minute` limit is still held to the kind’s `day` limit.

To limit a user, their team and their tenant at once, make one limit for each and name all three in the check. See [Budgets at several levels](/docs/guides/several-levels/).

## Creating a limit

In the app, limits are on the **Limits** page. Everyone in the workspace can see them. The owner and admins can change them, and members can in test. From your code or the CLI, a key or token needs the `limits:read` or `limits:write` scope.

[![The limits table, with six limits, each with Edit and a menu of more actions. Credits is a balance of 100 credits per team. Agent daily budget is 500 cents per agent a day. API burst is 60 requests in any 60 seconds per API key. Exports at once is 3 at once per workspace. AI budget is 200 cents per user a month, and shows its checks and refusals this month. API per hour is 1,000 requests in any 60 minutes per API key. The two budgets show Refuses if unreachable.](/docs/_astro/limits.Yx4KQJdi_Z1aqbKL.webp) ![The limits table, with six limits, each with Edit and a menu of more actions. Credits is a balance of 100 credits per team. Agent daily budget is 500 cents per agent a day. API burst is 60 requests in any 60 seconds per API key. Exports at once is 3 at once per workspace. AI budget is 200 cents per user a month, and shows its checks and refusals this month. API per hour is 1,000 requests in any 60 minutes per API key. The two budgets show Refuses if unreachable.](/docs/_astro/limits-dark.Bt_D6FPZ_1perfL.webp) ![The limits table, with six limits, each with Edit and a menu of more actions. Credits is a balance of 100 credits per team. Agent daily budget is 500 cents per agent a day. API burst is 60 requests in any 60 seconds per API key. Exports at once is 3 at once per workspace. AI budget is 200 cents per user a month, and shows its checks and refusals this month. API per hour is 1,000 requests in any 60 minutes per API key. The two budgets show Refuses if unreachable.](/docs/_astro/limits-phone.CLIfj_tb_228QPc.webp) ![The limits table, with six limits, each with Edit and a menu of more actions. Credits is a balance of 100 credits per team. Agent daily budget is 500 cents per agent a day. API burst is 60 requests in any 60 seconds per API key. Exports at once is 3 at once per workspace. AI budget is 200 cents per user a month, and shows its checks and refusals this month. API per hour is 1,000 requests in any 60 minutes per API key. The two budgets show Refuses if unreachable.](/docs/_astro/limits-phone-dark.CrBmOfQM_wCHzY.webp)](/docs/_astro/limits.Yx4KQJdi.png "Open the screenshot at full size")

* TypeScript

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

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

  // Named "200 cents per user per month for ai-call"
  const limit = await limitry.limits.create({
    subject: { kind: "user" },
    action: "ai-call",
    amount: 200,
    unit: "cents",
    window: "month",
    failMode: "closed",
  });
  console.log(limit.id);
  ```

* Python

  ```python
  from limitry import Limitry

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

  # Named "200 cents per user per month for ai-call"
  limit = limitry.limits.create(
      subject={"kind": "user"},
      action="ai-call",
      amount=200,
      unit="cents",
      window="month",
      fail_mode="closed",
  )
  print(limit.id)
  ```

* CLI

  ```bash
  limitry limits create --subject '{"kind":"user"}' --action ai-call \
    --amount 200 --unit cents --window month --fail-mode closed
  ```

* curl

  ```bash
  curl -X POST https://api.limitry.com/v1/limits \
    -H "Authorization: Bearer $LIMITRY_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"subject":{"kind":"user"},"action":"ai-call","amount":200,
         "unit":"cents","window":"month","failMode":"closed"}'
  ```

An AI agent can do the same through the [MCP server](/docs/mcp/). The workspace’s audit log records every change, and a change reaches [checks](/docs/checks/) at once.

## What can change

You can change a limit’s name, amount and fail mode at any time, and turn it off or on. Once a limit exists, you can’t change what it counts: its `subject` kind and id, `action`, `unit`, `window`, `every`, `start` and `rolling`. A change to one of them would start the limit’s counts over, or compare counts in the wrong unit. Limitry refuses it with `400 invalid_request` and a message saying what to do instead.

To change what a limit counts, create a new limit with the new values, then archive the old one. The old limit’s history stays. In the app, the edit view shows the fixed fields as text, with “Create a new limit to change this”. In a [`limitry.json`](/docs/limitry-json/), give the entry a new name.

A limit that’s off (`enabled: false`) never refuses, but checks still count against it. When you turn it on again, it starts from what your subjects actually used, not from zero.

## A limit no check reaches

Each limit’s `lastCheckedAt` says when a check last reached it, to the hour. When no check has reached a limit in 7 days, the **Limits** page flags it and names the subject kind it’s waiting for. This usually means your checks send a different subject kind or action than the limit has, such as `teams` instead of `team`. Fix the check, or create a limit that matches it.

## A limit’s page

On the **Limits** page, each limit shows how many checks reached it and how many it refused. A rate limit counts today. A weekly limit counts this week. A monthly or yearly limit, and a balance, count this month. Click a limit’s name to open its page.

| Part                 | What it shows                                                                                                         |
| -------------------- | --------------------------------------------------------------------------------------------------------------------- |
| Last 30 days         | The checks the limit allowed and refused each day, and what they used.                                                |
| Closest to the limit | The subjects that used the most in the current window, each against its allowance.                                    |
| Refused lately       | The subjects the limit refused in the last 7 days, the most refused first.                                            |
| Settings             | The allowance, who and which action it applies to, when it resets, and what happens if your code can’t reach Limitry. |

A balance’s page also has **Credits**, which lists every credit added to it. When your plan keeps less history, the chart shows fewer days.

## Who’s told

When a live limit refuses one of your users, Limitry tells the workspace’s owners and admins. It also tells them when one of your users passes 80% or 100% of a live limit. Each limit sends each notification at most once a day. Each person chooses whether it reaches them in the app, by email or on their phone. See [Notifications](/docs/notifications/).

## Managing limits

Test and live each have their own limits. The app shows the environment you picked at the top right. A key manages the limits of its own environment. Set limits up in test, then click **Copy to live** to create or update them in live. See [Test and live](/docs/test-and-live/).

Every call about one limit takes its name in place of its ID, such as `GET /v1/limits/AI%20budget` or `limitry limits get "AI budget"`. A name matches an active limit in the same environment. An exact match comes first. Otherwise, the name matches whatever its case. When two limits match that way, the call fails with a 400 that names both. Restoring a limit by name looks among the archived ones.

To keep your limits in your repository instead, list them in a [`limitry.json`](/docs/limitry-json/) and run `limitry sync`. It matches your limits to the file by name, creating and changing them as needed. It archives limits that aren’t in the file only when you ask it to. You can run it from your machine or from CI. See [Sync limits from GitHub](/docs/guides/sync-from-github/). A limit the file names is locked in the app and the API, except for an override until the next sync. Its page says **Set by limitry.json**, or names whatever else the sync came from (see [When the file is the source of truth](/docs/limitry-json/#when-the-file-is-the-source-of-truth)). You can also generate the file from your plans, or sync from your code (see [Three ways to sync](/docs/limitry-json/#three-ways-to-sync)).

## Trying a limit in the app

In test, the **Limits** page has **Try a check**. Pick a limit, type a subject id such as `user_123`, and click **Check**. You get the same response your code would get from a check with a test key: allowed or refused, and what’s left on every limit that applies. To see a refusal, keep clicking until a limit says no. These checks count against your test allowance, and we don’t bill them. Owners, admins and members can try checks, and only in test. In live, every check would count for real.

## Examples to start from

While a workspace has no limits in test, the **Limits** page offers **Add example limits**. It adds three limits, each named “Example: …”. They cap images per user per day, set an AI budget per user per month, and allow API calls per key per minute. **Simulate traffic** then sends a few hundred test checks from pretend users and keys, enough for some to get a no. The [Usage](/docs/usage/) page shows them within a minute. **Remove examples** archives the example limits and nothing else. Examples are for test only.

## Archiving a limit

Limitry never erases a limit. When you no longer need one, archive it. On the Limits page, open the limit’s ⋮ menu and choose **Archive**, or run `limitry limits archive <limit-id>`. The API’s `POST /v1/limits/{limitId}/archive` answers with the archived limit. Checks stop enforcing it at once and don’t count against it while it’s archived. It leaves the list, but its usage history and credits stay. The [Usage](/docs/usage/) page still shows it, marked archived. **Archived limits** on the Limits page lists them, and **Restore** (or `limitry limits restore <limit-id>`) brings one back. An archived limit’s name is free, so a new limit can take it. You can’t restore a limit while another active limit has its name.
