Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path
Agent Loop Cost Estimator
An AI agent cost estimator. Enter the steps per task, the tokens added each step, and your model’s input/output prices to see the cost per task, per day and per month — and why re-sending a growing context each step makes cost scale with the square of the steps, not linearly. Shows where prompt caching helps.
Input tokens grow every step, so cost scales roughly with the square of the step count. The premium above is how much the growing context costs versus a flat context — prompt caching and step caps target exactly this.
An agent's token bill grows with the square of its step count, because every step re-sends everything that came before it. An agent loops through plan, act and observe, and each turn is a fresh model call carrying the system prompt, the tool definitions and the whole history so far. This estimator turns that loop into a cost per task, per day and per month, and shows how much of the bill the growing context is responsible for.
What it computes
Each step is one model call. Step i sends the base context plus everything the earlier steps appended, so the input grows by a fixed amount per step:
input on step i = B + (i − 1) × g
input per task = N × B + g × N × (N − 1) / 2
output per task = N × o
cost per task = input / 1,000,000 × P_in + output / 1,000,000 × P_out
cost per day = cost per task × tasks per day
cost per month = cost per day × 30
growth premium = cost per task ÷ cost if every step sent only B
- N: steps, meaning model calls, per task.
- B: base context sent on every step (system prompt, tool definitions, the request).
- g: tokens each step leaves in the history, usually a tool call plus its result.
- o: output tokens generated per step.
- P_in, P_out: dollars per million input and output tokens.
The term g × N × (N − 1) / 2 is the triangle sum 0 + 1 + … + (N − 1) scaled by g, and it is why cost is quadratic in steps. The premium counts output cost on both sides, so it isolates the growth term.
A worked example
The defaults describe a mid-sized task: 12 steps, 3,000 base tokens, 800 tokens added per step, 300 output tokens per step, $3 per million input tokens, $15 per million output tokens and 500 tasks a day.
- Flat input:
12 × 3,000 = 36,000tokens. - Growth input:
800 × 12 × 11 / 2 = 52,800tokens. The last step alone sends3,000 + 11 × 800 = 11,800tokens. - Input per task is 88,800 tokens, and output is
12 × 300= 3,600 tokens. - Cost per task:
88,800 / 1M × $3 = $0.2664plus3,600 / 1M × $15 = $0.0540, so $0.3204. - Per day:
$0.3204 × 500 = $160.20, shown as $160. Per month:× 30= $4,806. - Premium: with a flat context the task would cost
$0.1080 + $0.0540 = $0.1620, and0.3204 / 0.1620 = 1.98, shown as 2.0×.
Changing only the step count shows the curve:
| Steps | Input tokens | Cost per task | Per month | Premium |
|---|---|---|---|---|
| 6 | 30,000 | $0.1170 | $1,755 | 1.4× |
| 12 | 88,800 | $0.3204 | $4,806 | 2.0× |
| 24 | 292,800 | $0.9864 | $14,796 | 3.0× |
| 48 | 1,046,400 | $3.3552 | $50,328 | 5.2× |
Doubling from 12 to 24 steps multiplies the cost per task by 3.1, and from 24 to 48 by 3.4. As N grows the growth term dominates, and each doubling approaches a fourfold increase.
Assumptions and limits
- Constant growth. Every step adds the same
gtokens. Real tool results vary, and one large file read can outweigh the rest of the run, so rerun with the worst case. - Output is not appended for you. Real agents keep each reply in the history, but the model only counts what you put in
g, so include the output that stays in context. - No caching or trimming. Every input token is billed at full price, history is never summarized, and the context window is not checked.
- A fixed step count. Because cost rises faster than N, runs of mixed length cost more on average than one run of average length, so pricing the mean step count understates the bill.
- Other charges. Reasoning tokens billed as output, retries and tool fees are not modeled; fold them into
o,NorB. A month is 30 days.
How to use the result
- Read the premium before the price. Near 1× the base context dominates, so shorten or cache the prompt and tool definitions. At 2× and above, summarizing, trimming or caching the history pays most.
- Cap steps where the curve bends. In the default, an 8-step cap drops cost per task from $0.3204 to $0.1752, 45% less for a third fewer steps.
- Shrink what each step leaves behind. Truncating tool output so each step adds 400 tokens instead of 800 brings the task to $0.2412, 25% less at the same step count.
- Budget for the long tail. Estimate with a p90 step count, not the median, and enforce a hard step limit.
- Weigh it against a success. Cost per task divided by the success rate is the cost per successful task, the figure to compare with the human time saved.
How it works
- Cost per task, day and month from steps, tokens per step and input/output prices.
- Input grows by a fixed amount each step, so cost scales with roughly N² steps, not N.
- Prices input and output tokens separately; a month is 30 days.
- The growth premium compares the bill with a context that never grows.
Frequently asked questions
Why does an AI agent cost more than a single LLM call?
An agent runs a loop of plan, act and observe, and every iteration is a separate model call that re-sends the accumulated context: the system prompt, the tool definitions and the full history of earlier steps and their results. A 12-step task is not 12 independent calls of the same size. Each call carries everything before it, so input tokens rise step after step. In the default example the first step sends 3,000 input tokens and the last sends 11,800.
Why does agent cost scale quadratically with the number of steps?
If each step appends about the same g tokens, step i sends the base context plus (i minus 1) times g. Summing over N steps gives N times the base plus g times N(N minus 1)/2, and that second term grows with the square of N. Doubling the steps therefore more than doubles the input cost, and once the growth term dominates each doubling approaches four times the cost. With the defaults, going from 12 to 24 steps raises cost per task from $0.3204 to $0.9864.
What is the context-growth premium?
It is the cost per task divided by what the same task would cost if every step sent only the base context, with output cost counted on both sides. A premium of 1.0 means history growth adds nothing, and the default of 2.0 means the growing context doubles the bill. The figure tells you which fix to reach for: near 1 the base prompt and tool definitions dominate, while a high premium means trimming, summarizing or caching the history pays most.
How does prompt caching change agent loop cost?
Prompt caching lets a provider re-read a prefix it has already processed at a discounted input rate instead of the full price. In an agent loop that prefix is the system prompt, the tool definitions and every earlier turn, which is exactly the part that repeats on each step, so caching attacks both the base and the quadratic growth term. This estimator bills every input token at the full rate, so treat its figure as the uncached ceiling and check your provider's cache read and write prices for the saving.
How do I estimate the tokens added per step?
Log a few real runs and measure how much the input grows from one call to the next; the average difference is your growth figure. It is usually the model's previous reply, including its tool call, plus the tool result that came back. Search results, file reads and fetched web pages tend to be the largest, so one verbose tool can dominate the growth. If the step sizes vary a lot, run the estimator twice, once with the average and once with a high percentile.
What are the biggest levers to reduce agent cost?
Cap the number of steps, because a runaway loop is the worst case and cost rises faster than the step count. Trim or summarize history so context stops growing, and cache the stable prefix. Truncate verbose tool results before they enter the history. Route simple steps to a cheaper model and keep the expensive one for hard reasoning. Output tokens are usually priced higher than input tokens, so shorter replies help as well. The premium shows whether growth or the base context is the bigger target.