Founders

How to Estimate a Runaway Agent's Cost Before You Set a Cap

Antonio Evans9 min read

A simple diagram showing an agent loop: a box labeled 'Agent' with arrows looping back to itself, annotated with 'iterations = N', 'tokens per call = T', and 'retry rate = R'.
Key points

Key points

  1. 1A runaway agent's worst-case cost is bounded by loop count times token size per iteration, adjusted for the retry rate, then multiplied by the price per token.
  2. 2Linear estimates undercount badly because each step re-bills prior context, so input tokens grow with the triangular number of steps, not the step count.
  3. 3The cap must be an inline, pre-execution check, because dashboards, alerts and account-level provider caps only record spend after the fact.
  4. 4Set the cap just above the computed worst case plus a small tail buffer, and below the point where one customer's spend destroys that account's margin.

Count the loop first

Most cost estimates start with the price per token and work upward. That is the wrong end. The number that decides whether a cap is safe is how many model calls a pathological run can make, because that count multiplies everything else.

An agent is not a single request with a predictable cost. It is a loop, and each step can trigger tool calls, sub-agent spawns, retries and longer reasoning traces. Cycles, a budget enforcement vendor, describes an agent as a loop where each step can trigger more steps, so exposure compounds with every step (Cycles on agent budget control). That piece walks through a "summarize this repository" task that looks like 23 calls and $2.63, then shows how a rate limit plus retries plus sub-agents turns it into a 200-call task at roughly ten times the cost.

So the first input is a count: the largest number of billable model calls one run can issue before something structural stops it. A step counter, a recursion limit or a circuit breaker gives you that number. Everything below plugs into it.

Start with the linear bound, knowing it is too low

The fast estimate multiplies iterations by an assumed per-iteration token size:

naive_tokens = max_iterations * tokens_per_iteration

Take an agent that loops on a failed API call, capped at 100 iterations by a step counter. Each iteration sends a system prompt, the tool schema, the last error and the model's previous reply, which measures 2,000 tokens in and 500 out.

naive_total_tokens = 100 * (2,000 + 500) = 250,000 tokens

That number is a useful floor and it is what most teams implicitly assume when they set a cap. It is also wrong in one direction: too low. Many chat and completion APIs are stateless, so they receive and bill the entire conversation history on every call (Augment Code on agent loop token costs). An agent loop appends tool output, observations and reasoning to the history after each iteration, so the prompt grows and earlier steps are paid for again and again.

The formal version of that growth, from the same analysis:

Total_input_tokens = N*S + u*N*(N+1)/2 + r*N*(N-1)/2

Where:
  N = number of iterations
  S = static prefix tokens (system prompt + tool definitions)
  u = new input tokens per iteration (user message + tool result)
  r = output tokens per iteration

A writeup on multi-turn tool use puts the undercount bluntly: estimates that multiply expected tool calls by per-call cost are wrong by 5 to 30 times depending on agent complexity, and it reports 40 percent of agentic AI pilots cancelled before production with runaway inference costs the most common reason (tianpan.co on the token economy of multi-turn tool use). The same source documents a five-step run where per-call token consumption grew 888, 3,400, 8,900, 14,200, 18,900. That is the quadratic curve in the wild.

Use both numbers. The linear bound is the floor. The quadratic bound is what you budget against, and your cap must clear it.

Measure token size per step instead of guessing

The quadratic formula needs three measured quantities: static prefix S, new input per iteration u, and output per iteration r. None should be guessed, because tool definitions are larger than people assume.

A detailed production system prompt runs 2,000 to 5,000 tokens, and every available tool gets serialized into context on every call whether or not the model uses it (tianpan.co). The same source reports measured production setups with 55,000 to 134,000 tokens of tool-definition overhead in a single call, and one team cut that from 134,000 to 8,700 tokens by loading tools dynamically. Guess 2,000 when the real prefix is 30,000 and your estimate is off by an order of magnitude before a single retry.

To get the three numbers:

  1. Log prompt tokens, completion tokens, total tokens, model, route, tenant, request status, retry count and estimated cost on every call. Yotta Labs' budgeting guide lists this field set and argues for measurement before enforcement, because hard limits set early block valuable workflows (Yotta Labs on token budgets).
  2. Pull a real production run, not a demo. Read S off the first call, u off the input delta between consecutive iterations, and r off the completion delta.
  3. Where the provider returns a usage object (usage.prompt_tokens, usage.completion_tokens, usage.total_tokens), read from that rather than converting character counts, since tokenizers differ by provider (Solvimon on token pricing).
  4. Use your worst observed iteration, not the median. An error payload with a large stack trace can be several times a normal tool result.

Databricks frames the same problem as a cost ledger: cost per output token, cost per inference, cost per active user, then revenue per user against it to find margin (Databricks on modeling unit economics). Your worst-case number belongs in that ledger, so the cap and the margin model share inputs.

Apply the retry rate, and know which retry you are pricing

The retry rate is the least intuitive multiplier and the one most likely to be wrong.

Retries cost twice. A failed call still bills its input, and the error response then joins the context, so every later call in the run pays for it again. tianpan.co notes that a 10 percent failure rate per step compounded across 10 steps without a circuit breaker can multiply costs several times over, and that one team cut per-task tool calls from 14 to 2 by adding explicit SUCCESS and FAILED terminal states so the model stopped retrying ambiguous outcomes (tianpan.co).

Contaminated context makes retries more likely. A simulation study of retry costs found a 5x retry multiplier pushed cost per successful task from $5.73 to $28.65, and that contaminated context raised the per-step error rate by 7.1x over baseline (AI Security Guard retry cost analysis). That last figure matters: the retry rate you measure on a clean run is not the rate inside a runaway, because the runaway is already polluting its own context.

If you want one number rather than a simulation, inflate the iteration count:

effective_iterations = max_iterations * (1 + retry_rate)

With 100 iterations and a 20 percent retry rate that is 120. Use 120 as N in the quadratic formula. It is conservative and defensible, and it is the adjustment behind the common shorthand of 100 * 2,000 * 1.2 = 240,000 tokens. Note what that shorthand does not do: it applies the retry rate to a flat per-iteration size, so it ignores context accumulation. Keep it as the floor.

A worked example you can rerun with your own numbers

Here is the full calculation for the agent looping on a failed API call, with every input written down so it can be replaced.

InputSymbolValueWhere it came from
Iteration ceilingN100Step counter in code
Static prefixS6,000 tokensSystem prompt plus tool schemas, first call
New input per iterationu1,200 tokensError text plus tool result delta
Output per iterationr500 tokensCompletion delta between iterations
Retry rate-20 percentObserved retries divided by total calls
Effective iterationsN_eff120100 * 1.2
Input price-$1.00 per millionProvider rate card, read when you set the cap
Output price-$5.00 per millionSame rate card

Step one, apply the retry rate: 100 * 1.2 = 120 effective iterations.

Step two, input tokens with the triangular term:

input_tokens = 120*6,000 + 1,200*120*121/2
             = 720,000 + 8,712,000 = 9,432,000

Step three, output tokens:

output_tokens = 500*120*119/2 = 3,570,000

Step four, price it:

cost = 9.432 * $1.00 + 3.570 * $5.00
     = $9.43 + $17.85 = $27.28

The linear estimate on the same inputs is 120 * 7,700 = 924,000 tokens, which lands near a dollar or two. The quadratic bound is more than ten times higher. That gap is the whole reason to do this before setting a cap rather than after the invoice.

Two things stand out. Output is the bigger line item even though input volume is nearly three times larger, because output tokens typically cost three to five times the input price (Solvimon). And the triangular term dominates: 8.7 million of 9.4 million input tokens are re-billed history, not new work.

Add what sits outside the token bill

A token-only estimate is a floor for a second reason: the token line is the visible part of AI spend, not all of it. Finout's breakdown of AI unit economics lists orchestration (agent frameworks, routing logic, workflow coordination), retrieval (vector queries and embeddings), evaluations, governance and labor as costs outside the API bill (Finout on AI unit economics). A runaway that fans out across sub-agents pays all of those per step.

Fan-out is the multiplier that breaks the cleanest estimate. tianpan.co reports roughly 7x cost per additional agent in a pipeline, so a four-agent research pipeline running 20 steps costs closer to 28x a single-agent system, before retries (tianpan.co). Unblocked, citing an arXiv study it flags as directional rather than a production benchmark, reports input tokens averaging 53.9 percent of agentic spend and the review-and-refinement stage eating 59.4 percent of all tokens (Unblocked on why agents burn tokens). Read those as reasons to instrument your own runs, not as your estimate.

If your agent can spawn sub-agents, apply the fan-out factor to the iteration count before the quadratic term, not after.

Turn the bound into a cap with a decision rule

You have a worst-case number per run. A cap is a policy on that number. Three defensible rules:

SituationCap ruleReasoning
Run cost well below the margin on the accountCap at 1.5x to 2x the worst caseHeadroom, so no normal customer ever hits it
Run cost a meaningful share of revenue per customerCap at 1.2x the worst case, hardProtects margin; a hit signals a problem above the model layer
Task genuinely open-ended (research, whole-repo work)Cap per session, re-authorizeNo defined end, so per-task caps either fire early or sit too high

The direction of the comparison matters most. A cap should cover the worst case but not much more, which means it is set from the worst case you computed, not the average run. Set it from the median and every third customer trips it. Set it at ten times the worst case and it is decoration.

The enforcement mechanism is a separate constraint. Most teams have a dashboard, Slack alerts at several thresholds and an account-level provider cap. Cycles calls that stack visibility rather than enforcement: dashboards are retrospective, an alert at $100 does not stop the $101 call, provider caps are scoped to an account rather than a run, and rate limits control velocity rather than total spend (Cycles). Ravoid makes the same point harder, describing enforcement as the layer that refuses to forward the next request once a rule is broken, and reconstructs a representative incident where four agents ran for eleven days and produced a $47,000 bill with all three layers in place (Ravoid on AI agent budget enforcement).

The requirement is a check before execution, atomic with the deduction, cumulative across retries and concurrent workers, which Cycles describes as reserve, execute, commit (Cycles). Budget dimensions belong per request, per user, per tenant, per feature and per environment, so a single monthly cap does not fail to stop a loop inside one agent or a dev script using a production key (Yotta Labs).

Then decide how the cap shows up in pricing. Charge per token and the customer absorbs the runaway, so the cap is a safety rail. Charge per seat or a flat fee and you absorb it, so the cap protects margin, which is why selling unlimited AI usage on a fixed seat price puts the supplier on the wrong side of heavy users (Willingness to Pay on AI pricing). The cap number is the same either way. Who pays is a pricing decision, not an engineering one.

If you would rather price the outcome than the token, the same worst-case figure is the ceiling on what one delivered result can cost you. That is the argument for tying spend to defined outcomes once a use case is locked, while keeping token-level controls underneath (Willingness to Pay on AI pricing).

Put the estimate into practice

Do the calculation once per agent, not once per company, because the inputs differ wildly between a support triage agent and a whole-repo coding agent. Write N, S, u, r and the retry rate into a comment next to the cap constant. When someone changes the system prompt or adds a tool, the constant is stale, and that comment is what tells the next engineer why.

Then reconcile. Compare the computed worst case to the largest actual run cost over a few weeks. If real runs exceed the bound, an input is stale, usually S after a tool was added or the retry rate after a new failure mode appeared. If real runs never exceed half the bound, tighten the cap.

For teams running several model families behind one app, the price input changes per route, so keep a rate per route next to the route budget. Quanta Inference puts text, image, video, audio and embedding models behind one OpenAI-compatible API key with prepaid dollar pricing, which makes the rate side one table to maintain instead of several (Quanta Inference). The loop-count side deserves a second pair of eyes, and Quanta Consulting runs a free 30-minute discovery call to review your goals and give a tailored recommendation, a reasonable place to pressure-test whether your agent's step ceiling is realistic (Quanta Consulting).

FAQ

FAQ

Why is my linear token estimate so far off from the real bill?

Because it assumes each iteration sees the same context, and in an agent loop it does not. The API re-bills the whole conversation history on every call, so input tokens grow with the triangular number of steps rather than the step count, which is why estimates come in 5 to 30 times too low depending on complexity (tianpan.co). Add the triangular term for accumulated input and output, as in the worked example above.

How do I measure S, u and r if I have never logged tokens?

Log prompt tokens, completion tokens, total tokens, model, route, tenant, request status and retry count from now on; Yotta Labs lists this as the minimum field set for a token budget (Yotta Labs). Read S from the first call of a real run, u from the input delta between iterations, and r from the completion delta, and pick your worst observed iteration. If the provider returns a usage object, read from it instead of converting character counts, because tokenizers differ by provider (Solvimon).

Should the retry rate apply to the iteration count or to the tokens?

Apply it to the iteration count, then run the quadratic formula on the inflated count. A retry does not just add one call's tokens, it adds an error payload that stays in context and is re-billed on every later call, so pricing it as a flat token addition undercounts. The rate you measure on clean runs is also lower than the rate inside a runaway, because contaminated context raises the per-step error rate substantially (AI Security Guard).

What should I add on top of the token bound?

Orchestration, retrieval, embedding generation, evaluations and governance all sit outside the token bill and scale with step count (Finout). If the agent can spawn sub-agents, apply a fan-out factor to the iteration count first, since per-additional-agent overhead is substantial and a four-agent pipeline can cost far more than four times a single agent (tianpan.co).

Is an account-level provider spending cap enough to stop a runaway?

No, because provider caps are scoped to an account, workspace or time window rather than one run, so they usually fire long after the damage and take down unrelated workloads when they do (Cycles). You need a check before the model call that deducts atomically with the check and tracks cumulative spend across retries and concurrent workers (Cycles). Set the number from your computed worst case and put it in that inline layer, not in a dashboard.

Sources

Sources

  1. AI Agent Budget Control: Enforce Hard Spend Limits - Cycles
  2. AI Agent Loop Token Costs: How to Constrain Context
  3. The Token Economy of Multi-Turn Tool Use
  4. Set Token Budgets for an AI Product
  5. What is AI Token Pricing?
  6. How to model unit economics for an AI product (cost per ...
  7. How AI Agent Loops Multiply Costs, The Retry Rate Analysis
  8. AI Unit Economics: Allocating Token Spend to the Teams ...
  9. Why AI Agents Burn Tokens: 4 Waste Mechanisms | Unblocked
  10. AI Agent Budget Enforcement: Dashboards Are Not Control
  11. AI Pricing Models: How to Price AI Products
About the author

About the author

Antonio Evans
Antonio Evans

Antonio Evans is the founder of Quanta Intelligence and a fractional CTO who builds HIPAA-ready AI systems for health and life-science companies. Before that he was technical co-founder of FriendsEAT for twenty years, CEO of Socialdraft, and a data engineer at IBM. Antonio lives with his dog Santino in Lisbon, Portugal.

All articles