Blog Guide
How to set a spending limit for an AI agent
An AI agent does not make one request and stop. It plans, calls a model, calls a tool, reads the result and calls the model again, often many times for a single task. Each call is billed by the model provider. Most of the time that is fine. The problem is the run that goes wrong: a loop that never finishes, a retry storm, or an agent that keeps pasting a huge document back into its own prompt. By the time someone notices, the money is already spent.
This guide explains where agent costs come from, why alerts and account-wide caps are not enough on their own, and how to put a hard limit on each agent.
Where the cost comes from
Model providers charge by tokens, the small pieces of text a model reads and writes. Each request has two parts:
- Input tokens: everything you send, including the system prompt, the conversation so far and any tool results.
- Output tokens: what the model writes back. These usually cost several times more per token than input.
Agents are expensive for a simple reason: the input grows. Every step adds the last answer and the last tool result to the conversation, so the tenth call in a task sends far more text than the first. Ten calls do not cost ten times one call; they can cost much more.
Common ways agents overspend
- Loops. The agent keeps trying the same failing step, or two agents keep handing work back and forth.
- Retries. A timeout or error triggers automatic retries, and each retry is billed again.
- Growing context. Large tool outputs, such as a whole web page or file, get carried into every later call.
- The wrong model. A routine step runs on the most capable and most expensive model when a cheaper one would do.
- No end condition. The task never clearly finishes, so the agent keeps going.
Why alerts and account caps are not enough
Most teams start with two tools. Both help, but neither stops a single agent in time.
Alerts tell you after the money is gone. A message that says "you have spent your monthly budget" arrives once the spending has happened, and often hours later.
Account-wide or project-wide limits, which many providers offer, protect the account as a whole. But they are shared. One runaway agent can use up the whole limit, and then every other agent and product on that account stops too.
What you want is a limit on each agent that is checked before each request is sent.
How a hard per-agent limit works
The idea is simple: before a request goes to the provider, estimate the most it could cost, and only send it if that fits in what the agent has left.
- Count the input. Work out how many input tokens the request contains and multiply by the model's input price.
- Assume the worst for the output. Multiply the request's maximum output tokens by the output price. This is the most the answer could cost.
- Add them up. That total is the most this request can cost.
- Compare with the budget. If the agent's remaining budget for the day or month is smaller than that total, block the request and return a clear error to the agent. Do not send it.
- Settle up afterwards. When the real answer comes back, record the actual cost, which is usually lower than the estimate, and release the difference.
Because the check happens before sending, a limit that says $5 a day means the agent cannot spend more than $5 that day. The limit holds even when nobody is watching.
Choosing the limits
- A per-request limit stops any single call from being unusually large, such as an accidental request with a whole database pasted in.
- A daily limit catches loops quickly. Set it a little above what the agent normally spends in a busy day.
- A monthly limit matches how you are billed and keeps the total predictable.
Start from what the agent actually spends in a normal week, then add a margin. A limit that blocks normal work gets switched off, and a limit nobody uses protects nothing.
Good habits that lower agent costs
- Give every agent task a clear finish line and a maximum number of steps.
- Cap retries, and wait a little longer between each one.
- Trim tool outputs before they go back into the prompt. Keep the parts the agent needs.
- Use smaller, cheaper models for routine steps and save the large models for the hard ones.
- Give each agent its own budget, so one problem cannot take down everything else.
How Xpenvo Agents does this
Xpenvo Agents does the check described above for you. Your agents send requests through it instead of straight to the provider, usually by changing one base URL in your provider's SDK. It works out what each request could cost, blocks anything that would go over that agent's daily, monthly or per-request limit, and records the real cost afterwards. It supports OpenAI, Anthropic, Google Gemini, Mistral, OpenRouter, Azure OpenAI and Amazon Bedrock. You keep your own provider keys, and prompts are not stored. Xpenvo Agents is in private beta, and teams anywhere can join.
More from the blog
- Company newsXpenvo Finance and Xpenvo Agents: one company, two productsXpenvo now has two clearly named products: Xpenvo Finance for personal and business money, and Xpenvo Agents for spending limits on AI agents. Here is what each one does and why we renamed the app. · 2 min read
- GuideHow to keep business and personal money apart when you work for yourselfA simple, worldwide guide for freelancers, side hustlers and small business owners: seven habits that keep business money separate from household money, make tax time easier and show whether the business really pays. · 3 min read