# 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.

<div data-sim="specx" data-spec="{&#34;head&#34;:&#34;Agent step limits: how a turn cap trades solved tasks against cost per resolved task&#34;,&#34;sub&#34;:&#34;Thirty coding-agent runs, each with the number of steps it needed to finish on its own and whether its fix passed the tests. Set the step limit and the price of a step, and see which runs get cut off and what each resolved task costs.&#34;,&#34;params&#34;:[{&#34;id&#34;:&#34;lim&#34;,&#34;label&#34;:&#34;Step limit&#34;,&#34;min&#34;:3,&#34;max&#34;:45,&#34;step&#34;:1,&#34;value&#34;:30,&#34;note&#34;:&#34;Like max_turns in the Claude Agent SDK or the OpenAI Agents SDK&#34;},{&#34;id&#34;:&#34;cstep&#34;,&#34;label&#34;:&#34;Cost per step&#34;,&#34;min&#34;:0.01,&#34;max&#34;:1,&#34;step&#34;:0.01,&#34;value&#34;:0.1,&#34;unit&#34;:&#34;$&#34;,&#34;note&#34;:&#34;SWE-agent&#39;s successful runs had a median of $1.21 over 12 steps&#34;},{&#34;id&#34;:&#34;qsave&#34;,&#34;label&#34;:&#34;Chance a cut-off run still passes&#34;,&#34;min&#34;:0,&#34;max&#34;:0.5,&#34;step&#34;:0.01,&#34;value&#34;:0.1,&#34;note&#34;:&#34;Share of would-be fixes that pass when the edits so far are submitted at the limit&#34;}],&#34;data&#34;:{&#34;columns&#34;:[&#34;task&#34;,&#34;steps&#34;,&#34;resolved&#34;],&#34;rows&#34;:[[1,12,1],[2,23,0],[3,9,1],[4,40,0],[5,15,0],[6,5,1],[7,19,1],[8,27,0],[9,13,1],[10,18,0],[11,10,1],[12,33,0],[13,6,0],[14,24,1],[15,21,0],[16,12,1],[17,17,0],[18,8,1],[19,29,0],[20,31,1],[21,20,0],[22,11,1],[23,9,0],[24,25,0],[25,15,1],[26,22,0],[27,7,1],[28,12,0],[29,17,1],[30,19,0]],&#34;note&#34;:&#34;Illustrative SWE-bench-style runs modelled on the statistics reported for SWE-agent: successful runs had a median of 12 steps and unsuccessful ones a mean of 21. steps: model turns the run needs to finish by itself. resolved: 1 if its patch passes the tests when it is allowed to finish.&#34;},&#34;row&#34;:[{&#34;id&#34;:&#34;done&#34;,&#34;label&#34;:&#34;finishes within the limit&#34;,&#34;expr&#34;:&#34;ge(lim, steps)&#34;,&#34;fmt&#34;:&#34;i&#34;,&#34;tex&#34;:&#34;f_i = [\\,s_i \\le L\\,]&#34;},{&#34;id&#34;:&#34;ok&#34;,&#34;label&#34;:&#34;counts as resolved&#34;,&#34;expr&#34;:&#34;resolved*(done + (1 - done)*qsave)&#34;,&#34;tex&#34;:&#34;r_i^{L} = y_i\\,\\big(f_i + (1-f_i)\\,q\\big)&#34;},{&#34;id&#34;:&#34;used&#34;,&#34;label&#34;:&#34;steps used&#34;,&#34;expr&#34;:&#34;min(steps, lim)&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;cost&#34;,&#34;label&#34;:&#34;cost ($)&#34;,&#34;expr&#34;:&#34;min(steps, lim)*cstep&#34;,&#34;tex&#34;:&#34;c_i = \\min(s_i, L)\\,c&#34;},{&#34;id&#34;:&#34;lns&#34;,&#34;label&#34;:&#34;ln(steps)&#34;,&#34;expr&#34;:&#34;ln(steps)&#34;,&#34;hide&#34;:true}],&#34;derived&#34;:[{&#34;id&#34;:&#34;nres&#34;,&#34;label&#34;:&#34;Runs that resolve with no limit&#34;,&#34;expr&#34;:&#34;sum(resolved)&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;base&#34;,&#34;label&#34;:&#34;Resolve rate with no limit&#34;,&#34;expr&#34;:&#34;mean(resolved)&#34;,&#34;fmt&#34;:&#34;p&#34;},{&#34;id&#34;:&#34;solve&#34;,&#34;label&#34;:&#34;Resolve rate at this limit&#34;,&#34;expr&#34;:&#34;mean(ok)&#34;,&#34;fmt&#34;:&#34;p&#34;,&#34;tex&#34;:&#34;R(L) = \\frac{1}{n}\\sum_i r_i^{L}&#34;},{&#34;id&#34;:&#34;ncut&#34;,&#34;label&#34;:&#34;Runs cut off by the limit&#34;,&#34;expr&#34;:&#34;count(done) - sum(done)&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;lostres&#34;,&#34;label&#34;:&#34;Would-be fixes cut off&#34;,&#34;expr&#34;:&#34;nres - dot(resolved, done)&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;spend&#34;,&#34;label&#34;:&#34;Cost of all runs ($)&#34;,&#34;expr&#34;:&#34;sum(cost)&#34;},{&#34;id&#34;:&#34;cpr&#34;,&#34;label&#34;:&#34;Cost per resolved task ($)&#34;,&#34;expr&#34;:&#34;spend/sum(ok)&#34;,&#34;tex&#34;:&#34;\\text{CPR}(L) = \\frac{\\sum_i c_i}{\\sum_i r_i^{L}}&#34;},{&#34;id&#34;:&#34;waste&#34;,&#34;label&#34;:&#34;Share of spend on unresolved runs&#34;,&#34;expr&#34;:&#34;1 - dot(resolved, cost)/spend&#34;,&#34;fmt&#34;:&#34;p&#34;},{&#34;id&#34;:&#34;mres&#34;,&#34;label&#34;:&#34;Mean steps of resolved runs&#34;,&#34;expr&#34;:&#34;dot(steps, resolved)/nres&#34;},{&#34;id&#34;:&#34;munr&#34;,&#34;label&#34;:&#34;Mean steps of unresolved runs&#34;,&#34;expr&#34;:&#34;(sum(steps) - dot(steps, resolved))/(count(steps) - nres)&#34;},{&#34;id&#34;:&#34;mu_r&#34;,&#34;label&#34;:&#34;Log-normal centre, resolved runs&#34;,&#34;expr&#34;:&#34;dot(lns, resolved)/nres&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;mu_a&#34;,&#34;label&#34;:&#34;Log-normal centre, all runs&#34;,&#34;expr&#34;:&#34;mean(lns)&#34;,&#34;hide&#34;:true}],&#34;row2&#34;:[{&#34;id&#34;:&#34;d2r&#34;,&#34;label&#34;:&#34;sq dev resolved&#34;,&#34;expr&#34;:&#34;resolved*(lns - mu_r)^2&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;d2a&#34;,&#34;label&#34;:&#34;sq dev all&#34;,&#34;expr&#34;:&#34;(lns - mu_a)^2&#34;,&#34;hide&#34;:true}],&#34;post&#34;:[{&#34;id&#34;:&#34;sig_r&#34;,&#34;label&#34;:&#34;Log-normal spread, resolved runs&#34;,&#34;expr&#34;:&#34;sqrt(sum(d2r)/(nres - 1))&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;sig_a&#34;,&#34;label&#34;:&#34;Log-normal spread, all runs&#34;,&#34;expr&#34;:&#34;sqrt(sum(d2a)/(count(d2a) - 1))&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;med_r&#34;,&#34;label&#34;:&#34;Typical length of a resolved run (steps)&#34;,&#34;expr&#34;:&#34;exp(mu_r)&#34;,&#34;tex&#34;:&#34;\\tilde s = e^{\\mu},\\; \\mu = \\overline{\\ln s_i}&#34;},{&#34;id&#34;:&#34;model_res&#34;,&#34;label&#34;:&#34;Model: resolve rate at this limit&#34;,&#34;expr&#34;:&#34;base*Phi((ln(lim) - mu_r)/sig_r)&#34;,&#34;fmt&#34;:&#34;p&#34;,&#34;tex&#34;:&#34;\\hat R(L) = \\bar y\\;\\Phi\\!\\left(\\frac{\\ln L - \\mu_r}{\\sigma_r}\\right)&#34;},{&#34;id&#34;:&#34;model_cut&#34;,&#34;label&#34;:&#34;Model: share of runs cut off&#34;,&#34;expr&#34;:&#34;1 - Phi((ln(lim) - mu_a)/sig_a)&#34;,&#34;fmt&#34;:&#34;p&#34;}],&#34;chart&#34;:{&#34;title&#34;:&#34;Runs by length and outcome, with the share resolved under a limit&#34;,&#34;x&#34;:{&#34;var&#34;:&#34;t&#34;,&#34;label&#34;:&#34;Step limit (or steps a run needs)&#34;,&#34;from&#34;:3,&#34;to&#34;:45},&#34;y&#34;:{&#34;from&#34;:-0.05,&#34;to&#34;:1.05},&#34;series&#34;:[{&#34;label&#34;:&#34;Model: share resolved within this limit&#34;,&#34;expr&#34;:&#34;base*Phi((ln(t) - mu_r)/sig_r)&#34;},{&#34;label&#34;:&#34;Model: share of runs still going (cut off)&#34;,&#34;expr&#34;:&#34;1 - Phi((ln(t) - mu_a)/sig_a)&#34;,&#34;dash&#34;:true}],&#34;points&#34;:{&#34;x&#34;:&#34;steps&#34;,&#34;y&#34;:&#34;resolved&#34;,&#34;label&#34;:&#34;Runs (1 = resolved if allowed to finish)&#34;},&#34;mark&#34;:&#34;lim&#34;},&#34;level&#34;:&#34;intermediate&#34;,&#34;bars&#34;:{&#34;title&#34;:&#34;Cost of each run under this limit ($)&#34;,&#34;y&#34;:&#34;cost&#34;,&#34;label&#34;:&#34;task&#34;},&#34;steps&#34;:[{&#34;title&#34;:&#34;Look at the run lengths&#34;,&#34;text&#34;:&#34;Resolved runs are short and unresolved ones long: {mres} steps on average against {munr}. With no limit, {nres} runs resolve ({base:p}).&#34;,&#34;show&#34;:[&#34;mres&#34;,&#34;munr&#34;,&#34;base&#34;]},{&#34;title&#34;:&#34;Apply the step limit&#34;,&#34;text&#34;:&#34;A run that needs more than {lim} steps is stopped. Here {ncut} runs hit the limit, and {lostres} of them would have produced a passing fix.&#34;,&#34;show&#34;:[&#34;ncut&#34;,&#34;lostres&#34;]},{&#34;title&#34;:&#34;Count what still resolves&#34;,&#34;text&#34;:&#34;Runs that finish keep their outcome; a cut-off would-be fix passes only with chance {qsave}. The resolve rate becomes {solve:p}.&#34;,&#34;show&#34;:[&#34;solve&#34;]},{&#34;title&#34;:&#34;Price every run&#34;,&#34;text&#34;:&#34;Each run pays for the steps it used, up to the limit: ${spend} in all, and {waste:p} of it goes to runs that never resolve.&#34;,&#34;show&#34;:[&#34;spend&#34;,&#34;waste&#34;]},{&#34;title&#34;:&#34;Divide by the wins&#34;,&#34;text&#34;:&#34;Cost per resolved task is ${cpr}. A tighter limit cuts the long, hopeless runs but also some slow successes, so this number has a minimum.&#34;,&#34;show&#34;:[&#34;cpr&#34;]},{&#34;title&#34;:&#34;Fit a model to choose the limit&#34;,&#34;text&#34;:&#34;A log-normal fit to the run lengths (typical resolved run {med_r} steps) predicts a {model_res:p} resolve rate and {model_cut:p} of runs cut off at this limit.&#34;,&#34;show&#34;:[&#34;med_r&#34;,&#34;model_res&#34;,&#34;model_cut&#34;]}]}"><p>Interactive simulation. It needs JavaScript; the text below explains what it shows.</p></div>

## 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](/tools/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](/tools/openai-agents-sdk) raises `MaxTurnsExceeded` when a run passes `max_turns`, counting each LLM call as a turn. [LangGraph](/tools/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](/ai-simulations/swe-bench-confidence-cost-per-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](/tools/langsmith) or [Langfuse](/tools/langfuse) traces, paste them in, and set the limit where the model curve flattens. The [agent loop token cost](/ai-simulations/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](/tools/long-running-tasks) and [cost and latency budgets](/tools/cost-and-latency-budgets).

## Sources

- [Yang et al., SWE-agent: Agent-Computer Interfaces Enable Automated Software Engineering (arXiv 2405.15793)](https://arxiv.org/abs/2405.15793)
- [Claude Agent SDK docs: How the agent loop works](https://code.claude.com/docs/en/agent-sdk/agent-loop)
- [OpenAI Agents SDK docs: Running agents](https://openai.github.io/openai-agents-python/running_agents/)
- [LangGraph docs: Graph API](https://docs.langchain.com/oss/python/langgraph/graph-api)
- [LangChain docs: Built-in middleware](https://docs.langchain.com/oss/python/langchain/middleware/built-in)
