← Back to Blog

Four AI Agents, One Infinite Loop, $47,000 in 11 Days

By Craig Lowther, Founder · 6 August 2026 · 5 min read · Cost Guard — Runaway AI Spend

A 4-agent LangChain system was left to hand tasks between itself, one agent to the next, with no exit condition. It ran unattended for 11 days. The bill was $47,000. Nobody noticed until it was over.

4
agents in the loop
11
days unattended
$47k
final bill

What Happened

A multi-agent system built on LangChain was set up so that four agents could hand work off to one another — agent A finishes a step and passes context to agent B, B to C, C to D, and back around. It's a common pattern for breaking a complex task into specialised roles. The problem is what happens when the handoff loop has no exit condition and no budget ceiling: the agents kept the conversation going with each other, each round-trip triggering fresh LLM calls, for eleven straight days. No human was reviewing the session. No cost alert fired in time to matter. By the time anyone looked at the bill, it read $47,000.

This isn't a hypothetical. It's one of a growing pattern of "agent left running, nobody checked the meter" incidents now common enough that vendors are shipping dedicated "token budget enforcement" products specifically to stop it (see Sources below).

Why This Keeps Happening

The failure isn't the model, and it isn't malice. It's a structural gap in how agent loops get written:

The exact code pattern

Strip away the multi-agent framework and the underlying bug is almost always this shape:

// no iteration cap, no budget guard, no token ceiling
while (!taskComplete) {
  const response = await llm.chat.completions.create({
    model: 'gpt-4',
    messages: conversationHistory,
  });
  conversationHistory.push(response);
  taskComplete = checkIfDone(response); // agent decides for itself when to stop
}

The loop's exit condition is entirely inside the agent's own judgement. If the agent never decides it's done — which is exactly what happened here — the loop runs until someone kills the process or the bill forces the issue.

How Thuban Cost Guard Catches This

This is precisely what TCG-101 (Unbounded AI Loop Execution), one of Cost Guard's two v0.1 detection rules, is built for. Cost Guard's static analyzer walks every while, do-while, and for loop in your codebase and checks whether an AI SDK call inside it (OpenAI, Anthropic, Vercel AI SDK) has a genuine bounded exit — a real iteration counter, a budget-guard wrapper, or a reachable break condition. A loop whose only exit condition is "the model decides it's done" doesn't count. That's flagged CRITICAL, before the code ever ships, in every thuban scan.

TCG-101 · Unbounded AI Loop

Flags any AI SDK call sitting inside a loop with no visible iteration bound, budget guard, or guarded break — the exact shape of this incident.

TCG-201 · Missing Output Token Limit

Escalates to HIGH automatically when that same unbounded call also has no max_tokens set — removing the last remaining cost ceiling.

Runs automatically

No flag, no config. Cost Guard is part of every thuban scan and thuban protect pass, the same as Thuban's other 69+ detection rules.

100% local

Static AST analysis on your own machine. Your code, and your agent's logic, never leave your filesystem.

Positioning

An agent that decides for itself when to stop calling the API is an agent with no stop button. That's not a model-safety problem — it's a code review that never happened.

Sources

Would this pattern still be sitting in your codebase right now?

Free, no signup, runs entirely locally — your code never leaves your machine.

npx thuban protect

Cost Guard runs automatically in every scan — no extra flag needed.

Thuban Help
Ask a question or pick a topic below.