Your AI agent's token bill grows quadratically. Here's the math (and a free calculator)
Most token calculators multiply tokens by price. That works for a single API call. It is wrong for an agent. Why agent loops get expensive Each step of an agent loop sends the whole conversation so far: the s
Most token calculators multiply tokens by price. That works for a single API call. It is wrong for an agent.
Why agent loops get expensive
Each step of an agent loop sends the whole conversation so far: the system prompt, every earlier turn, and every tool result.
Say your system prompt is 2,000 tokens and each step adds 800 tokens. Then:
| Step | Input tokens sent |
|---|---|
| 1 | 2,000 |
| 3 | 3,600 |
| 6 | 6,000 |
| 12 | 10,800 |
A 12-step run sends 76,800 input tokens in total. The naive estimate of 12 ร 2,800 gives 33,600, so the real number is 2.3ร higher. The total grows with the square of the step count:
total_input = n * system + adds * n * (n - 1) / 2
= 12 * 2000 + 800 * 12 * 11 / 2 = 76,800
Three levers that actually move the bill
- Prompt caching. The system prompt and the early turns repeat on every step, so cached reads cut most of that cost.
- Summarize after N steps. Swap old turns for a short summary, which caps context growth.
- Route by step. Use a small model for tool-call formatting and a big one only for planning.
Get your own number in 10 minutes
I built a free spreadsheet for this: cost per query, cost per user and gross margin, with dated, cited list prices for six current models. It works in Excel and Google Sheets.
๐ AI API Cost Calculator Lite (free)
If you need agent loops, caching, sub-agent fan-out and break-even pricing per plan, the full AI Cost & Margin Model covers it. Code LAUNCH20 takes 20% off until 18 Nov.
How do you budget for agent runs? Tell me in the comments.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes โ full credit and traffic to the original publisher.