A production AI agent just burned $47,000 in 11 days through infinite tool-call loops. The agent would call a tool, get a result it didn't like, call the tool again with slightly different parameters, get another unsatisfactory result, and loop — endlessly. The fix? Seven lines of code. Every team running AI agents in production will hit this. Most won't know until the bill arrives.
What Is a Tool-Call Loop?
AI agents work by calling tools — APIs, database queries, web searches, file operations. The agent decides which tool to call, what parameters to use, and what to do with the result. Most of the time, this works fine.
But sometimes the agent gets stuck. It calls a tool, doesn't get the result it expects, and instead of stopping or asking for help, it tries again. And again. And again.
Here's what a tool-call loop looks like:
Agent: "Let me search for the customer record."
→ Tool: search_customer("john@example.com")
→ Result: No records found
Agent: "Let me try a different search."
→ Tool: search_customer("john@example")
→ Result: No records found
Agent: "Let me try another variation."
→ Tool: search_customer("john")
→ Result: 500 records found (too many)
Agent: "Too many results. Let me narrow it down."
→ Tool: search_customer("john@")
→ Result: No records found
[...this continues for hours, burning tokens each cycle]
Each loop iteration costs money — the agent processes the previous results, generates new reasoning, and makes another API call. At scale, with frontier models, a single stuck agent can burn hundreds of dollars per hour.
This isn't a rare edge case. It's the single most common preventable failure in production AI agents. Every team deploying agents at scale has encountered it, or will.
Why Tool-Call Loops Happen
1. No Maximum Retry Limits
Most agent frameworks don't set a default maximum for tool calls per task. An agent can theoretically call tools infinitely. Nobody configured a limit because nobody expected the agent to get stuck.
2. Agents Don't Know When to Stop
LLMs are completion engines — they always try to "finish" the task. When a tool call fails, the agent's instinct is to try harder, not to give up. There's no built-in concept of "I should stop and ask for help."
3. Error Recovery Looks Like Retry
When a tool returns an error, the agent's training data suggests retrying with different parameters. This works for humans (who stop after 2-3 attempts) but agents have no fatigue mechanism. They'll retry 1,000 times with the same enthusiasm as the first attempt.
4. No Cost Awareness
Agents don't know how much each call costs. They don't see the bill accumulating. There's no internal mechanism that says "you've spent $500 on this task, maybe stop."
5. Silent Failures
Tool-call loops often produce no visible errors. The agent is "working" — it's making calls, getting responses, reasoning about results. Monitoring systems see normal activity. The only anomaly is the bill at the end of the month.
The 7-Line Fix
The solution is embarrassingly simple. Here's the pattern:
1. Set a Maximum Tool-Call Count
Hard limit per task. No exceptions. If the agent hasn't completed the task in 15 tool calls, stop and escalate.
2. Implement Duplicate Call Detection
If the agent calls the same tool with the same (or very similar) parameters twice in a row, flag it and pause.
3. Add Cost Thresholds
Track cumulative cost per task. If a single task exceeds a cost threshold (e.g., $5), stop and alert.
4. Circuit Breakers
If a tool fails 3 times in a row, mark it as degraded and stop calling it. This is standard in distributed systems — apply it to AI agents.
5. Timeout Limits
Set a maximum execution time per task. If the agent hasn't completed in 5 minutes, terminate and report.
6. Logging and Alerting
Log every tool call with timestamp, parameters, and result. Set alerts for unusual call volumes.
7. Human Escalation
When any safeguard triggers, route the task to a human with full context — not silently discard it.
Honest caveat: These safeguards add overhead. Maximum call counts might terminate agents that genuinely need 20 calls for complex tasks. Cost thresholds need calibration per use case. Circuit breakers might flag tools that are intermittently slow, not broken. The key is tuning these parameters for your specific workload, not copying generic values.
The Financial Impact
| Scenario | Daily Cost | Monthly Cost | Annual Cost | |----------|-----------|--------------|-------------| | No safeguards, 1 agent stuck/week | $4,273 | $128,182 | $1,538,182 | | With safeguards (7-line fix) | $0 (auto-stopped) | $50 (alert overhead) | $600 | | Net savings | — | — | $1,537,582 |
For the specific $47K case:
- Duration: 11 days before detection
- Cost per day: ~$4,273
- Root cause: No maximum tool-call count, no cost threshold, no circuit breaker
- Fix implementation time: 2 hours (7 lines + testing)
Even a conservative estimate — one stuck agent per month burning $2,000 before detection — costs $24,000/year. The 7-line fix costs 2 hours of engineering time.
The Bigger Picture
The $47K bug isn't an isolated incident. It's a symptom of a systemic problem: production AI agents are deployed without the basic safety mechanisms that have been standard in distributed systems for decades.
Circuit breakers, retry limits, timeouts, and health checks are table stakes for any production service. Yet AI agents — which make autonomous decisions and spend money on every action — are routinely deployed without any of these.
The community is catching on. IBM's Futile Cycle Detection research (F1=0.72 on detecting stuck agents) is one approach. Manifest (500 stars/day) includes smart routing that detects cost anomalies. Pydantic AI v1.85 added production evaluation via OpenTelemetry.
But the fundamental fix is cultural: treat AI agents like production services, not experiments. Apply the same operational rigor — limits, monitoring, alerts, escalation — that you'd apply to any service that can autonomously spend your money.
Closing Thoughts
Seven lines of code would have saved $47,000. That's the most expensive missing code in AI production. And it's not unique — every company running agents without safeguards is one bad prompt away from their own $47K surprise.
The fix isn't complex. It's not expensive. It's not even interesting engineering. It's just basic operational discipline applied to a new class of system. If you're running AI agents in production without maximum tool-call limits, cost thresholds, and circuit breakers, you're not deploying AI — you're deploying an uncontrolled spending machine with a chat interface.
Fix it today. Not after the next bill arrives.
Running AI agents without safeguards? Book an Agent Safety Audit — we'll review your agent architecture, implement tool-call loop prevention, and set up cost monitoring to prevent expensive surprises.