← All questions
AgentsMedium· Agents & Orchestration

How do you stop an agent looping forever?

An iteration cap is necessary and not sufficient on its own — it just turns an infinite loop into a truncated failure with no useful output, which is better than a runaway process but still a failure.

What I actually want:

  • A bounded iteration budget, paired with a defined terminal state — what does the agent return when it exhausts its budget, and does the caller know the result is truncated rather than complete?
  • A spend ceiling alongside the iteration cap, since iterations and cost are effectively the same axis for anything calling a paid model.
  • Progress detection — if the agent is repeating the same failing action, stop after one or two repeats rather than letting it retry the same dead end nine more times.

The goal isn’t just “it eventually stops” — it’s that when it stops early, the system and the caller both know exactly why.

Code playground (Python)