Agent loops: when an agent won't stop

Why does an AI agent sometimes keep trying the same thing, and how do you make it stop?

In production · about 5 minutes

An agent keeps repeating a step that isn't working, and you pay for every try. Only the model decides when it's done, so your code has to decide when it must stop.

Picture: a robot sends the same search to a tool that says no results, over and over, try 1, 2, 3 up to 40 with no cap (made up), while a row of coins fades away