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.

Not startedJump to quiz

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

Stable prefix first (system prompt, tools, fixed docs), changing content last. Cached tokens are billed at a steep discount.

Routing cost

Blended cost for a share routed to the small model.

Semantic cache collisions

Similar questions can need different answers. Strict thresholds, scoped caches, expiry on document change.

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1calculateEach call sends a 6,000-token fixed prefix and 2,000 changing tokens. Input costs €2 per million; cached tokens cost 10% of that. What is the input cost per call with caching?
Q2diagnoseYou enabled prompt caching but the cache hit rate is near zero. The system prompt starts with "Current time: …". Why?
Q3calculate80% of requests go to a small model at €0.002 per call and 20% to a large one at €0.02. What is the blended cost per call?
Q4diagnoseA semantic cache answers "What time does the Maribor branch open?" with the Ljubljana branch's hours. What went wrong?
Q5interpretModel A costs €0.002 per call and succeeds on 60% of tasks; model B costs €0.006 and succeeds on 95%. Failed tasks go to a human costing €2 each. Which is cheaper per task?