Agent step limits: how a turn cap trades solved tasks against cost per resolved task
Every production agent needs a cap on its turns, because failing runs tend to run long. Set the limit too low and you cut off slow successes; too high and you pay for hopeless loops. Move the limit and watch the resolve rate, the cut-off runs and the cost of each resolved task.
Interactive simulation. It needs JavaScript; the text below explains what it shows.
What you are looking at
Each row is one run of a coding agent on a SWE-bench-style task. steps is the number of model turns the run needs to finish on its own, and resolved says whether its patch passes the tests when it is allowed to finish. The data follow the pattern reported for SWE-agent: successful runs are short, with a median of 12 steps, and unsuccessful ones are longer, with a mean of 21.
The orange columns apply your limit to each run: whether it finishes in time, whether it still counts as resolved, the steps it uses and what it costs. On the chart every run is a point at its length, at 1 if it resolves and 0 if it does not, and the vertical line is your limit. The solid curve is a model of the share of runs that resolve within a given limit, and the dashed curve is the share of runs still going at that point, the ones a limit there would cut off. The bars show the cost of each run.
The algorithm
Let s_i be the steps run i needs, y_i its outcome, L the step limit and c the cost of one step.
Cut off. Run i finishes if s_i ≤ L. A cut-off run keeps whatever edits it made; like SWE-agent, which submits the existing edits when a run exceeds its budget, a cut-off would-be fix passes with a small chance q. So run i counts as resolved with weight y_i × (f_i + (1 − f_i) × q), where f_i is 1 if it finishes.
Resolve rate. R(L) is the mean of those weights over all n runs.
Cost. A run pays for the steps it uses: c_i = min(s_i, L) × c. Cost per resolved task is the total cost divided by the number of resolved tasks.
Model. Run lengths are skewed: many short runs and a long tail. A log-normal fit captures that: take μ and σ as the mean and standard deviation of ln(s) over the resolved runs. The model resolve rate is ȳ × Φ((ln L − μ) / σ), where ȳ is the resolve rate with no limit and Φ the normal CDF. The same fit over all runs gives the share cut off.
Worked example. With no limit, 14 of the 30 runs resolve: 46.7%. Resolved runs take 13.8 steps on average and unresolved ones 21.0. At the default limit of 30 steps, 3 runs are cut off (tasks 4, 12 and 20, which need 40, 33 and 31 steps), and one of them, task 20, would have resolved. With q = 0.1 it still counts as 0.1 of a fix, so the resolve rate is (13 + 0.1) / 30 = 43.7%. At $0.10 a step the runs cost $51.50, of which 62.7% goes to runs that never resolve, and each resolved task costs 51.5 / 13.1 = $3.93. The log-normal fit puts a typical resolved run at 12.3 steps and predicts a 45.1% resolve rate and 10.6% of runs cut off at this limit, close to the 43.7% and 10% in the data.
Things to try
- Raise the limit to 31. Task 20 now finishes, every would-be fix survives (46.7%), and cost per resolved task falls to $3.70, cheaper than at 30, because one extra step bought a whole fix.
- Lower the limit to 15. The resolve rate falls to 34.7% and 4 would-be fixes are cut off, yet cost per resolved task is $3.69: the long, hopeless runs were costing as much as the lost fixes.
- Lower it to 10. Now 23 runs are cut off, 9 of them would-be fixes, and cost per resolved task climbs to $4.81. Too tight a limit is both worse and dearer.
- Set the chance a cut-off run still passes to 0. At a limit of 30 the resolve rate falls to 43.3% and cost per resolved task rises to $3.96: auto-submitting partial work is worth something.
- Sweep the limit from 10 to 45 and watch cost per resolved task: it zigzags between $3.63 and $3.94 above a limit of 12 because each lost fix is a big share of only 14. With a sample this small, choose the limit from the model curve, not from the lowest point.
How it works in real systems
Every agent framework has a cap. The Claude Agent SDK has max_turns, which counts tool-use round trips and has no limit by default, and max_budget_usd, a spend cap; hitting either ends the loop with a result of subtype error_max_turns or error_max_budget_usd, and the docs suggest a budget as a good default for production agents. The OpenAI Agents SDK raises MaxTurnsExceeded when a run passes max_turns, counting each LLM call as a turn. LangGraph counts super-steps against a recursion_limit that defaults to 1,000 from version 1.0.6, raises GraphRecursionError when it is reached, and offers a RemainingSteps value so a graph can wrap up gracefully before the limit. LangChain's ModelCallLimitMiddleware sets run_limit and thread_limit with an exit behaviour of end or error.
Benchmarks use limits too, and they change the scores. SWE-agent set a budget of $4 per instance and reports that 93.0% of resolved instances were submitted before exhausting it, against 69.0% of instances overall: runs that hit the cap are mostly failures. That is why a limit costs fewer fixes than it cuts runs. When you compare agents, report the limit with the score and the cost per resolved task, with a confidence interval, because thirty runs leave wide error bars.
A limit is a blunt tool. Better agents also stop early on signs of a loop: the same tool call with the same arguments, or the same failing test, several times in a row. Read the lengths of your own runs from LangSmith or Langfuse traces, paste them in, and set the limit where the model curve flattens. The agent loop token cost simulation shows why later steps cost more than early ones; this simulation keeps the cost per step flat, which flatters long runs. See also long-running tasks and cost and latency budgets.