# How to set max iterations for AI agent loop before it drains your budget

> Source: <https://startupfortune.com/how-to-set-max-iterations-for-ai-agent-loop-before-it-drains-your-budget/>
> Published: 2026-11-18 23:36:38+00:00

**Recommendation:** Cap agent loops at 1.5-2x the p95 step count of successful tasks, halt after three identical failed tool calls, and set a per-task dollar ceiling at 10x median cost, since a 200-step loop cost about $95 versus $0.34 for 8 steps.
More in the playbook "Controlling LLM cost": <https://wpnews.pro/playbooks/llm-cost>

*A stuck agent never crashes. It just keeps paying for the same mistake until someone reads the invoice, and three cheap checks stop it at the third attempt.*

- Agent loops cost quadratically, not linearly, because every step resends the whole growing context, so a 200-step loop in our worked example costs about $95 against $0.34 for a normal 8-step task.
- Set the step cap at roughly 1.5 to 2 times the p95 step count of your successful tasks, not at a round number someone guessed.
- Hash each tool name plus its arguments and stop when the same call fails three times, which catches most loops long before the cap does.
- Add a per-task dollar ceiling at about 10 times your median task cost, and make sub-agents draw from the parent's budget.
- When a limit trips, return partial work and log the trace, and don't let a wrapper quietly restart the task.

Picture a support agent with a tool called search_orders. It sends a date in the wrong format, gets a 400 error back, reads the error, and sends the same date in the same wrong format. Then it does it again. Nothing throws an exception, nothing times out, and every turn looks like progress in your logs. If you're wondering how to set max iterations for AI agent loop code so that this ends at turn ten instead of turn two hundred, the answer takes three numbers, and two of them come from data you already have.

The failure is boring, which is why it's expensive. A model that sees an error will often try the same thing with a tiny variation, or the exact same thing, because the error message didn't change its mind. A person would stop and ask. An agent with no limit keeps going until the context window fills or the provider rate-limits it.

Most teams price agents per step. That's the wrong model. Each turn of an agent resends the system prompt, the tool definitions, and everything that has happened so far, so the input for step 50 includes steps 1 to 49. Cost grows with the square of the step count, not in a straight line.

Here's a worked example. The numbers are illustrative, so swap in your own. Assume $3 per million input tokens and $15 per million output tokens, a 6,000-token starting prompt, and 1,500 tokens added per step (the tool call plus the error it returns), with 300 output tokens per step.

[How to Write an AI Agent Usage Cap Clause Before Customers Hit It](https://startupfortune.com/how-to-write-an-ai-agent-usage-cap-clause-before-customers-hit-it/)

AI agent usage cap contract clause terms need five specific parts, not one number and a price, and vendors like Vercel and OpenAI already default to soft caps with notification instead of instant shutoff when customers cross their limit. - [how to write AI agent usage cap clauses](https://startupfortune.com/how-to-write-an-ai-agent-usage-cap-clause-before-customers-hit-it/) - [what happens when customers hit token limits](https://startupfortune.com/how-to-write-an-ai-agent-usage-cap-clause-before-customers-hit-it/)

A healthy task finishes in 8 steps. Total input is 6,000 times 8 plus 1,500 times 36, which is 102,000 tokens, or about $0.31. Add roughly $0.04 of output and the task costs about $0.34.

Now the stuck agent. At 200 steps the input total is 6,000 times 200 plus 1,500 times 20,100, which comes to 31.35 million tokens. That's about $94 in input and another $0.90 in output, so roughly $95 for one task that produced nothing. A startup running 250 tasks a day spends about $85 on a normal day. One uncapped loop cost more than the whole day.

It gets worse the longer you leave it. Take the same loop to 1,000 steps and the input total reaches about 757 million tokens, or roughly $2,270. Five times the steps, 24 times the bill.

Prompt caching softens this. Cached input is billed at a fraction of the normal rate with most providers, so your real number will be lower than mine. But caching only discounts the repeated prefix. It doesn't stop the prefix from growing, and it does nothing about the output tokens or the tool calls themselves, which may hit paid APIs of their own. Treat caching as a reason your bill is smaller, not as a reason to skip the cap.

One more multiplier is easy to miss. If your agent spawns sub-agents, each one can loop independently. Three sub-agents each stuck for 100 steps is three loops, and the parent doesn't know any of them are stuck unless you tell it.

## Setting the step cap from your own data

Don't pick 25 because it feels safe, and don't pick 100 because you never want to cut off a legitimate task. Both are guesses. Your logs already hold the real answer.

Pull the step counts for tasks that finished successfully over the last two to four weeks. Throw out the failures, because a loop that ran 400 steps and then got killed will poison the distribution. Sort what's left and find the 95th percentile. If your median successful task takes 7 steps and your p95 is 14, then 95 percent of good work finishes in 14 steps or fewer.

[How to handle LLM API rate limits in production before retries burn your quota](https://startupfortune.com/how-to-handle-llm-api-rate-limits-in-production-before-retries-burn-your-quota/)

A 400,000 tokens-per-minute cap, minus 20% held back as headroom, leaves 320,000 to plan against. Splitting that into lanes puts 96,000 on user-facing calls, and a background step costing 18,000 tokens supports only about twelve steps a minute. - [how to handle LLM API rate limits](https://startupfortune.com/how-to-handle-llm-api-rate-limits-in-production-before-retries-burn-your-quota/) - [prevent retries from burning API quota](https://startupfortune.com/how-to-handle-llm-api-rate-limits-in-production-before-retries-burn-your-quota/)

Set the cap at 1.5 to 2 times that number. At a p95 of 14, a cap of 21 to 28 is a sensible range, and 25 is fine. You're leaving room for the hard tasks that need more steps than usual without leaving room for a loop to run away. If the cap sits at 3 or 4 times the p95, you're paying for the loop to run for a long time before anything stops it.

Segment before you trust a single number. A refund-lookup agent and a multi-document research agent don't share a step profile. If you run one global cap, it'll be too tight for the research agent and too loose for the lookup agent. Compute p95 per agent type, or per task class if you have them, and store the caps in config rather than code so you can change them without a deploy.

Revisit the number every month. The cap drifts out of date as you add tools, change prompts or swap models, and a new model that plans better may bring your p95 down. If more than 1 or 2 percent of tasks hit the cap, either the cap is too tight or something upstream is broken, and both are worth knowing.

Most frameworks give you the knob already. The OpenAI Agents SDK takes a max_turns argument on its runner and raises a MaxTurnsExceeded error when you pass it. Anthropic's Claude Agent SDK has a maxTurns option, and LangGraph raises a GraphRecursionError when a run exceeds its recursion limit. Check your framework's current default before you ship, because defaults change between versions and some are set high enough to be useless as a safety net. Whatever the name, you're setting a ceiling on the number of model calls in one run.

## Catch repeats before the cap does

A step cap is a blunt tool. It stops a loop at step 25, but a loop that was clearly stuck at step 4 has already wasted 21 turns by then. Repeat detection is the sharper check, and it's the one that saves real money.

The mechanics are simple. After each tool call, build a key from the tool name and its arguments, normalized so that harmless differences don't hide a repeat. Sort the JSON keys, trim whitespace, lowercase where case doesn't matter, and drop fields like request IDs or timestamps that change every call. Hash the result and keep a counter per task. If the same key comes up three times with the same error, stop.

Three is a sensible threshold. Two identical calls can be legitimate, such as polling a job status or retrying after a real transient failure. By the third identical failure you're paying for a definition of insanity. If your tools are slow to settle, loosen the rule for those specific tools rather than raising the number for everything.

Exact matching misses the sneaky version, where the agent changes one character each time. It adds a space, flips a flag, swaps a field order that your normalizer already ignores, or alternates between two calls in a cycle. Two extra checks cover most of it. Track the last six or so keys and flag any pattern where the same two or three keys repeat in sequence, and count consecutive errors from the same tool regardless of arguments. Five failures in a row from search_orders is a signal even when every call looked slightly different.

Frankly, this check is worth more than the step cap. The cap protects you from the unknown. Repeat detection protects you from the one failure that accounts for most of your loops, and it fires early.

When it fires, don't kill the run right away. Do something gentler first: inject a short message into the context, something like 'This call has failed three times with the same error. Try a different approach or report that you can't complete the task.' Many agents will recover. If the next step is another repeat, then stop for real.

## A dollar ceiling on every task

Step caps and repeat checks both count events. Neither of them counts money, and money is what your finance lead cares about. A 20-step task that reads huge documents can cost more than a 60-step task that calls small tools. So add a third limit denominated in dollars.

Derive it the same way you derived the cap. Take the cost of successful tasks, find the median and the p99, and set the ceiling at about 10 times the median, or 2 times the p99, whichever is lower. In my worked example the median task costs $0.34, which puts the ceiling around $3. A normal task never gets close, and a runaway task is stopped at $3 instead of $95.

You need a running total to enforce it. After every model response, read the token counts the provider returns in the usage field, multiply by your per-token prices, add any paid tool calls such as search or scraping APIs, and compare the total with the ceiling before you start the next step. Check before the call, not after, so a single expensive step can't blow past the limit by a mile. Some SDKs build this in. Anthropic's Claude Agent SDK, for example, exposes a maxBudgetUsd option next to maxTurns, so check whether yours does before you write your own meter.

Two details trip people up. First, sub-agents should draw from the parent's budget, not carry a fresh one each. If the parent has $3 and spawns three workers, they share that $3. Second, put an account-level limit behind the per-task one. A per-task ceiling won't help if a bug spawns ten thousand tasks, so set a daily spend alarm at your provider and a per-customer ceiling in your own system. The per-task limit catches one bad run, and the account limit catches a bad deploy.

## What to do when a limit trips

The default behavior in many frameworks is to raise an exception and lose everything the agent did. That's a bad outcome, because the agent may have finished 90 percent of the work before it got stuck on the last step.

Catch the limit error and return what you have. Tell the user, or the calling system, which step failed and why, with the partial results attached. A message like 'I found the three matching orders but couldn't pull shipping status because the carrier API rejected the request' is far more useful than a generic failure, and it keeps the customer from retrying the whole thing from scratch.

Don't let a wrapper retry the task automatically. This is the quiet way a capped loop turns back into an uncapped one. If your queue worker retries failed jobs three times and each attempt runs to the cap, you've multiplied the damage by four. Tool-level retries with backoff, capped at three, are fine. Whole-task retries need their own limit, and for tasks that tripped a loop guard, the right number is usually zero until a human or a different prompt has looked at them.

Log every trip with the full trace: the task ID, which limit fired, the last ten tool calls, the arguments, the errors and the running cost. Then alert on the rate, not on individual events. A single trip is noise. If the share of tasks hitting a limit jumps from 0.5 percent to 4 percent after a deploy, you've shipped a regression, and the tripped traces will tell you which tool broke.

Also check the dull cause behind a lot of these loops: bad tool errors. If a tool returns '400 Bad Request' with nothing else, the model has nothing to reason from and tries the same thing again. Return the specific problem, such as 'date must be YYYY-MM-DD, you sent 11/18/2026', and a surprising number of loops stop being loops. It's the cheapest fix on this list, and it costs a few lines in your tool wrappers.

Wire all three guards into the same place, ideally the loop controller itself and not scattered across tools, so that every agent you build later inherits them. Start with the p95 query this week. It takes an afternoon, and at $85 for a normal day against $95 for one stuck task, the afternoon pays for itself the first time a loop trips.

**Also read:** [How to handle LLM API rate limits in production before retries burn your quota](https://startupfortune.com/how-to-handle-llm-api-rate-limits-in-production-before-retries-burn-your-quota/) • [How to Write AI Agent Tool Descriptions That Stop Wrong Tool Calls](https://startupfortune.com/how-to-write-ai-agent-tool-descriptions-that-stop-wrong-tool-calls/) • [How to Compare LLM API Cost Per Million Tokens Before You Pick a Model](https://startupfortune.com/how-to-compare-llm-api-cost-per-million-tokens-before-you-pick-a-model/)

*This article is posted in [AI News](https://startupfortune.com/category/ai/), check it out for more related stories.*

## Join the discussion

[Open in the community →](https://startupfortune.com/community/)

Almost there. Sign in and your reply posts straight away.
