Skip to the page
Tecyfy

How to stop runaway AI agent bills with spend limits

An agent's cost grows with the square of its steps, because every step re-sends everything before it. A loop that never stops is how a $1 task becomes a $250 one. Put 3 limits in place: steps and retries per run, a budget per run, and a hard spend limit at the provider, and decide in advance what a run does when it hits one.

Tecyfy · Published · 7 min read

A hand pulls a brake lever marked LIMIT on a spinning money meter as coins fly off: a hard spend limit that stops a runaway AI agent bill

To stop runaway AI agent bills, put 3 limits on every agent: a maximum number of steps and retries per run, a budget per run that stops it when its cost passes a figure, and a hard spend limit at the provider as the last line. Then decide what a run does when it hits one: stop, save its work and say why, so a person can choose to give it more.

Why agent bills run away

An AgentAgentA model that uses tools in a loop, deciding each next step from what the last one returned, until the task is done. works in a loop: the model asks for a tool, your code runs it, the result goes back, and the model decides the next step. Every step sends the whole conversation so far: the instructions, the tools, and every earlier tool result.

So the cost of a run is not steps times a fixed price. Each step is bigger than the one before. Take a run that starts with 20,000 tokens of instructions and tools, where each tool result adds 3,000 more, on Claude Opus 5.5 at $4 per million input tokens:

StepsInput tokens readInput cost, no cacheWith the cache
10335,000$1.34about $0.29
504.7 million$18.70about $1.74
20063.7 million$254.80about $15.70

That is input alone, before any output, and the cache column includes writing each new step to the cache at $5 per million. Going from 50 to 200 steps is 4 times the steps and almost 14 times the cost. Prompt cachingPrompt CachingLetting the API keep the processed start of a prompt for a few minutes, so later requests that begin the same way are cheaper and quicker. cuts each re-read to a twentieth of the price, but the shape stays the same: the long run is the expensive one.

Long runs are rarely planned. They come from a tool that keeps failing while the agent keeps trying, a search for something that is not there, 2 agents handing work back and forth, or an error message the model reads as an instruction to try again.

Try itWhat each step of an agent run costs
20Ktokens

sent with the first step

3Ktokens
200steps
$5

the run stops once it passes this

step 1budget stops it after step 24step 200

$5.23paid before the stop$255with no budget

Unchecked, 200 steps cost $255 in input. The $5 budget stops the run after step 24, at $5.23: 98% of the cost was in the steps it never took.

Input tokens only, on Claude Opus 5.5 at $4 per million (cache writes $5, cache reads $0.20). Each step sends the whole run so far; output and thinking would add to every step.

Limit 1: steps and retries per run

Count the model calls in a run and stop at a number that a normal run never reaches. Look at your logs: if the longest good run took 30 steps, a limit of 60 stops the runaways and none of the real work.

The common frameworks have the setting:

  • Claude Agent SDK: max_turns in ClaudeAgentOptions. A run that reaches it ends with the result error_max_turns.
  • OpenAI Agents SDK: max_turns on Runner.run, which raises MaxTurnsExceeded.
  • LangGraph: recursion_limit in the run's config, which raises GraphRecursionError. Its default is 1,000 steps, far more than most agents need, so set your own.

In your own loop it is a counter. Count retries of the same tool separately: 3 failures in a row of the same call is a reason to stop and report, not to try a fourth time.

Limit 2: a budget per run

A step limit does not know that one step read 300,000 tokens. A budget does. Every response reports its tokens (input, cache writes, cache reads, output); multiply by the prices and add them up as the run goes, and stop when the total passes the budget:

PRICE = {"input": 4, "cache_write": 5, "cache_read": 0.20, "output": 20}  # Opus 5.5, $ per million

def cost_of(usage):
    return (usage.input_tokens * PRICE["input"]
            + usage.cache_creation_input_tokens * PRICE["cache_write"]
            + usage.cache_read_input_tokens * PRICE["cache_read"]
            + usage.output_tokens * PRICE["output"]) / 1_000_000

spent = 0.0
for step in range(MAX_STEPS):
    response = client.messages.create(**request)
    spent += cost_of(response.usage)
    if spent > BUDGET_USD:
        stop_run(reason=f"budget of ${BUDGET_USD} reached after {step + 1} steps")
        break
    ...

The Claude Agent SDK has this built in as max_budget_usd. Its estimate is made on your side and can pass the figure, so set it a little under what you can afford. The agent cost simulator shows what a run of your shape costs before you pick the number.

Limit 3: a hard limit at the provider

Your own limits only cover the code that has them. A forgotten script, a leaked key or a bug in the budget check is caught only by a limit at the provider:

  • Anthropic: set a monthly spend limit on the Billing page of the Claude Console, below your tier's cap. Once usage reaches it, requests fail until the next month or until you raise it. Workspaces other than the default one can have their own lower limit, so give each agent or environment its own workspace.
  • OpenAI: set a hard spend limit for the organization or for a project. A spend alert is not one: it notifies you and lets traffic carry on. Enforcement is not instant, so spend can pass the limit slightly.

A provider limit stops everything at once, the good runs with the bad. Set it where hitting it means something has gone wrong, not where a busy day ends.

What a run should do at the limit

A run cut off mid-task with no note is the worst of both: the money is spent and nothing can be reused. Decide in advance:

  • Stop and say why. Return a result that names the limit, the step it reached and the last thing it was doing.
  • Keep the work. Save the files, rows or notes written so far, so a rerun can start from them.
  • Let a person extend it. A queue of "this run needs more budget" with the reason is cheaper than a higher default for every run.
  • Hand routine steps to a smaller model. If a run keeps hitting its budget on easy steps (reading, sorting, summarizing), routingModel RoutingSending each request to the cheapest model that handles it well, by rules, a classifier or a cascade, and checking with an eval that quality holds. them to a cheaper model helps more than raising the cap.

Watch the runs, not just the bill

A monthly bill tells you a run went wrong weeks after it did. Log each run's steps and cost, and look at the longest ones each week. A run that took 5 times the usual steps is where the next runaway is coming from.

How to reduce LLM API costs covers the steady savings (caching, batches, shorter output, smaller models), and what an AI agent costs per month works out a normal month's bill.

Questions

Why did my AI agent run up such a large API bill?
Usually a loop with no stop: a tool keeps failing and the agent keeps retrying, or it keeps searching for something it cannot find. Each step re-sends the whole conversation so far, so the cost of a run grows with the square of its steps. 200 steps cost far more than 4 times 50.
How do I set a budget limit for an AI agent?
Count the tokens each call reports and stop the run when its cost passes a figure you choose. Agent frameworks have a setting for it: the Claude Agent SDK has max_turns and max_budget_usd, the OpenAI Agents SDK has max_turns, and LangGraph has recursion_limit. Add a hard spend limit in the provider's console as the last line.
What should an agent do when it hits its budget?
Stop cleanly and say so: save what it has done, report which step it reached and why it stopped, and let a person decide whether to give it more budget. A run that ends with a clear note costs less to recover than one that was cut off without one.
Does a spend limit in the Claude Console stop requests?
Yes. The Billing page of the Claude Console lets you set a monthly spend limit below your tier's cap; once usage reaches it, requests fail with an error saying the limit was reached until the next month or until you raise it. Workspaces can have their own lower limits.
Are OpenAI spend alerts the same as a limit?
No. Spend alerts only notify you and traffic continues. A hard spend limit, set for the organization or a project, is what makes requests fail with a 429 once it is reached, and enforcement is not instantaneous, so spend can pass it slightly.