Caching, Batching & Cost Engineering
- Implement exact and semantic response caching and measure hit rate vs staleness risk
- Explain prompt-prefix caching and structure prompts to exploit it
- Control output length deliberately, since output tokens usually cost several times input tokens
- Route requests between a cheap and an expensive model by difficulty, with an escalation rule
- Account cost per request and per feature, and cut your capstone's cost measurably
| Spaced-rep warm-up: due cards incl. Day 47 caching & Day 129 routing | 10 min |
| ELI5 + tech read; list the four levers from memory | 15 min |
| Guided: two-tier cache + cost ledger | 40 min |
| Practice: difficulty router with escalation | 20 min |
| Project: cost report with before/after and projections | 25 min |
| Quiz + flashcards | 10 min |
Builds on: Day 47 โ Caching & queues โ cache-aside, TTL, invalidation ยท Day 107 โ LLM APIs II โ cost math per feature ยท Day 129 โ Model selection โ cascades & routers ยท Day 155 โ Serving & inference optimization
On Day 47 the pantry was a metaphor for keeping popular ingredients within arm's reach. Now imagine the restaurant discovers that 30% of orders are literally the same dish โ "the usual" โ and starts plating those from a warming shelf instead of cooking from scratch. That is response caching. Then the chef notices something subtler: hundreds of different orders all start with the same base โ the same stock, the same sauce. So the kitchen pre-makes the base once and only cooks the part that differs. That is prompt-prefix caching: your system prompt and few-shot examples are the stock; the provider charges you a fraction to reuse the already-processed prefix.
The last trick is staffing. You do not put the executive chef on grilled-cheese orders. A router looks at each ticket and sends easy ones to the line cook (small, cheap model) and genuinely hard ones to the executive chef (frontier model) โ with a rule that a line-cook failure escalates rather than ships. Caching, prefix reuse, shorter outputs, and routing are the four levers that routinely cut LLM bills by half or more without touching quality where it matters.
Cost is where AI projects die quietly. A demo that costs $0.04/request is charming at 100 requests/day and a budget crisis at 100k. FDEs get asked "what will this cost at scale?" in the first customer meeting, and "why did the bill triple?" in month two. Engineers who can show a cost-per-request breakdown and then cut it 40% with caching and routing โ while proving quality held via the Day 140 eval harness โ are the ones trusted with production. Day 173 turns this skill into customer-facing ROI math.
The pantry โ a cache-aside read: miss, fill, hit
step 1 / 6Cache-aside: the app ALWAYS asks the pantry (cache) before going to the market (database). First request for user:42 โ the pantry is empty.
Guided practice
Build a two-tier cache wrapper
25 min- Create
llm_cache.pyfrom the starter. Tier 1 is an exact cache keyed on a hash of (model, normalized prompt, params). Tier 2 is a semantic cache using sentence-transformers embeddings with a cosine threshold. - Wire it in front of your capstone's answer function (cache-aside, Day 47 pattern): check exact โ check semantic โ call model โ store.
- Replay 30 questions from your Day 140 golden set, then replay 15 paraphrases of them (write them or generate them). Record: exact hits, semantic hits, misses.
- Lower the semantic threshold from 0.92 to 0.80 and replay again. Find a paraphrase pair where the cache now returns a WRONG answer โ that is the staleness/precision trade made visible.
- Write down your chosen threshold and TTL, with one sentence of justification each.
Cost accounting per request
15 min- Add a
cost_ledgerto your capstone: after every model call, log a structured line with feature, model, input_tokens, output_tokens, cache_status, and computed dollars (put your provider's real prices in a PRICES dict). - Replay your golden set through the app once with caching off, once with caching on.
- Run the starter aggregation script over the log: cost per request p50/p95, cost per feature, and the percentage saved by caching.
- Sanity-check against the provider dashboard's usage page โ if your ledger disagrees by more than ~10%, find the uncounted call (retries and judge calls are the usual suspects).
On your own
The router that must not lie
20 minAdd difficulty routing to the capstone: questions matching "easy" criteria (short, single-fact, high retrieval confidence) go to the small model; everything else to the big one. Your escalation rule: if the small model's answer fails citation validation or the groundedness check, retry on the big model โ never ship the failure.
Constraints: (1) re-run the Day 140 golden set and report pass rate per route โ overall quality must not drop more than 2 points; (2) report the cost saving; (3) log the route decision so Day 157's dashboard can chart route mix.
Hints: retrieval score percentiles from Day 117 make a decent difficulty signal; when in doubt, route up.
Cut the capstone's cost, with receipts
Produce docs/cost_report.md: (1) baseline cost per request and per 1k requests from the ledger, broken down by feature; (2) the three levers you applied โ caching, prompt reordering for prefix cache, output caps, or routing (pick at least two) โ each with its measured saving; (3) the quality guardrail: golden-set pass rate before and after, with the Day 139 error bars; (4) a projection table: monthly cost at 1k, 10k, and 100k requests/day, before vs after. This document feeds directly into Day 173's ROI one-pager.
Common mistakes & misconceptions
- Shipping a semantic cache without an eval. A 0.85-similarity hit on a different-jurisdiction question is a confident wrong answer; tune the threshold against labeled paraphrase pairs, not vibes.
- Forgetting to invalidate the response cache when the RAG corpus updates. The retrieval index changed but the cache still serves answers grounded in deleted documents.
- Putting volatile content (retrieved chunks, timestamps, user name) at the TOP of the prompt โ it breaks the shared prefix and forfeits prefix-cache discounts. Stable first, volatile last.
- Optimizing input tokens while ignoring output. Output tokens usually cost 3โ5ร more; a max-token cap and a brevity contract often save more than any retrieval trim.
- Routing to the cheap model with no escalation path. The rule is "cheap first, but never ship a validation failure" โ otherwise your cost cut is a silent quality cut.
- Measuring savings on cache-warm replays only. Report steady-state hit rate on realistic traffic, not the 100% hit rate of replaying the same 30 questions twice.
Q1. Your prompt is: [retrieved chunks][system instructions][few-shot examples][question]. Why is prefix caching saving you almost nothing?
Q2. A semantic cache at threshold 0.80 returns the France leave-policy answer for a Germany question. The correct first fix isโฆ
Q3. Why must the golden set be re-run after adding a cheap-model route?
Go deeper โ curated resources
- Request coalescing โ When 50 identical requests arrive in the same second (a shared dashboard, a retry storm), let one call the model and 49 await its result. Same idea as the thundering-herd cache guard from Day 47.
- Cache wrapper live in the capstone with a justified threshold and TTL
- Cost ledger reconciles with the provider dashboard
- cost_report.md committed: โฅ25% saving, quality within 2 points, projections included
- Quiz โฅ 2/3
โ Back: This is Day 47's cache-aside pattern and TTL discipline applied to LLM responses, Day 107's cost math made operational, and Day 129's routing decision finally implemented with an eval guardrail from Day 140.
Forward โ: Day 157 charts cost/request on the monitoring dashboard. Day 160's design answers must name these levers unprompted, and Day 173 converts today's cost report into the customer-facing ROI one-pager.
Unlocks: D173 ROI, Pricing & Cost Analysis