AI token cost: calculate the real cost of an agent run
AI token cost per request is the easy part. An agent run resends its prompt on every step, writes and reads a cache, retries failed calls and sometimes reruns from scratch. A worked cost model with published Claude prices shows how far a run drifts from the per-token price.
The AI token cost of an agent run is the sum of every model call the run makes, and each call is billed on input tokens, output tokens and, where caching is on, cache writes and reads at their own rates. Then come the retries and the reruns. A per-token price covers only the first two lines. In the worked example below, one accepted result from a ten-step coding agent bills more than ten calls’ worth of tokens before retries, and every rerun multiplies the whole run; caching the stable prefix is the line that shrinks most.
What does an AI API bill you for, per call?
Anthropic’s pricing page lists five rates for each model. For Claude Sonnet 5.5 they are $2 per million base input tokens, $2.50 per million for 5-minute cache writes, $4 per million for 1-hour cache writes, $0.10 per million for cache hits and $10 per million output tokens. Output is five times the input rate, so a step that writes long code or long reasoning costs more than its prompt size suggests.
Agents add input you never typed. When a request carries tools, the API inserts a tool-use system prompt: 286 tokens on Sonnet 5.5, before your own tool names, descriptions and schemas, which are billed as input too. Every tool_result you send back is input on the next call. Server-side tools charge on top of tokens: web search costs $10 per 1,000 searches, and its results count as input tokens in the turns that follow.
Token counts also differ between models for the same text. Anthropic states that the tokenizer of Claude 4.7 and later models “produces approximately 30% more tokens for the same text” than earlier ones. Two models with the same per-token price can still bill different amounts for the same prompt.
Why does a multi-step agent cost more than its prompt?
Because the API is stateless. Each step resends the system prompt, the tool definitions and the conversation so far, so the same tokens are billed again on every call. A ten-step run pays for its stable prefix ten times, where a single call pays for it once.
Three more lines grow the bill:
- Retries. A timeout or a server error sends the same call again. Whether the failed attempt was billed depends on the provider and on how far it got; Anthropic, for instance, states that a web search that fails with an error is not billed. Assume the worst until your provider’s terms or invoices say otherwise.
- Quality reruns. The run finishes, the tests fail or the reviewer rejects the result, and the whole run starts again. Every step that had succeeded is billed a second time.
- Fallback. A rate-limited call that a client silently sends to a more expensive model changes the price of that step without changing the token count.
A worked cost model for one agent run
Take a coding agent on Claude Sonnet 5.5, and write its token counts as letters so you can put in your own: P is the stable prefix of each call (system prompt, tool definitions, repository notes), C the changing input of each call on average (history, tool results), and O the output of each call. The shape of the run is hypothetical, chosen to show how the formula moves:
- ten model calls per run;
- two calls per run fail and are retried, and the failed attempt is billed as a full call;
- two runs out of three pass the tests, so every two accepted results take three runs.
A per-token calculator prices one call: P + C input tokens at $2 per million plus O output tokens at $10 per million. The run bills these lines instead:
| Line, per run | Without caching | With 5-minute caching |
|---|---|---|
| Stable prefix (ten calls) | 10 × P at $2 per million | P written once at $2.50 per million, then 9 × P read at $0.10 per million |
| Changing input | 10 × C at $2 per million | same |
| Output | 10 × O at $10 per million | same |
| Retried calls | two more full calls | two more calls, prefix read from cache |
| Reruns | three runs per two accepted results | three runs per two accepted results |
Multiply each token count by its rate and divide by one million: that is the whole formula, and it is the one Kong’s cookbook describes when it “counts the cost of each request, from its token usage and the input_cost and output_cost of the target that served it”.
Three things stand out once you fill it in. Without caching, the stable prefix costs ten times what it costs in a single call, and when P is larger than C it is the largest input line. With caching, the nine reads together bill less than one uncached prefix, because cache hits on Sonnet 5.5 are priced at 0.05x the base input rate; the retries are assumed to land within the five minutes, so they hit the cache too. And the rerun factor multiplies every line above it, so a lower pass rate costs more than any price difference between two comparable models.
To fit the model to your own agent, log four numbers per run: input tokens split into uncached, cache write and cache read; output tokens; billed attempts per call; and whether the result was accepted. The cheapest LLM API comparison applies the same attempts-and-keep-rate logic to single requests.
How do you reduce token costs in an agent run?
Work from the largest multiplier down.
Raise the pass rate first. When three runs produce two accepted results, a third of the spend goes to runs that are thrown away. Better instructions, smaller steps and a check after each step that catches errors early cut reruns before any price changes.
Cache the stable prefix. Put the system prompt and tool definitions first and keep them byte-identical across calls. Anthropic’s page notes that a 5-minute write (1.25x) pays for itself after one cache read and a 1-hour write (2x) after two. A cache written and never read costs more than no cache.
Cap retries and read your fallback chain. Back off on rate limits instead of hammering the endpoint, and check what the last model in the chain costs. The LLM fallback guide separates billing errors from throttling.
Route each step to the cheapest model that holds quality. Steps that classify, extract fields or pick a tool rarely need the model that writes the code. Ficelle’s free vs paid LLM benchmark found free routing quality-safe against gpt-5 on 100% of classification tasks, 95% of JSON extraction tasks and 94% of reasoning and code tasks. Paid models still won polished long-form writing, where free routing was quality-safe on only 50% of tasks, so that work belongs on a paid model or with a human editor. The results were judged by an LLM against rubrics: treat them as directional and rerun them on your own steps.
Compress what you still send. Kong’s LLM cost optimization cookbook puts an LLMLingua compressor in front of the model that “keeps 60%” of prompts of 100 tokens or more; its demo shows a prompt going from about 138 tokens to 87 billed, a 37% reduction. Kong also makes the point that budgets should be set in money: “Token limits don’t map to money, because a token costs a different amount on each model.” Its example gives standard users a $1/hour shared budget and premium users $5/hour.
Batch what is not interactive. Anthropic’s Batch API carries a 50 percent discount on input and output tokens. It suits offline evaluation and bulk extraction. An agent loop that waits on each answer cannot use it.
The LLM cost optimization guide covers these levers in more depth.
Where free model routing fits, and where it does not
Moving a step to a free model sets its token line to zero, with limits. Free access comes as zero-price models and renewable quotas that run out, and a client that quietly falls back to a paid model when they do brings the cost back without telling you. Strict-zero routing closes that gap in Ficelle: paid fallback is hardcoded off, the router refuses to start if it reads another value, and any model that reports billing is quarantined. When a free model rate-limits, Ficelle marks it as cooling down and answers from the best working free alternative when another eligible model is available; when none is, the request fails instead of billing you.
Ficelle never routes to paid models, so the expensive steps of your agent, polished writing first, stay on the paid provider you call directly. Ficelle runs on your machine at http://127.0.0.1:8646/v1, and its open core is free under BSL 1.1 with the OpenRouter and Nous reference providers. Pro adds the maintained, curated provider pool with fixes when a provider breaks, native prompt compression and custom virtual models. If the cheap steps of your agent runs are where the token bill grows, see Ficelle Pro pricing.