Decompose the cost of a single API call into its components and explain which dominates.
Write the cost formula for a single LLM API call given non-cached input tokens, cached input tokens, and output tokens. Explain which term typically dominates on production workloads and what that implies for cost optimization.
Per-call cost splits into new input, cached input, and output tokens; output tokens are roughly five times pricier per token, so output usually dominates the bill.
Think of a taxi ride with two meters. The first meter runs while you read out your destination to the driver, that is the input. The second meter runs while the driver narrates the whole route back to you, that is the output. The reading-out meter is cheap and ticks fast in parallel. The narrating meter is several times more expensive per word, because the driver speaks one word at a time and you pay a premium for each. There is even a loyalty discount: if you give the same long address every trip, the company remembers it and charges almost nothing to re-read it. So your bill is mostly driven by how much the driver talks back, not how much you said up front.
Detailed answer & concept explanation~7 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
4 min: write the three-term formula, state the output 5x and cached 0.1x ratios, derive the crossover, then rank caching and output trimming as the levers and flag agent-loop compounding.
| Token type | Phase | Relative rate | Cost lever |
|---|---|---|---|
| New input | Prefill, parallel, compute-bound | 1x baseline | Trim only after output is minimized |
| Cached input | Prefill, served from cache | About 0.1x | Cache every stable prefix |
| Output | Decode, sequential, bandwidth-bound | About 5x | Shorten responses, the top lever |
Real products, models, and research that use this idea.
- Anthropic prices Claude Opus 4.7 output tokens roughly five times its input rate, with prompt caching billed near a tenth of input on cache reads.
- OpenAI's GPT-5.5 pricing keeps output several times the input rate and offers automatic prompt caching discounts on repeated prefixes.
- Cursor and similar coding agents lean on prefix caching because each agent step re-sends a large growing file and tool context as input.
- RAG chat products cache the long fixed system prompt and retrieved boilerplate, leaving output length as the main remaining cost driver per turn.
- Gemini 3.1 Pro exposes context caching so high-traffic apps amortize a shared instruction prefix across many calls at a steep discount.
What an interviewer would ask next. Try answering before peeking at the approach.
QDerive the exact condition under which output cost overtakes input cost.
QWhy is output billed several times more than input at the hardware level?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Assuming the bill tracks total token count, so trimming a long prompt is the big win. Output tokens cost several times more each, so a short response usually matters more than a short prompt.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.