Skip to content

Automation & Cost

Also worth it

AI Agent Cost Calculator: Runaway Loops & Budget Burn

Sogoh prices a runaway agent loop across workers, then gives you the breaker limits that keep it inside budget.

Reviewed$0 · No login

TL;DR

Sogoh’s loop-cost model shows a runaway agent’s bill is not linear: with full-history accumulation, input tokens grow quadratically. At 2,000 input and 800 output tokens per step, 50 retries send about 3.53M input tokens — roughly 141× a 3-step run on Claude Sonnet 5.5. Cap iterations, cap tokens per run, and enforce a dollar hard stop at the API gateway.

Runaway loop · token growth · breaker settings

Scenario preset

--Prices read from the OpenRouter models API on OpenRouter ↗

2,000
800
50
5

Max runaway bill

--

--

Worst case if every worker runs to the iteration limit.

Normal run (3 steps)
--
Multiplier vs normal
--
Budget consumed
--

Budget burn time

--

--

24-hour unattended bill--

Upper bound: assumes the loop never stops and keeps re-sending the capped context. Real requests fail at the model context limit first.

Input tokens sent
--
Output tokens
--
Average per step
--
Cumulative bill by iteration--

Solid line: cumulative fleet bill. Dashed line: budget red line.

Circuit breaker settings

Copy these into the agent definition and the API gateway. A framework limit alone does not stop a workflow that restarts.

max_iterations
--
max_tokens_per_run
--
max_cost_per_run_usd
--
hard_stop_usd
--
max_wall_clock_seconds
--
max_concurrent_runs
--
--

Planning estimate, not an invoice. Everything runs in your browser and inputs never leave the page.

Calculate exact prompt tokens first, then re-run this bill

NotesResults come from public data and the formulas or rules shown on this page: calculators compare scale and direction, while parsers and path builders follow each tool’s documented syntax, which can change between versions. Neither is financial, tax, legal or medical advice; check the official source or a qualified professional before acting.

Method & sourcesFull disclaimer

Share

Email updates

Told when this page’s prices change

The formula stays the same. Prices do not. When a plan changes, one short email with the difference.

Submitting means you agree to receive occasional Sogoh updates and have read the privacy policy. Unsubscribe any time.

How the numbers are derived

  1. 01

    Measure one step, not the task

    Read input and output tokens from a single tool call in your gateway logs. 2,000 input and 800 output is a typical tool-using step; reasoning models often double the output side, and every extra tool result lands in the next input.

  2. 02

    Pick the accumulation mode honestly

    A static window means the framework truncates history. Full history means every retry re-sends earlier errors and outputs. Only the second produces quadratic growth — check whether your loop appends the previous failure to the next prompt.

  3. 03

    Read the ceiling, then the burn rate

    The headline number is the worst case across all workers: single-thread bill × concurrency. Compare it with a 3-step normal run, then check when the loop would cross your budget red line at 3.5 seconds per step.

  4. 04

    Enforce the breaker in two places

    Set max_iterations in the graph and a hard dollar/token stop in the API gateway. An outer workflow restart resets in-process counters, but it cannot reset a gateway cap; alert at 50% of budget so a silent loop is visible.

FAQ

Why does a looping agent’s bill grow quadratically?

If each retry appends the previous error and output, the input at step i is T + (i − 1) × (T + O). Total input over N steps is N × T + (T + O) × N(N − 1) / 2: the second term is quadratic. At 2,000 input and 800 output tokens, 50 steps send 3.53M input tokens instead of 100k. This is the doom loop pattern: a failed tool call, a retry with the error appended, and no external stop.

What max_iterations should I set?

Set it from your budget, not from a blog post. With a $50 cap, five workers and the defaults above, the calculator recommends about 40 iterations to stay under half the budget. Remember that a LangGraph super-step may contain several model calls, so count the calls that actually bill.

Does this include prompt caching or batch discounts?

No. It uses list prices without cache reads, batch rates or tool surcharges. Caching can cut the input price by roughly 75–90% on supported providers, but it slows the curve rather than removing the quadratic term. Enter your effective rate under Custom.

Why cap the context per request?

Every model has a context window, and most frameworks truncate or reject oversized prompts. Without a cap the math produces bills no real request could ever generate. The default 200k cap keeps the estimate actionable; lower it if your model or gateway limit is smaller.

How do I stop an infinite loop in n8n or LangGraph?

Use more than one layer: recursion_limit or max_iterations inside the graph, a per-run timeout and token cap, and a hard spend stop at the API gateway or provider. Then test the breaker by forcing a failing tool call; a limit you have never triggered is not a guarantee.

How current are the prices, and why do they differ from my invoice?

Model prices were read from the OpenRouter models API on 18 November 2026. Provider-direct billing, cache reads, batch discounts, long-context surcharges and negotiated rates can differ. Switch to Custom and enter the rate you actually pay.

← Back to calculators