How we stopped our AI agents from burning $400 overnight on infinite loops
Last month, I woke up to a $412 API bill. A background research agent got stuck retrying a failing web scraper for six hours straight. We had built prompts.. We forgot to put hard limits on how many times an agent can try the same sub-task.
To fix this, we stopped letting the model decide when to stop. We built an outside step counter into our backend wrapper. If an agent calls the tool twice with identical parameters, our system pauses the run right away. It flags the run for a person to review of letting it guess again.
We also added a token budget for each task. Once the agent hits 80 percent of its limit, it has to summarize whatever data it has gathered. This keeps finished runs from failing completely without giving us usable output.
Since we added these two checks, our average cost per agent run dropped by 65 percent. Overnight billing surprises stopped entirely. Always keep execution limits from the models reasoning logic. How are you handling loops in your production AI agents?
p/scrimba
p/overads-io
p/runevr
p/google-cloud-platform
p/producthunt
p/meridian-16
p/many-displays
p/retainiq-2
p/kodlo
p/reso
p/dots-7
p/termexo
p/zush
p/htmldrag
p/murror
p/gatrends
p/comcent
p/expert-chase-deleted-1107920
p/statefold-2
p/contral
Replies
Hard agree on never letting the model decide when to stop. Prompts get ignored the second an agent gets stuck on a failing tool call.
I hit the exact same wall with AI coding agents burning through daily limits on retry loops, so I built TokenSafe (`npx tokensafe`) to sit locally in front of them. It catches repeated identical calls and breaks the loop right away, remembers failed paths so the agent stops retrying the same dead end on the next run, and cuts bloated file reads and noisy logs before they eat your budget.
Curious if your wrapper also remembers failing sub-tasks across runs, or if you reset that state per task?