Why do AI agents get stuck in loops, and how do you design stop conditions?
A classic agent failure: it tries a command, it fails, it tries the same command again, and again. Or it keeps "improving" a draft forever. Loops happen because the agent loop itself has no natural end; the model decides when it's done.
Why loops happen
- The model doesn't register that a previous attempt already failed (often because the error message was vague or truncated).
- The goal is fuzzy, so there's no clear signal of "done."
- The agent lacks a tool that would actually solve the problem, so it keeps reaching for the closest one.
Stop conditions worth adding
- Hard limits: a maximum number of steps, tool calls, tokens or wall-clock minutes.
- Repetition detection: if the same tool is called with identical arguments several times, interrupt.
- Explicit success criteria: tests pass, a file exists, a schema validates. Concrete checks beat "looks good."
- Escalation: instead of failing silently, the agent stops and asks a human with a summary of what it tried.
Design tip
Clear, specific error messages from your tools do a lot of work. "Permission denied writing /data/out.csv" gives the model something to reason about; "Error" invites a retry.
What limits do you set by default? And do you prefer an agent that gives up early or one that persists and occasionally burns budget?
Comments · 4
Capping the number of tool calls or steps is the simplest guardrail. Even a hard stop after ten steps beats an agent that runs forever and burns tokens.
Cost limits are a good stop condition too. Set a budget per task so a runaway loop becomes a small bill and not a big surprise.
How do you detect that the agent is repeating itself? Compare the last few actions for similarity?
Keeping a short history of recent actions and flagging near-identical ones works well. When it triggers, force a different strategy or hand control to a human.