Token economics & cost estimation
Cost = input tokens × input price + output tokens × output price, summed over every call. Estimate per request, multiply by volume, then pull the levers: caching, shorter prompts, smaller models, batch.
How it works
- Output usually costs ~4–8× input, because each output token is a sequential decode step.
- Input dominates volume: prompt, tools, chunks and history are resent every call, so chat input grows roughly quadratically.
- Reasoning tokens bill as output even when you never see them.
- Cached input reads at ~10–50% of base price; batch APIs take ~50% off work that can wait.
- Estimate from real logs with p50 and p95 token counts, plus headroom for retries and growth.
Check yourself
Answer to mark it learned; your answers aren't scored.
You move a ticket classifier to a reasoning model at the same per-token prices. Request count and input tokens are unchanged, and visible answers stay ~20 tokens, yet the bill jumps several-fold. Most likely cause?
Go deeper
Full explanation
Providers bill per token, with separate input and output prices quoted per million tokens. Output usually costs ~4–8× input because each output token is a sequential decode step, while input is processed in parallel during Prefill vs decode.
Per call: in_tokens × in_price + out_tokens × out_price. Per task, sum every call: an agent loop, or RAG with a query rewrite and a judge, makes several.
What people miss:
- Input dominates volume. System prompt, tool schemas, retrieved chunks and history are resent on every call; chat history makes a conversation's total input grow roughly quadratically with turns.
- Hidden output. Reasoning tokens are billed as output even when you never see them.
- Tokens are not words. ~0.75 English words per token; other languages often need more tokens for the same text.
Levers, cheapest first:
- Trim the prompt: fewer, better chunks; summarize old history.
- Prompt caching: cached input reads at ~10–50% of the base price.
- Cap output:
max_tokens, terse formats, structured outputs. - A smaller model, or Model routing & cascades for the easy traffic.
- Batch APIs (~50% off) for anything that can wait 24 h.
Estimate from real logs with p50 and p95 token counts, multiply by volume, and add headroom for retries and growth.
Code Monthly estimate for a RAG assistant, with two levers
# Monthly cost of a RAG support assistant. All inputs are ASSUMPTIONS:
# take token counts from your logs (p50 and p95) and prices from the provider's page.
PRICE_IN, PRICE_OUT = 3.00, 15.00 # $ per 1M tokens, Sonnet-class list price (2025)
CACHE_WRITE, CACHE_READ = 1.25, 0.10 # Anthropic-style multipliers on the input price
REQUESTS_PER_MONTH = 20_000 * 30
STATIC = 3_000 # system prompt + tool schemas: identical on every call
RETRIEVED = 4_000 # RAG chunks: different on every request
HISTORY = 1_000 # recent turns + the user's question
OUTPUT = 400 # answer tokens (reasoning tokens would bill here too)
def per_request(cache_hit: float | None = None, retrieved: int = RETRIEVED) -> float:
# cache_hit=None: caching off. Otherwise misses pay the write, hits the read.
f = 1.0 if cache_hit is None else (1 - cache_hit) * CACHE_WRITE + cache_hit * CACHE_READ
input_cost = (STATIC * f + retrieved + HISTORY) * PRICE_IN
return (input_cost + OUTPUT * PRICE_OUT) / 1e6
for label, cost in [
("baseline", per_request()),
("+ prompt caching, 90% hits", per_request(cache_hit=0.9)),
("+ 2k retrieved tokens, not 4k", per_request(cache_hit=0.9, retrieved=2_000)),
]:
print(f"{label:<30} ${cost:.4f}/req ${cost * REQUESTS_PER_MONTH:>7,.0f}/month")
# baseline $0.0300/req $ 18,000/month
# + prompt caching, 90% hits $0.0229/req $ 13,761/month
# + 2k retrieved tokens, not 4k $0.0169/req $ 10,161/monthReal example
A support assistant handles 20k requests/day: ~8,000 input tokens (3k static prompt and tools, 4k retrieved chunks, 1k history) and ~400 output tokens. At $3/$15 per 1M (a Sonnet-class list price, 2025): 8,000 × $3/1M = $0.024 plus 400 × $15/1M = $0.006, so $0.03 per request, ~$18k/month. Output is ~5% of the tokens but 20% of the bill. Caching the static prefix (90% hits) brings it to ~$13.8k; sending 2k tokens of better-reranked chunks instead of 4k brings it to ~$10.2k, a ~44% cut without changing the model.
Trade-offs +3 · −2
+ You gain
- Turns "AI is expensive" into a per-request number that product and finance can reason about.
- Shows which lever matters: in most RAG and agent apps, input tokens drive the bill.
- Catches runaway designs (agent loops, unbounded history) before launch, not on the invoice.
− You pay
- Only as good as the token distribution: p95 prompts and agent loops break averages.
- Prices change several times a year; keep the model as a script, not a memorized number.
Use it when
Before launching any LLM feature, when choosing between models or architectures, and whenever a design adds calls per task (agents, rewrites, judges).
Avoid it when
Don't shave tokens on a low-traffic prototype before it works, and never let cost per token override quality: optimize cost per successful task.
Numbers to know 5
- ~4 characters ≈ 1 token ≈ 0.75 English words (OpenAI rule of thumb).
- Output ~4–8× input on 2025 list prices per 1M: GPT-4.1 $2/$8, Claude Sonnet 4 $3/$15, GPT-5 $1.25/$10.
- Cached input reads: ~0.1× base on Anthropic; 50–90% off on OpenAI depending on the model.
- Batch APIs: ~50% off for results within 24 h.
- History: if each turn adds k tokens, N turns send ~k·N²/2 input tokens in total.
Common pitfalls 4
- Estimating from averages: long-tail prompts and agent loops dominate the bill. Use p95 and cost per task.
- Forgetting reasoning tokens: a short visible answer from a thinking model can bill thousands of output tokens.
- Resending the full chat history forever: window it, summarize it or cap it.
- Ignoring retries, eval runs and judge calls: they are real traffic on the same invoice.
Interview questions 2
Answer out loud first, then reveal.
Estimate the monthly LLM cost of this feature on the whiteboard.Show answer
I build it bottom-up. Tokens per request from the prompt layout: system and tools, retrieved context, history, question and expected output, at p50 and p95, not one number. Calls per task: RAG with a query rewrite and a judge is three calls, not one. Then cost per task is the sum of input times input price plus output times output price, times tasks per day, times 30. I add headroom for retries and growth, and show the sensitivity: which term dominates and what caching, routing or a smaller context would save.
The LLM bill doubled last month with flat traffic. How do you investigate?Show answer
Same traffic at double cost means tokens per request or price per token changed. I break the bill down by feature, model and token type (input, cached input, output, reasoning) from the gateway logs. Usual culprits: a prompt edit that broke the cache prefix so hits fell to zero, an agent taking more steps, history no longer truncated, a model upgrade with more thinking tokens, or retries during a provider incident. Then I add per-feature budgets and alerts so the next spike shows up in a day, not on the invoice.