Quadratic Cost Explosion: The O(N²) Loop Trap

Naive AI agent architectures incur quadratic (O(N²)) token costs because the entire accumulated conversation history and previous tool outputs are re-sent and re-billed with each new LLM call in a loop. A 20-step agent run can consume over 10 times the tokens a simple per-step estimate would suggest.

The top 3

  1. Top Reason for Quadratic AI Agent Costs: Context Accumulation: The primary driver of O(N²) token cost explosion in AI agents is the repeated re-sending of the entire conversation history, or context, with each new turn in a multi-step loop. For example, a 20-step workflow, if each step adds 1,000 new tokens, can lead to a cumulative input of 210,000 tokens, significantly more than a linear 20,000.
  2. Worst AI Agent Loops Driving Quadratic Costs: Specific agent behaviors are notorious for triggering O(N²) token cost explosions. These include 'Tool-Call Retry Storms' (repeated failed actions), 'State Stagnation' (agent stuck without real progress), 'Self-Correction Hallucinations' (endless self-critique of faulty outputs), and 'Multi-Agent Oscillation' (agents ping-ponging tasks between each other). Agentic coding tasks are especially susceptible, consuming 1,000 times more tokens than simpler code reasoning or chat tasks.
  3. Biggest Real-World AI Agent Cost Overruns from O(N²) Loops: The quadratic nature of token consumption in agent loops has led to staggering financial losses. One rogue agent instance was documented consuming 1.67 billion tokens in just 5 hours, resulting in an estimated bill of €16,000 to €50,000. Another incident involved a four-agent system stuck in an 11-day infinite conversation loop, accumulating a $47,000 cost before detection.

Sources

Open the full topic