Skip to content

Use Limitry with the OpenAI and Anthropic SDKs

The OpenAI and Anthropic SDKs report the tokens each reply used. Limitry turns those tokens into cents at your prices, and charges them to the user’s budget. Your code calls the model exactly as before, inside one reserve block.

Create the 200-cent monthly budget from Limit AI spend per user. It counts in cents, with the action ai-call.

Reserve a few cents, call the model, then commit what the tokens cost:

import { createClient } from "@limitry/sdk";
import OpenAI from "openai";
// Uses the LIMITRY_API_KEY environment variable.
const limitry = createClient();
// Uses the OPENAI_API_KEY environment variable.
const openai = new OpenAI();
// Your prices, in cents per million tokens.
const priceCents = (usage: OpenAI.CompletionUsage) =>
Math.ceil(
(usage.prompt_tokens * 300 + usage.completion_tokens * 1500) / 1_000_000,
);
export async function answer(userId: string, prompt: string) {
return limitry.reserve(
{ subject: { kind: "user", id: userId }, action: "ai-call", cost: 5 },
async (reservation) => {
const completion = await openai.chat.completions.create({
model: "gpt-6.2-sol",
messages: [{ role: "user", content: prompt }],
});
if (completion.usage) reservation.cost(priceCents(completion.usage));
return completion.choices[0]?.message.content ?? "";
},
);
}

Ask for the usage with stream_options. OpenAI then sends it in the stream’s last chunk:

import { createClient } from "@limitry/sdk";
import OpenAI from "openai";
// Uses the LIMITRY_API_KEY environment variable.
const limitry = createClient();
// Uses the OPENAI_API_KEY environment variable.
const openai = new OpenAI();
// Your prices, in cents per million tokens.
const priceCents = (usage: OpenAI.CompletionUsage) =>
Math.ceil(
(usage.prompt_tokens * 300 + usage.completion_tokens * 1500) / 1_000_000,
);
export async function streamAnswer(userId: string, prompt: string) {
return limitry.reserve(
{ subject: { kind: "user", id: userId }, action: "ai-call", cost: 5 },
async (reservation) => {
const stream = await openai.chat.completions.create({
model: "gpt-6.2-sol",
messages: [{ role: "user", content: prompt }],
stream: true,
stream_options: { include_usage: true },
});
let text = "";
for await (const chunk of stream) {
text += chunk.choices[0]?.delta.content ?? "";
if (chunk.usage) reservation.cost(priceCents(chunk.usage));
}
return text;
},
);
}

The same block works with Claude. The reply’s usage has the token counts:

import Anthropic from "@anthropic-ai/sdk";
import { createClient } from "@limitry/sdk";
// Uses the LIMITRY_API_KEY environment variable.
const limitry = createClient();
// Uses the ANTHROPIC_API_KEY environment variable.
const anthropic = new Anthropic();
// Your prices, in cents per million tokens.
const priceCents = (usage: Anthropic.Usage) =>
Math.ceil(
(usage.input_tokens * 300 + usage.output_tokens * 1500) / 1_000_000,
);
export async function answer(userId: string, prompt: string) {
return limitry.reserve(
{ subject: { kind: "user", id: userId }, action: "ai-call", cost: 5 },
async (reservation) => {
const message = await anthropic.messages.create({
model: "claude-sonnet-5-5",
max_tokens: 1024,
messages: [{ role: "user", content: prompt }],
});
reservation.cost(priceCents(message.usage));
return message.content
.map((block) => (block.type === "text" ? block.text : ""))
.join("");
},
);
}

A stream’s final message carries the usage for the whole reply:

import Anthropic from "@anthropic-ai/sdk";
import { createClient } from "@limitry/sdk";
// Uses the LIMITRY_API_KEY environment variable.
const limitry = createClient();
// Uses the ANTHROPIC_API_KEY environment variable.
const anthropic = new Anthropic();
// Your prices, in cents per million tokens.
const priceCents = (usage: Anthropic.Usage) =>
Math.ceil(
(usage.input_tokens * 300 + usage.output_tokens * 1500) / 1_000_000,
);
export async function streamAnswer(userId: string, prompt: string) {
return limitry.reserve(
{ subject: { kind: "user", id: userId }, action: "ai-call", cost: 5 },
async (reservation) => {
const stream = anthropic.messages.stream({
model: "claude-sonnet-5-5",
max_tokens: 1024,
messages: [{ role: "user", content: prompt }],
});
stream.on("text", (text) => process.stdout.write(text));
const message = await stream.finalMessage();
reservation.cost(priceCents(message.usage));
return message;
},
);
}
  • A reply that uses 10,000 input tokens and 2,000 output tokens commits 6 cents at these prices.
  • On the Usage page, each user’s AI spend this month, across both providers.
  • A user over budget gets a LimitReachedError from reserve before the call, so the model is never called and it costs you nothing. Limit AI spend per user shows the message to send them.

To cap each model on its own as well, name the model in the action: One budget, or a budget per model.

Related: Reservations.