How to Stop AI Agents From Burning Your API Budget
An agent that decides its own step count will keep spending until something stops it. That's the whole problem, and it isn't a billing issue you fix later. It's a design decision you make before the first call.
Give each agent its own key, put a hard ceiling on every run, and decide the limit while you still control the code. Everything below is about doing that without slowing the work down.
What makes agent costs so hard to predict?
A subscription has a fixed price. An agent doesn't, because it chooses how many operations to run.
The loop is simple enough. Read the goal, plan, call a tool, read the result, decide what's next, repeat. Every pass through that loop spends input tokens, output tokens, and usually a paid API call. Nothing in the structure forces a stop. The model decides when it's finished, and that judgement goes wrong in ways that quietly cost money rather than loudly.
Three failures do almost all the damage. The agent never concludes it's done and grinds to its step limit. A tool returns something unexpected and the agent retries the same call in a loop. Or it spawns subagents, each one holding its own conversation and spending from the same unchecked budget.
None of those look like an outage. They look like a working agent that's unusually busy. You find out from the invoice.
There's a second multiplier most people miss. Each step re-sends the conversation so far, so the input side of the bill grows as the run goes on. A single long run is more expensive than the same work split across shorter runs, because the long one keeps paying to re-read its own history. Where output is priced several times higher than input, that gap widens further, and it compounds the moment one agent hands work to another.
How do you pick a limit you can actually defend?
From your own history, not from a benchmark someone else published.
Pull the last few weeks of runs for each job type and record four things per run: step count, duration, cost, and how it ended. Then look at the median and the ninetieth percentile of the runs that finished successfully.
That gives you two numbers that mean something. The median tells you what normal costs. The ninetieth percentile tells you where "unusually expensive but still legitimate" sits. Skip that step and you'll pick a ceiling from intuition, which is usually either so high it never triggers or so low it breaks real work.
Set a soft alert at the ninetieth percentile. The agent keeps going, but it reports progress and its running cost so you can decide whether to intervene. Set the hard stop at roughly double that, rounded up to a round number. You want the ceiling to catch runaway loops, not to referee every expensive-but-justified run.
Where do the controls actually go?
Four places, and they're worth having all of them because each catches a different failure.
A per-run ceiling stops the single runaway job. A step cap stops the agent that never concludes, and it fires earlier than the cost ceiling because steps are cheap to count. A token budget catches the case where a handful of enormous prompts do the damage rather than many small ones. And a wall-clock timeout catches an agent that's stuck waiting on a tool and quietly accumulating retries.
The step cap is the one people skip, and it's the cheapest of the four. Counting iterations costs nothing and it triggers before the bill gets interesting.
Should agents share an API key?
No, and the reason is attribution rather than security.
If five agents share a key, a spike tells you that somebody overspent but not which one. You end up reading logs to answer a question that a separate key would have answered instantly. Separate keys also let you revoke one misbehaving agent without taking the others down, and they make per-agent rate limits possible.
The security argument is real too. A shared key is a shared blast radius. One compromised agent takes the rest with it.
Do caching and model routing actually help?
Yes, and routing usually helps more than caching for agent workloads.
Caching pays off when the same prefix gets sent repeatedly. Agent runs often share a system prompt and tool definitions, so caching that part is straightforward and worth doing. It chips away steadily at cost.
Routing does something different. Most agent steps don't need your most expensive model. Classification, extraction, and deciding which tool to call next are all tasks a cheaper model handles fine. Reserve the expensive model for the steps that genuinely need reasoning, and the savings are usually larger than caching alone. The trick is being honest about which steps those are, because the temptation is to route everything to the big model and stop thinking about it.
What should you review every week?
Four numbers, and it takes about five minutes.
Look at your most expensive runs and check whether they finished successfully. Failed-then-retried runs are where money goes missing, because the work gets paid for twice and you have nothing to show for the first attempt. Watch the median cost per successful run; if it's climbing, context is probably growing somewhere it shouldn't. Watch step counts for a creep upward, which usually means a prompt changed and the agent is now looping more. And watch the share of spend going to your top model, because that ratio drifting up is the earliest signal that routing has quietly stopped working.
What does a production setup look like?
Unremarkable, which is what you want.
Each agent has its own key with a hard monthly cap set slightly above its expected spend. Every run carries a step cap, a token budget and a timeout, all enforced by the code that invokes the model rather than by the prompt asking the model to be careful. Alerts fire at the ninetieth percentile, not at the ceiling, so you hear about the anomaly before the failure. And a weekly review catches drift before it becomes a monthly surprise.
That's the entire system. Nothing about it is clever, and that's the point. Cost control on agents isn't a monitoring problem you solve with a better dashboard. It's a boundary you set in code, and the dashboard only exists to confirm the boundary is holding.
Comments
Sign in with GitHub to join the discussion. Comments are stored in GitHub Discussions, not on this site.