arrow_back Back to Blog
Agentic AI

Why Your AI Agent Costs Grow Quadratically (and How to Stop It)

SimplifAI Team

You test your agent with a 5-step chain. Looks fine — costs a few cents per run. You ship to production. Two weeks later you open your LLM provider dashboard and the bill is 15x what you expected. What happened?

The answer is a property of how LLMs work that catches nearly every team off guard: every step in an agentic chain includes the entire conversation history as context. That means token usage — and cost — grows quadratically with the number of steps, not linearly.

The Quadratic Growth Problem

When your agent calls an LLM, the prompt includes everything: the original task, every tool call so far, every tool result, and every model response in the chain. The context window grows with each step.

If each step adds approximately T tokens to the conversation (input + output combined), then by step N, the input context for that step is roughly N × T tokens. Total input tokens across all N steps:

Total input tokens = T × (1 + 2 + 3 + ... + N) = T × N(N+1)/2

That’s O(N²). Double the chain length, quadruple the input token cost.

The Math With Real Numbers

Assume a moderately complex agent: each step adds ~2,000 tokens of context (tool results, model responses) and generates ~500 output tokens. Here’s what that looks like on Claude Sonnet 4 ($3.00/M input, $15.00/M output):

Chain Steps Total Input Tokens Total Output Tokens Total Cost vs. 5-Step
5 steps30,0002,500$0.131x (baseline)
10 steps110,0005,000$0.413.1x
15 steps240,0007,500$0.836.4x
20 steps420,00010,000$1.4110.8x
30 steps930,00015,000$3.0223.2x

Your 5-step test ran at $0.13/run. Your production agent — which needs 20 steps for complex tasks — costs $1.41/run. At 1,000 runs/day, that’s $1,410/day vs. $130/day. And it gets worse: production tasks are often harder than tests, so chains run longer.

The same pattern holds on GPT-4o ($2.50/M input, $10.00/M output): a 5-step run costs ~$0.10, a 20-step run costs ~$1.15, a 30-step run costs ~$2.48. The multipliers are nearly identical — this is a structural problem, not a pricing quirk.

Five Patterns That Make It Worse

  • Verbose tool outputs. If your search tool returns 5,000-token web pages instead of 500-token summaries, every subsequent step pays for those 5,000 tokens again in context. Truncate or summarize tool results before returning them to the model.
  • No conversation pruning. Most agent frameworks append every message forever. A 30-step agent might carry 20,000 tokens of intermediate reasoning from step 3 that’s irrelevant by step 25 — but still being sent as input every step.
  • Error retry loops. When a tool call fails and the agent retries, it appends the error message and tries again, adding more context. A 3-retry loop on a 15-step chain can add 30–50% more tokens.
  • Multi-agent pipelines passing full context. If agent A finishes and hands its full conversation history to agent B, you pay for A’s entire context again on every step of B’s chain. Pass summaries, not transcripts.
  • System prompts repeated each step. If your system prompt is 2,000 tokens, that’s an additional 60,000 input tokens for a 30-step chain — paid in full, every time.

Six Strategies to Control Agentic Costs

1. Implement a Sliding Context Window

Instead of passing the full conversation history, keep only the last K turns (e.g., K=10). The model sees recent context but not the full chain from step 1. This caps input tokens at a fixed ceiling regardless of chain length — converting quadratic growth to linear.

# Sliding window: keep only the last K messages
MAX_CONTEXT_TURNS = 10

def build_messages(history, system_prompt, new_message):
    recent = history[-MAX_CONTEXT_TURNS:]
    return [
        {"role": "system", "content": system_prompt},
        *recent,
        {"role": "user", "content": new_message}
    ]

2. Summarize at Context Milestones

Sliding windows lose information. A better approach: when the conversation exceeds a token threshold, ask the model to summarize the conversation so far into 500–800 tokens, then replace the full history with that summary. You preserve the key decisions and facts without paying for every intermediate step.

3. Truncate Tool Outputs Aggressively

Tool results are often the biggest single source of context bloat. Cap every tool response at 1,000–2,000 tokens. For web search results, extract only the relevant sentences. For code execution output, keep only the last N lines of stdout/stderr. The model rarely needs the full raw output — it needs the signal.

4. Route Early Steps to Cheaper Models

In a multi-step chain, early steps often involve simple planning, tool selection, or data extraction — tasks a cheaper model handles well. Reserve the expensive frontier model for the final synthesis step. A 20-step chain where steps 1–15 use GPT-4o mini ($0.15/M input) and steps 16–20 use Claude Sonnet 4 can cut costs by 60–70% with minimal quality loss.

5. Break Long Chains Into Checkpointed Sub-Tasks

Instead of one 30-step agent run, break the task into 3 sub-tasks of 10 steps each. Each sub-task starts with a clean context (just its specific goal and a brief summary of prior outputs). This converts one O(N²) chain into three O((N/3)²) chains — a 9x reduction in context tokens for the same amount of work.

6. Set Hard Step Limits With Fallbacks

Every production agent should have a max_steps guard. If the agent reaches the limit without completing, return a partial result or escalate to a human. Runaway agents — ones that get stuck in loops retrying failed tool calls — are the most expensive scenario and the easiest to prevent.

The Monitoring Blind Spot

Here’s what makes this particularly dangerous: you can’t see it happening in real time. Your OpenAI or Anthropic dashboard shows aggregate token usage and total cost. It does not show you that one specific agent run used 450,000 input tokens across 22 steps, or that your average chain length grew from 8 steps last week to 14 steps this week as task complexity increased.

By the time you see the bill, you’ve already paid for thousands of expensive runs. And the bill doesn’t tell you which agent, which task type, or which step in the chain is responsible for the spike.

This is the monitoring blind spot that LLMeter is built to close. LLMeter tracks token usage per API call with full metadata. Tag calls with a session ID to group all steps in an agent run, see the token growth curve across steps, and set alerts when a session exceeds a token budget. When costs spike, you see exactly which agent run caused it and where in the chain the growth happened — not just a monthly aggregate.

The Bottom Line

Agentic AI costs are not like chatbot costs. The quadratic growth dynamic means a task that costs $0.13 in a 5-step test will cost $1.41 in a 20-step production run — a 10x difference before accounting for error retries, verbose tool outputs, or multi-agent handoffs.

The mitigation strategies are well-understood: sliding context windows, tool output truncation, step limits, model tier routing. What’s missing for most teams is visibility into where the tokens are actually going. Set up per-call tracking before you scale, not after the bill arrives.

LLMeter is free for up to 10,000 calls/month. Two-minute setup, no proxy, no code changes to your LLM calls — just add a lightweight ingest call after each agent step. See the growth curve before it becomes a budget crisis.