Skip to content

Offer a free plan with a daily cap

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.

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
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);

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.

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:

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);
}
}

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.

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.

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

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);

See Reset now.