Lesson 6.2 · 8 min read · about 55 min to master at a very slow pace
Controlling Cost: Caching, Routing and Budgets
The levers that cut an LLM bill without hurting quality: prompt caching, response caching, routing easy requests to smaller models, trimming context, and budgets. Each with a worked calculation.
Measure first
Log input tokens, cached tokens, output tokens, model and cost for every call, tagged by feature and client. Optimisation without this data is guesswork; with it, the biggest lever is usually obvious.
Prompt caching
When many requests start with the same long prefix (system prompt, tool definitions, a fixed document), providers can cache it. Cached input tokens are billed at a steep discount (often around 10–25% of the normal price) and processed faster. To benefit, put the stable content first and the changing content (retrieved chunks, the question) last.
Worked example
Each call sends a 4,000-token fixed prefix plus 1,000 tokens that change. Input costs €2.50 per million; cached input is billed at 10%.
- Without caching: € input per call.
- With caching: €.
Input cost drops by 72% from reordering the prompt and turning caching on.
Response caching
If many users ask the same question ("What are your opening hours?"), store the answer. Exact caching matches identical normalised text. Semantic caching matches questions whose embeddings are very close. Be careful: a semantic cache can serve the answer to a different question ("opening hours in Ljubljana" versus "in Maribor"). Use a strict threshold, scope the cache per user group, and expire entries when the documents change.
Routing
Most traffic is easy. Send easy requests to a small, cheap model and hard ones to a large model. A router can be rules (length, topic), a small classifier, or the small model escalating when unsure.
With 70% of requests () on a model costing €0.001 per call and 30% on one costing €0.015: €, about a third of sending everything to the large model. Check with evals that routed answers are still good enough.
The other levers
- Trim context: fewer, better chunks after reranking.
- Cap output: ask for concise answers; set a maximum output length.
- Batch non-urgent work through discounted batch APIs.
- Budgets: daily caps per client with alerts at 50% and 80%, and a defined behaviour at 100% (cheaper model, search-only mode, or a polite pause).
Stuck? Common doubts
Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.
First passDoes prompt caching change the answers?
No. It reuses the model's internal computation for an identical prefix; the output is the same as without caching. It only applies when the prefix matches exactly, which is why stable content must come first and must not contain timestamps or per-user details.
Going deeperIs a cheaper model always the right choice for cost?
No. A cheaper model that fails more often can cost more overall through retries, human escalations and lost trust. Compare cost per successful task on your eval set, not cost per call.
Cheatsheet
Prompt caching
Routing cost
Semantic cache collisions
Check your understanding
Score 75% or more to master this lesson and earn XP. Answer first, then check.