# Limit AI spend per user

> Give each of your users a monthly AI budget in dollars, charge what each reply actually cost, and stop at the budget.

Say each user on your product’s free plan gets a $2 AI budget a month. You don’t know what a reply costs until it’s done, because it depends on the tokens used. So you reserve what it will probably cost before the call and commit the real cost after it.

## 1. Create the budget

Count in cents. $2 is 200:

* TypeScript

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

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

  const limit = await limitry.limits.create({
    name: "AI budget",
    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()

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

* CLI

  ```bash
  limitry limits create --name "AI budget" --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 '{"name":"AI budget","subject":{"kind":"user"},"action":"ai-call",
         "amount":200,"unit":"cents","window":"month","failMode":"closed"}'
  ```

The fail mode is `closed`, so if your code can’t reach Limitry, refuse the call rather than spend money nobody is counting. Choose `open` if you’d rather keep answering users.

Limitry stores every cent before it responds, so the budget never goes over, even if a server fails.

## 2. Reserve, call, commit

Reserve on the high side. Whatever the call doesn’t use, Limitry gives back straight away.

* TypeScript

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

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

  // Your prices, in cents per million tokens.
  const priceCents = (usage: { inputTokens: number; outputTokens: number }) =>
    Math.ceil((usage.inputTokens * 300 + usage.outputTokens * 1500) / 1_000_000);

  export async function answer(userId: string, prompt: string) {
    try {
      return await limitry.reserve(
        { subject: { kind: "user", id: userId }, action: "ai-call", cost: 5 },
        async (reservation) => {
          const res = await callModel(prompt);
          reservation.cost(priceCents(res.usage));
          return res.text;
        },
      );
    } catch (err) {
      if (err instanceof LimitReachedError) {
        return "You've used this month's AI allowance. It resets on the 1st.";
      }
      if (err instanceof UnavailableError) {
        return "AI replies are paused for a moment. Try again shortly.";
      }
      throw err;
    }
  }
  ```

* Python

  ```python
  import math

  from limitry import LimitReachedError, Limitry, UnavailableError

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


  # Your prices, in cents per million tokens.
  def price_cents(usage) -> int:
      return math.ceil((usage.input_tokens * 300 + usage.output_tokens * 1500) / 1_000_000)


  def answer(user_id: str, prompt: str) -> str:
      try:
          with limitry.reserve(
              subject={"kind": "user", "id": user_id}, action="ai-call", cost=5
          ) as reservation:
              res = call_model(prompt)
              reservation.cost = price_cents(res.usage)
              return res.text
      except LimitReachedError:
          return "You've used this month's AI allowance. It resets on the 1st."
      except UnavailableError:
          return "AI replies are paused for a moment. Try again shortly."
  ```

If the work inside `reserve` throws, `reserve` releases the reservation, so a failed reply costs the user nothing.

`callModel` stands for your own model call. For the code with a real SDK, see [OpenAI and Anthropic](/docs/works-with/openai-anthropic/), [OpenRouter](/docs/works-with/openrouter/), [the Vercel AI SDK](/docs/works-with/vercel-ai-sdk/) or [LiteLLM](/docs/works-with/litellm/).

## 3. One budget, or a budget per model

A check carries the subject, the action and the cost, and nothing else. Limitry doesn’t store the model, the token counts or the prompt of each call. Keep those details in your own logs. The cost is where models differ: `priceCents` charges each call at its model’s price.

The simplest setup sends the same action, `ai-call`, for every model. The one budget from step 1 then covers them all. A limit matches its action name exactly, so a call sent as `ai-call.claude` would miss a limit on `ai-call`.

To cap a model on its own, or to read spend per model, name the model in the action and use `*` for the overall budget. Every limit that matches a check applies, so a call sent as `ai-call.claude` counts against both of these:

| Limit        | Subject kind | Action           | Amount            |
| ------------ | ------------ | ---------------- | ----------------- |
| Model budget | `user`       | `*`              | 200 cents a month |
| Claude spend | `user`       | `ai-call.claude` | 150 cents a month |
| GPT spend    | `user`       | `ai-call.gpt`    | 100 cents a month |

Limitry reports usage per limit, so each model’s limit shows that model’s spend. A model with no limit of its own still counts against the budget, but has no figure of its own.

A limit with the action `*` counts every check for its subject kind. Your `user` subjects may also make other checks, such as exports. If they do, send AI checks with a kind of their own, such as `{ kind: "ai-user", id }`, and put these limits on that kind.

## 4. Different budgets per plan

Give paying users more by putting a second limit on a different subject kind, and sending that kind for them. For example, a `pro-user` limit of 2,000 cents a month, and `{ kind: "pro-user", id }` as the subject for Pro users.

## 5. A short limit from first use

Many AI products also limit bursts: “45 messages per 5 hours, starting from your first message”. That’s a second limit, next to the monthly budget. It resets five hours after a user’s first message, and nothing runs while they’re away:

* TypeScript

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

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

  const limit = await limitry.limits.create({
    name: "Messages per 5 hours",
    subject: { kind: "user" },
    action: "chat",
    amount: 45,
    window: "hour",
    every: 5,
    start: "first_use",
  });
  console.log(limit.id);
  ```

* Python

  ```python
  from limitry import Limitry

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

  limit = limitry.limits.create(
      name="Messages per 5 hours",
      subject={"kind": "user"},
      action="chat",
      amount=45,
      window="hour",
      every=5,
      start="first_use",
  )
  print(limit.id)
  ```

A limit matches its action exactly, so the burst limit counts only checks sent with the action `chat`. Send one before each message, with the default cost of 1, next to the `ai-call` reservation for the budget. The two limits use different actions because a check charges the same cost to every limit it matches: one in cents would use up 45 messages at once. See [When limits reset](/docs/resets/).

## 6. Know when someone runs out

Add a webhook endpoint under **Settings → Developers**. The first time a user hits the budget in a month, you receive [`limit.reached`](/docs/usage/#webhook-limitreached). Use it to show an upgrade offer.

Related: [Reservations](/docs/reservations/), [Pools, balances and budgets](/docs/pools-and-budgets/).
