Skip to content

Give AI agents a budget

Say your product runs AI agents on your customers’ behalf, and each agent calls models and paid tools in a loop. A loop that goes wrong can spend a lot before anyone notices. Give every agent a budget in money, and have the agent check it before every step.

Count in cents. $5 a day for every agent:

import { createClient } from "@limitry/sdk";
const limitry = createClient();
await limitry.limits.create({
name: "Agent daily budget",
subjectKind: "agent",
action: "*",
amount: 500,
unit: "cents",
window: "day",
failMode: "closed",
});

action is *, so every check you make for an agent draws from the same budget. The fail mode is closed: if Limitry can’t be reached, the agent stops rather than spending without a count.

For a budget that never resets, such as $20 for one long task, use the window total instead of day. See Pools, balances and budgets.

Before each step, whether it calls a model or a paid tool, hold an estimate with the action tool, and settle what the step actually cost:

import { createClient, LimitExceeded } from "@limitry/sdk";
const limitry = createClient(); // reads LIMITRY_API_KEY
async function step(agentId: string, run: () => Promise<Step>) {
return limitry.hold(
{ subject: { kind: "agent", id: agentId }, action: "tool", estimate: 10 },
async (held) => {
const result = await run();
held.cost(result.costCents);
return result;
},
);
}
export async function runAgent(agentId: string, task: Task) {
try {
while (!task.done) {
await step(agentId, () => nextStep(task));
}
} catch (err) {
if (err instanceof LimitExceeded) {
return pause(task, "This agent reached its budget for today.");
}
throw err;
}
}

When the budget is used up, Limitry refuses the next step before it runs, so the agent stops between steps rather than in the middle of one.

A daily budget refills at midnight UTC. To let an agent go past its cap sooner, a person decides:

  • For one day: an owner or admin clicks Reset next to the agent on the Usage page, which refills its budget at once (see Reset now).
  • For good: an owner or admin adds a limit for that one agent, with subjectId set. It replaces the agent limit with the same action and window, so it can raise that agent’s budget.
  • Through approvals: with a total budget instead of a daily one, an assistant you connected to Limitry’s MCP server can ask for more credits. An owner or admin approves or declines the request in the app. Asking needs a person’s credential, so an agent that checks with a workspace API key can’t ask for itself. The request names the environment the budget is in, and without one the credits go to test. See Approvals.

The first time an agent hits its budget in a window, your webhook endpoints receive limit.exceeded, with the agent’s id, so you can tell the person who started it.

Related: Reservations, Limit AI spend per user of your product.