⚡ Agent Context Cost

An agent that takes twelve turns is not twelve calls. Every turn resends the whole conversation — the system prompt, every tool result, every previous answer — so the input side of the bill grows with the square of the turn count while the estimate you wrote on the whiteboard grows in a straight line. At twelve turns the real bill is already several times the estimate, and the last turn alone can cost nine times the first. This calculator turns your loop shape into the number that will actually appear on the invoice.

📐 Loop Shape

Enter what one turn adds to the conversation. The model assumes the whole history is resent each turn, which is how almost every agent framework works by default. If your framework truncates or summarises, set the cap below.

What every turn carries
Rates — from your contract

A tool result counts once as new content and then again on every following turn. That is why tool output dominates agent bills even when the individual results look small: a 500-token JSON response fetched on turn three is billed on turns four through twelve as well.

The Per-Call Estimate Is the Wrong Unit

Almost every agent budget starts the same way: take the average tokens in one request, multiply by the number of requests, multiply by the price. That arithmetic is correct for a stateless service and wrong for an agent, and the error is not a rounding difference. It is structural.

A stateless endpoint sends the same short prompt every time. An agent cannot, because the model has no memory between calls. Everything it needs to know about what happened in turn three has to be re-sent in turn four: the user's instruction, its own previous reasoning, and every tool result it has collected. Context is not a cache the provider keeps for you — it is a payload you re-upload on every call, and you are billed for the upload.

The consequence is that the per-call unit understates the bill by a factor that grows with the loop length. Short loops hide it. A three-turn chain is close to the estimate and looks fine in testing. A twenty-turn research agent is off by an order of magnitude, and nobody notices until the invoice arrives, because the individual calls each looked cheap.

Why the Growth Is Quadratic

Let k be everything one turn adds to the conversation — user message, assistant output, and tool result. The input at turn t is the base plus k for each completed turn before it:

k = user + output + tool tokens added per turn ctx(t) = base + (t - 1) × k input tokens at turn t total_in = N × base + k × N × (N - 1) / 2 sum over all turns est = N × base flat-call estimate overhead = 1 + k × (N - 1) / (2 × base) total_in / est N_2x = 1 + 2 × base / k first turn at 2x overhead

The estimate term is deliberately the flat one: N calls of the same size as the first. That is what a budget written before the loop was instrumented actually contains, and it makes the comparison a like-for-like one rather than a comparison against a per-turn average that already knows the answer. The second term is the whole story. It has an N squared in it, divided by two, and it is multiplied by k — so the marginal cost of adding one more turn is not constant, it is the current context length. Turn twelve is expensive because turns one through eleven are still in the payload.

The N_2x line is the one worth memorising. It says where your estimate stops being usable, and it depends only on the ratio of base to growth. A large system prompt buys you more turns before the error becomes visible, which is a genuinely underrated argument for thorough tool definitions. A small prompt with chatty tool results crosses the threshold almost immediately.

The Last Turn Is the Expensive One

Because context length is linear in the turn index, the cost of a turn is linear in the turn index too. The ratio between the final turn and the first is simply the final context divided by the base. In a loop with a 2,000 token base and 1,500 tokens of growth, the twelfth turn sends 18,500 tokens against the first turn's 2,000 — a factor of 9.25.

This changes what optimisation should target. Averaged over the loop, every turn looks roughly similar and no single turn looks alarming, which is why per-turn dashboards rarely surface the problem. Sorted by cost, the last few turns carry most of the bill. If the loop can terminate earlier — a better tool, a tighter success condition, a cheaper verification step — the saving is concentrated exactly where the money is.

TurnContext sentCost of this turnCumulative
12,000$0.0072$0.0072
46,500$0.0162$0.0468
812,500$0.0282$0.1416
1218,500$0.0402$0.2844

The last row is the whole task, and the numbers are per-turn cost at $2 per million input plus $8 per million output for 400 output tokens. Turn twelve alone is 5.6 times turn one. The cumulative column is what a quarterly review actually sees, and it is 5.1 times what a per-call estimate would have predicted for the same twelve turns.

Tool Output Is the Usual Villain

Of the three things a turn adds, tool results are typically the largest and the least examined. A user message is short by design. An assistant answer is bounded by the model's verbosity. A tool result is bounded by whatever the tool returns, which is often raw: a full JSON document, a web page's text, an unfiltered SQL result, a directory listing.

The cost of a tool result is not its length, it is its length multiplied by the number of turns that follow. A 4,000-token result fetched on turn two of a twelve-turn loop is billed eleven times — 44,000 input tokens for one lookup. Trimming that result to 800 tokens before it enters the conversation removes 35,200 tokens from the task, which at $2 per million is seven cents per task and $420 a month at 200 tasks a day.

This is why "return a summary, not a dump" is the highest-leverage change in most agent codebases, and why it is invisible in any per-call analysis. The trimming happens inside the tool, so the model never sees the difference, the quality does not change, and the bill moves by a third.

Compaction Is a Threshold, Not a Strategy

Capping the context at a fixed length converts the quadratic term into a linear one: once the conversation hits the cap, each turn sends the cap instead of the ever-growing history. The saving is exactly the growth that would have happened above the threshold, which is why it rises with turn count and does nothing for short loops.

The threshold itself is the interesting decision, and it has two costs rather than one. Below the cap, the model sees everything and quality is unaffected. Above it, something has to be dropped or summarised, and the summarisation call is itself a completion you pay for. A cap set aggressively saves input tokens and spends them back on summarising, plus whatever quality loss comes from the model forgetting the middle of the task.

Set the cap as input to see the trade in tokens. The default of zero means no compaction, which is what most frameworks do unless you configure otherwise — so if the cap saving field reads as a dash, you are paying the full quadratic price and probably did not choose to.

Two Levers This Page Does Not Price

The first is caching, and it interacts with everything above. The stable part of an agent's context — system prompt, tool definitions, standing instructions — is the same on every turn, which makes it the ideal cache prefix. It is also usually the part that does not grow, so caching it caps the floor of the bill without touching the growth. Whether that trade pays depends on request spacing, which is a separate calculation on the prompt cache break-even page.

The second is model routing. Running early turns on a cheap model and escalating only when the task proves hard is attractive, but it does not change the shape of the curve — it changes the price of the vertical axis. A quadratic bill discounted by 70% is still quadratic, and it still overtakes the linear estimate at the same turn count. Routing buys headroom; it does not buy structure.

What actually buys structure is fewer turns and smaller tool results, in that order. Both are engineering decisions, both are measurable per task, and neither requires a vendor change.

Pricing Basis, Sources & Assumptions

This page does not assert vendor prices. Both rates are inputs you control, pre-filled as a starting point because the arithmetic is easier to read with real-looking numbers. Enter the rates on your invoice, including any negotiated or committed-use discount.

Pricing inputBasis used on this page
SourceAll per-token rates are user-supplied and every result depends on what you enter. The structural fact the model rests on — that a chat completion is stateless and the caller resends the full history each turn, with every token billed as input — follows the OpenAI API pricing documentation and the Anthropic pricing documentation, both re-read on . Default rates are illustrative placeholders, not a price claim.
RegionNot applicable — first-party API pricing is global per provider. Regional and multi-region marketplace endpoints can carry a premium; use the rate on your invoice if you buy through one.
CurrencyUSD, as entered by the user
Last checked — the stateless request model and the input/output price split were re-confirmed against provider documentation on this date. No rate is asserted.

Modelling assumptions