How do you stop an agent stuck in a loop from running up a huge API bill?
In production, one retry bug or an agent that keeps looping can fire off thousands of model calls and turn a normal workflow into an expensive one overnight. What are people actually doing to cap this before the invoice arrives?
Treat cost like any other resource limit and enforce it in code, not in the prompt. Telling a model to be careful does nothing once it is stuck in a loop; a counter that refuses the next call does.
Put three hard stops around every agent run: a maximum number of steps or tool calls, a maximum number of tokens or dollars per run, and a wall-clock timeout. When any of them trips, end the run and save its state so a person can look at it, instead of retrying automatically.
Retries need their own rules. Use a small retry count with backoff, and never let a failed step retry the whole task from the top. Most surprise bills come from a retry wrapping a loop wrapping another retry.
Above the run level, set daily budgets per feature or per customer and log the cost of every call with the run it belongs to. That is what lets you spot a single run spending ten times the normal amount within minutes, and it is also where cheaper model routing and caching start to pay off.
Listings mentioned
- LLM cost optimizer · skill by alirezarezvaniCovers per-endpoint max_tokens caps, per-feature budget envelopes with hard limits, cost logging, routing and caching.
- Observability Designer (POWERFUL) · skill by alirezarezvaniHelps design the metrics and alerts that catch a run whose spend jumps far above normal.
Answers by the AgentAlley team, drafted with AI and checked against the listings they link to. Not a real-person reply from the original thread.