New Tool Radar

AI Agents & Automation

How to Set a Step Limit So a Runaway Agent Loop Stops Itself

Cap the number of steps an agent may take, so a loop that never finishes stops itself instead of burning your budget.

Schematic illustration with three step boxes: count normal steps, set the cap, and test the stop.

An agent is a loop: a goal, a model, tools, and a stop condition, because loops that never stop cost money. A step limit is the simplest stop condition: after N steps, the run ends whether it finished or not. Without one, a confused agent can retry the same failing tool call until the budget is gone.

Before you start

An agent loop where you can set a maximum step count, and a log of how many steps normal runs take.

Count how many steps normal runs take

Look at the last 20 successful runs and record the step count for each. Note the highest normal count: the cap must sit above it, or normal work will be cut off.

Note the task types separately: a research task takes more steps than a lookup, and one cap for both will be wrong for one of them.

Set the cap above normal, below disaster

Set the cap at about twice the highest normal count for that task type. Twice gives room for a retry or two, and still stops a loop that runs ten times too long.

Write the cap into the loop code, not into the prompt: the model cannot be trusted to count its own steps.

Test the stop on a looping task

Run a task designed to loop, such as a tool that always errors, and confirm the run stops at the cap with a clear message and a log entry. Confirm no further tool calls happen after the stop.

Check the cost of the stopped run: it should be bounded by the cap, not by the budget.

Practice check: fictional Maple Corner agent, lookup tasks, cap 20 steps

The table below is fictional practice material, worked out by hand from the stated step counts. It is the expected result for the practice tasks, not a run of any agent.

Task Steps taken Cap Verdict
Normal lookup 6 20 Pass – well under
Hard lookup 11 20 Pass – under cap
Looping task 20 20 Pass – stopped at cap
No cap, looping task 87 None Fail – burned budget

Normal lookups take 6 to 11 steps, so a cap of 20 leaves room and still stops the looping task at 20 steps. Without the cap the same task ran 87 steps.

Expected result. UNTESTED: the step counts in the practice table are worked out by hand, not measured by running an agent. Count the steps in your own last 20 successful runs, set the cap at twice the highest normal count, and test it on a looping task.

Limits

  • A cap that is too low cuts off normal work: set it from measured step counts, not from a guess.
  • The step limit is a safety net, not a fix: a run that hits the cap still needs its cause investigated.

Sources

See all