# Use Limitry with OpenRouter

> Give each of your users an AI budget across every model on OpenRouter, charged at the exact cost OpenRouter reports for each request.

OpenRouter gives your product one API for many models, and tells you what each request cost. Limitry charges that exact cost to the user’s budget, so each of your users stays within their allowance on every model. There’s no price table to keep.

## 1. Create the budget

Create the 200-cent monthly budget from [Limit AI spend per user](/docs/guides/limit-ai-spend/#1-create-the-budget). It counts in cents, with the action `ai-call`.

## 2. Reserve, call, commit

OpenRouter answers the OpenAI API, so the OpenAI SDK talks to it. Reserve a few cents before the request. Then commit the cost OpenRouter returns, in whole cents:

* TypeScript

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

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

  // OpenRouter answers the OpenAI API, so the OpenAI SDK talks to it.
  const openrouter = new OpenAI({
    baseURL: "https://openrouter.ai/api/v1",
    apiKey: process.env.OPENROUTER_API_KEY,
  });

  // OpenRouter adds what the request cost to its usage, in US dollars.
  type OpenRouterUsage = OpenAI.CompletionUsage & { cost?: number };

  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 completion = await openrouter.chat.completions.create({
            model: "anthropic/claude-sonnet-5.5",
            messages: [{ role: "user", content: prompt }],
            user: userId,
          });
          const usage = completion.usage as OpenRouterUsage | undefined;
          if (usage?.cost !== undefined) {
            reservation.cost(Math.ceil(usage.cost * 100));
          }
          return completion.choices[0]?.message.content ?? "";
        },
      );
    } catch (err) {
      if (err instanceof LimitReachedError) {
        return "You've used this month's AI allowance. It resets on the 1st.";
      }
      throw err;
    }
  }
  ```

* Python

  ```python
  import math
  import os

  from limitry import LimitReachedError, Limitry
  from openai import OpenAI

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

  # OpenRouter answers the OpenAI API, so the OpenAI SDK talks to it.
  openrouter = OpenAI(
      base_url="https://openrouter.ai/api/v1",
      api_key=os.environ["OPENROUTER_API_KEY"],
  )

  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:
              completion = openrouter.chat.completions.create(
                  model="anthropic/claude-sonnet-5.5",
                  messages=[{"role": "user", "content": prompt}],
                  user=user_id,
              )
              # OpenRouter adds what the request cost to its usage, in US dollars.
              cost = getattr(completion.usage, "cost", None)
              if cost is not None:
                  reservation.cost = math.ceil(cost * 100)
              return completion.choices[0].message.content or ""
      except LimitReachedError:
          return "You've used this month's AI allowance. It resets on the 1st."
  ```

The `user` field sends your user’s id to OpenRouter too, so both tools name the same person.

To count small requests more finely, count the budget in hundredths of a cent. A $2 budget is then 20,000, and the commit is `Math.ceil(usage.cost * 10_000)`.

## What you see

* A request that costs $0.0123 commits 2 cents, and the user’s budget shows 198 cents left.
* On the [Usage](/docs/usage/) page, each user’s spend this month, in the cents OpenRouter charged.
* A user over budget gets your message straight away. The request never reaches OpenRouter, so it costs you nothing.

Related: [Reservations](/docs/reservations/), [Warn your users before they hit a limit](/docs/guides/warn-users/).
