New Tool Radar

AI Agents & Automation

How to Set a Max-Steps Stop Condition on an Agent Loop

Cap an agent loop at a fixed step count so a stuck agent stops instead of running forever.

Schematic with three numbered boxes: estimate normal steps, set the cap, watch the log.

An agent loop runs until the task is done, but a confused agent can loop forever, calling the same tool again and again. A max-steps stop condition ends the loop after a fixed number of steps, so you get a partial result and a log instead of an endless bill. Set the cap before the first run.

Checked on 2026-11-29. We did not run any tool for this guide. Every result below is an expected result, marked UNTESTED, for you to confirm on your own files.

Prerequisites

  • An agent loop you can configure
  • A rough idea of how many steps the task should take
  • Access to the run log

Steps

1. Estimate the normal step count

Run the task once by hand and count the steps, then add a margin of about half as many again.

Expected result: A normal step count is written down, for example 6 steps.

2. Set the max-steps cap

Set the cap to the normal count plus margin, for example 10 steps for a 6-step task.

Expected result: A cap number is set in the agent config.

3. Run and watch the log

Run the agent and watch the log. It should finish before the cap on a normal task.

Expected result: The run finishes before the cap, or hits the cap and stops.

4. Read the stopped run

If the run hit the cap, read the log to see where it looped, then fix the prompt or tool before rerunning.

Expected result: The loop point is named in the log, with a fix planned.

Worked example

A practice research task normally takes 6 steps. The cap was set to 10 and the agent finished in 7.

Run Steps used Hit the cap?
Normal task 7 No
Stuck task 10 Yes
Cap set 10 –
Runs observed 27

Expected result: The normal run uses 7 steps and the stuck run stops at 10, so no run loops forever (UNTESTED – confirm it yourself.)

Verdict table

Check Expected result Pass?
Estimate the normal step count A normal step count is written down, for example 6 steps. –
Set the max-steps cap A cap number is set in the agent config. –
Run and watch the log The run finishes before the cap, or hits the cap and stops. –
Read the stopped run The loop point is named in the log, with a fix planned. –

Limits

  • A cap that is too low stops good work early; a cap that is too high wastes tokens on stuck runs.
  • The cap does not fix the cause of the loop; read the log and fix the prompt or tool.

Sources

See all