# Add Limitry with your agent

> Let Claude Code, Codex or another coding agent find the costly requests in your code, propose limits and add them once you agree.

Claude Code, Codex or another coding agent can add Limitry for you. It reads your code, proposes limits, asks you before creating any, and wraps the costly requests. It works in [test](/docs/test-and-live/) unless you tell it to use live. Nothing it does affects your real customers until you copy its limits to live.

## 1. Paste this prompt

```text
Read https://limitry.com/docs/skill/SKILL.md and follow it. Add Limitry
to this codebase: find the calls that cost us money or that a user could
abuse, propose limits in a table, and wait for my answer before creating
any.
```

The skill tells your agent how Limitry works and which steps to follow. The agent connects through the [MCP server](/docs/mcp/) or the [CLI](/docs/cli/), finds the costly requests, proposes limits and waits for you. Once you agree, it creates the limits in test and wraps the code with the SDK’s `guard` or `reserve`. It also handles a refusal where your product answers its user, and adds a test. [Build with AI agents](/docs/agents/) has more on the skill.

## 2. What it writes

Say your product has an AI feature. The agent might propose a $2 AI budget a month for each of your users: 200 cents a month on the action `ai-call`.

You only know what a reply cost once it’s done, because it depends on the tokens used. So the agent wraps the request with `reserve`. It holds what the reply will probably cost, runs it, then charges what it actually cost. If the request fails, it gives the hold back:

* TypeScript

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

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

  const reply = await limitry.reserve(
    { subject: { kind: "user", 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;
    },
  );
  ```

* Python

  ```python
  from limitry import Limitry

  limitry = Limitry()  # uses the LIMITRY_API_KEY environment variable

  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)  # your prices, in cents
  ```

When every request costs the same, such as each one to your own API with a limit of 60 a minute for each key, the agent uses `guard` instead. It checks first and runs the work only if it’s allowed:

* TypeScript

  ```ts
  const response = await limitry.guard(
    { subject: { kind: "apikey", id: key.id }, action: "api-request" },
    () => handleRequest(request),
  );
  ```

* Python

  ```python
  with limitry.guard(subject={"kind": "apikey", "id": key.id}, action="api-request"):
      response = handle_request(request)
  ```

A refusal throws `LimitReachedError`, which carries `limitId`, `limitName`, `reason` and `retryAfterSeconds`. Your code can turn it into a 429 or a message for the user.

## 3. Go live

The agent’s limits are in test, and your code reads its key from `LIMITRY_API_KEY`. Use a test key (`lim_test_…`) while you try it. To go live:

1. Copy the limits to live. Click **Copy to live** on the **Limits** page, or let the agent ask for it and approve the request in the app. [Approvals](/docs/approvals/) explains how.
2. Give your production code a live key (`lim_live_…`). An owner or admin can create one in the app with **Live** picked at the top right, or approve the agent’s request for one. The secret never reaches the agent.

## If Limitry is unreachable

Each limit has a fail mode, and the SDK remembers it from the last response. If Limitry doesn’t respond within 1.5 seconds (`timeout`), the fail mode decides. With `open`, the default, the work runs. With `closed`, the SDK throws `UnavailableError`, which is a different error from `LimitReachedError`. A `429`, a `5xx` or a `402` from Limitry counts as no response too. Your product never hangs waiting for Limitry.
