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.
1. Create the budget
Section titled “1. Create the budget”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",});from limitry import Limitry
limitry = Limitry()
limitry.limits.create( name="Agent daily budget", subject_kind="agent", action="*", amount=500, unit="cents", window="day", fail_mode="closed",)limitry limits create --name "Agent daily budget" --subject-kind agent \ --action "*" --amount 500 --unit cents --window day --fail-mode closedcurl -X POST https://api.limitry.com/v1/limits \ -H "Authorization: Bearer $LIMITRY_API_KEY" \ -H "Content-Type: application/json" \ -d '{"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.
2. Check before every step
Section titled “2. Check before every step”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; }}from limitry import LimitExceeded, Limitry
limitry = Limitry() # reads LIMITRY_API_KEY
def step(agent_id: str, run): with limitry.hold( subject={"kind": "agent", "id": agent_id}, action="tool", estimate=10 ) as held: result = run() held.cost(result.cost_cents) return result
def run_agent(agent_id: str, task): try: while not task.done: step(agent_id, lambda: next_step(task)) except LimitExceeded: return pause(task, "This agent reached its budget for today.")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.
3. Let a person approve more
Section titled “3. Let a person approve more”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
subjectIdset. It replaces theagentlimit with the same action and window, so it can raise that agent’s budget. - Through approvals: with a
totalbudget 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.
4. Know when an agent stops
Section titled “4. Know when an agent stops”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.