# Offer a free plan with a daily cap

> Give a free plan a monthly AI budget and a daily cap against abuse, and a paid plan its budget alone, reset on each user's billing date.

Say each of your free users gets a $2 AI budget a month, and at most 50 cents a day, so one sign-up can’t spend the whole month in an hour. Your Pro users get $20 a month and no daily cap. With Limitry that’s three limits, and one check.

## 1. The limits

Give each plan its own subject kind, so each plan gets its own limits:

| Limit          | `subject.kind` | `amount` (cents) | `window` | `start`        |
| -------------- | -------------- | ---------------- | -------- | -------------- |
| Free AI budget | `user-free`    | 200              | `month`  | `billing_date` |
| Free daily cap | `user-free`    | 50               | `day`    | `calendar`     |
| Pro AI budget  | `user-pro`     | 2000             | `month`  | `billing_date` |

* TypeScript

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

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

  const freeBudget = await limitry.limits.create({
    name: "Free AI budget",
    subject: { kind: "user-free" },
    action: "ai-call",
    amount: 200,
    unit: "cents",
    window: "month",
    start: "billing_date",
  });
  console.log(freeBudget.id);

  const freeDaily = await limitry.limits.create({
    name: "Free daily cap",
    subject: { kind: "user-free" },
    action: "ai-call",
    amount: 50,
    unit: "cents",
    window: "day",
  });
  console.log(freeDaily.id);

  const proBudget = await limitry.limits.create({
    name: "Pro AI budget",
    subject: { kind: "user-pro" },
    action: "ai-call",
    amount: 2000,
    unit: "cents",
    window: "month",
    start: "billing_date",
  });
  console.log(proBudget.id);
  ```

* Python

  ```python
  from limitry import Limitry

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

  free_budget = limitry.limits.create(
      name="Free AI budget",
      subject={"kind": "user-free"},
      action="ai-call",
      amount=200,
      unit="cents",
      window="month",
      start="billing_date",
  )
  print(free_budget.id)

  free_daily = limitry.limits.create(
      name="Free daily cap",
      subject={"kind": "user-free"},
      action="ai-call",
      amount=50,
      unit="cents",
      window="day",
  )
  print(free_daily.id)

  pro_budget = limitry.limits.create(
      name="Pro AI budget",
      subject={"kind": "user-pro"},
      action="ai-call",
      amount=2000,
      unit="cents",
      window="month",
      start="billing_date",
  )
  print(pro_budget.id)
  ```

The monthly budgets reset on each user’s own date (`start: "billing_date"`). Until you set a billing date, that’s the day of the user’s first counted check. Once they pay, set their billing date (step 3). The daily cap resets at midnight UTC. A tighter alternative against bursts is a rolling 24 hours: `window: "day", rolling: true`.

## 2. One check per AI reply

Send the user’s plan as the subject kind. The check has to fit every limit on that kind. A free user gets a no once they’ve spent 50 cents in a day, even with budget left for the month. `reserve` holds what the reply will probably cost, then charges what it actually cost:

* TypeScript

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

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

  try {
    const reply = await limitry.reserve(
      {
        subject: { kind: `user-${user.plan}`, id: user.id },
        action: "ai-call",
        cost: 5,
      },
      async (reservation) => {
        const res = await callModel(prompt);
        reservation.cost(priceCents(res.usage)); // your prices, in cents
        return res.text;
      },
    );
    console.log(reply);
  } catch (error) {
    if (error instanceof LimitReachedError) {
      // Tell the user which limit, and when to try again.
      console.log(error.retryAfterSeconds);
    }
  }
  ```

* Python

  ```python
  from limitry import LimitReachedError, Limitry

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

  try:
      with limitry.reserve(
          subject={"kind": f"user-{user.plan}", "id": user.id}, action="ai-call", cost=5
      ) as reservation:
          res = call_model(prompt)
          reservation.cost = price_cents(res.usage)  # your prices, in cents
          print(res.text)
  except LimitReachedError as error:
      # Tell the user which limit, and when to try again.
      print(error.retry_after_seconds)
  ```

## 3. Billing dates and plan changes

When a user subscribes, set their billing date to the subscription’s, so their budget resets on the day they pay. See [Reset on the billing date](/docs/works-with/stripe/#reset-on-the-billing-date).

A user who upgrades moves to the `user-pro` kind. The daily cap no longer applies, and the Pro budget starts from their billing date.

## 4. Goodwill

A user reached their cap during a launch? Reset it at once, without moving their schedule:

* TypeScript

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

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

  const reset = await limitry.subjects.reset("user-free", "user_123", {});
  console.log(reset);
  ```

* Python

  ```python
  from limitry import Limitry

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

  reset = limitry.subjects.reset("user-free", "user_123")
  print(reset)
  ```

See [Reset now](/docs/resets/#reset-now).
