Deployed Lab

Module 3 · Reliability and cost patterns · Chapter 12 · Free

Caching, batching and cost patterns

19 min1 clickable picture1 break-it lab1 predict, 2 quick quizzes, 5-question final quiz5-min video review
In 30 seconds

The picture: a kitchen that preps shared ingredients once instead of chopping them fresh for every order.

Pattern card

Pattern: Caching, batching and cost patterns.
Problem: every call re-pays for the same instructions, documents and questions, and urgent pricing is charged for work that was never urgent.
Forces: freshness against savings, latency against price, shared answers against privacy.
Solution shape: cache stable prefixes and safe answers, route patient work to batch APIs, meter every feature.
Watch out: a cache keyed too loosely serves one user's answer to another.

The cold open Ledgerline is a made-up finance tool. Its invoice summariser works well, and the monthly model bill quietly grows to $11,000 (illustrative).
The plan for this chapter
  1. Find where the money goes.
  2. Cache the stable prefix of every prompt.
  3. Cache answers carefully: exact is safe, similar is risky.
  4. Batch the work that can wait.
  5. Work one bill down step by step, then break a costly design and fix it.

Next: where the money goes.

Step 1: Where the money goes

In most products, input tokens dominate the bill. Instructions, documents and history are re-sent on every call, while the answer is usually short.

Think of a kitchen. Shared prep is your input; the plated dish is the output. Good kitchens prep once and serve many times.

Where the picture breaks down:

Next: the biggest, safest saving first: the prompt prefix.

Step 2: Cache the stable prefix

Many providers offer prompt or context caching: if the start of your prompt matches a recent call exactly, the repeated part bills at a lower rate and processes faster. Check current pricing before you budget on the discount.

  1. Put the stable parts first: system instructions, long reference documents, worked examples.
  2. Keep them byte-identical. One changed word or date near the front can break the match for everything after it.
  3. Put the variable parts last: the user's question, today's data, anything personal.

The rule: design your prompt like a form letter. The letterhead never changes; only the name and last paragraph do. See also the FDE chapter Latency, streaming and cost.

Next: caching whole answers, where the real danger lives.

Step 3: Cache answers, exact first, similar with care

An answer cache stores a finished reply and serves it again instead of calling the model.

Predict first:

The cache matched on similar wording and knew nothing about accounts: a privacy incident caused by a cost saving. Scope the key by user, or do not cache personalised answers at all.

Next: the work that never needed a live call.

Step 4: Batch the work that can wait

Ask one question of every job: does a person need this answer in the next few seconds? If not, it is batch work.

Next: the whole system in one picture.

The picture: three doors, one meter

Press Next to walk the flow.

Caching, batching and the cost meter A request checks the answer cache first. On a miss it calls the model with a cached prefix. Patient work goes to the batch queue. Requestuser or job Answer cacheexact match? Model callcached prefix first Batch queuework that can wait Answer outserved or generated Cost meterbudget and alerts miss reply
  1. A user question or a scheduled job. First: has this exact thing been answered safely before?
  2. The key covers the question, user scope and data version. A hit returns the stored answer at near zero model cost.
  3. The prompt opens with the stable, cached prefix, so repeated instructions bill at the cached rate.
  4. Jobs with no one waiting skip the live tier and queue for batch pricing.
  5. Cached, generated or batched, the user sees one answer. The route is invisible to them.
  6. Each feature records spend against its budget. A spike alerts a human before the invoice does.

Next: the worked example, one real bill taken apart.

Step 5: Worked example, 100,000 reports a month

A made-up pipeline summarises 100,000 reports a month. Every figure is illustrative. Each live call costs an illustrative $0.11, of which $0.06 is the shared 6,000-token instruction prefix (illustrative price: $10 per million input tokens, so 6,000 tokens cost $0.06).

Step 1, the baseline. 100,000 calls × $0.11 = $11,000 a month.

Step 2, cache the prefix. Say cached input bills at one quarter of the normal rate (illustrative; check your provider). The prefix now costs $0.06 ÷ 4 = $0.015 per call. New call cost: $0.11 − $0.06 + $0.015 = $0.065. New monthly total: 100,000 × $0.065 = $6,500.

Step 3, batch what can wait. 80 percent of reports are overnight jobs: 80,000 calls. At half price: 80,000 × $0.065 × 0.5 = $2,600. The other 20,000 stay live: 20,000 × $0.065 = $1,300.

Total: $2,600 + $1,300 = $3,900, down from $11,000. That is $7,100 saved, about 65 percent, with no change to the summaries themselves.

Next: meters and budgets, then the lab.

Step 6: Meters and budgets per feature

A feature without a meter is a blank cheque: you learn its cost from the invoice, weeks later.

Next: break a costly design, then design your own.

Step 7: Hands-on lab, break Ledgerline's cost design

Why this exercise: the three most expensive cost mistakes are design choices, not code bugs. Spotting them in a config is the skill.

Lab: Ledgerline's report pipeline is Step 5's worked example: 100,000 reports a month, $0.11 a live call, $0.06 of it the shared 6,000-token instruction prefix. This lab runs in your browser and needs JavaScript enabled.

Design exercise: a podcast show-notes generator. A small brief (illustrative):

# brief, illustrative
product: show notes from podcast transcripts
volume: 300 episodes a month
shared: the same style guide (4,000 tokens) on every call
personal: each show has its own sponsor lines and links
deadline: notes needed 1 hour after upload, not live

On paper, decide: what goes in the cached prefix, what gets an answer cache, what runs on batch, what gets a meter. A sound answer: the style guide sits first and identical in every prompt; episodes are batched, since an hour of slack fits a batch window; sponsor lines stay out of any shared cache, as they differ per show; spend is metered per show.

When NOT to cache: skip caching when answers are personal and hard to scope safely, when source data changes faster than answers are reused, when volume is so low the cache costs more than it saves, and when one wrong repeated answer costs more than the calls ever would.

Field note, November 2026

Production teams commonly find their largest saving in the prompt, not the model: one shared instruction block, kept identical, dominates input spend at scale. Treat caching discounts and batch terms as moving targets and re-check them each quarter. Common practice, not a statistic.

Mix-ups to avoid

Chapter quiz

Five questions. Score 70% or more to mark this chapter complete.

Review this chapter in 5 minutes

Watch the whole chapter recapped: input tokens and cost per outcome, prefix caching, safe answer caching and scoped keys, batching patient work, and per-feature meters.

Decision rule: before paying for a call, ask: was it answered before, is the prefix cached, and can it wait? If any answer is yes, do not pay full price.

Review

Glossary

Prompt caching
Cheaper, faster processing for a prompt prefix that repeats exactly. Example: the same instructions opening every call.
Stable prefix
The unchanging start of a prompt, placed before anything personal or dated.
Answer cache
Finished replies stored and served again instead of calling the model. Example: opening hours, served all day.
Semantic cache
A cache matching similar, not identical, questions. Bigger savings, higher risk of a wrong or leaked answer.
Cache key
The fields a cached answer is stored under. Safe keys include user scope and data version.
Batch API
A queued, lower-priced way to run calls that can wait. Example: overnight summaries delivered by morning.
Cost per successful outcome
Total spend divided by tasks actually completed, retries included. The honest unit price of a feature.
Cost meter
Per-feature tracking of spend against a budget, with alerts.
Stale answer
A cached reply that was right once and is wrong now, because the source data changed.

Next: "Streaming and UX patterns for slow models".