Module 3 · Reliability and cost patterns · Chapter 12 · Free
Caching, batching and cost patterns
- Most AI bills are input tokens, re-sent on every call. Stop paying full price for the same words twice.
- Cache the stable prefix, cache safe answers, batch any work that can wait.
- Meter cost per feature and per successful outcome, or you are signing blank cheques.
The picture: a kitchen that preps shared ingredients once instead of chopping them fresh for every order.
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.
- Nobody can say which feature spends what. There is one bill and no meters.
- Most of the spend is a 40-page instruction document, sent fresh on every call.
- A nightly bulk job runs on the live, full-price tier because nobody moved it.
- Find where the money goes.
- Cache the stable prefix of every prompt.
- Cache answers carefully: exact is safe, similar is risky.
- Batch the work that can wait.
- 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.
- Measure cost per successful outcome, not per call. A cheap call that fails and retries three times costs more than one good call.
- Split the bill by feature. One total hides the feature that eats half the budget.
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:
- Prepped food keeps for a day. A cached answer can go stale the second the source data changes.
- A kitchen serves whoever orders. A cache must never serve one customer's plate to another customer.
- Cached input is cheaper, not free, and the discount varies by provider.
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.
- Put the stable parts first: system instructions, long reference documents, worked examples.
- Keep them byte-identical. One changed word or date near the front can break the match for everything after it.
- 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.
- Exact-match caches are safe. Same question, same user scope, same data version: same answer. Think "what are your opening hours", asked a thousand times.
- Semantic caches are risky. They match questions that look similar, not identical, and the cache makes that judgement silently.
- Two failure modes: a similar question from another user gets the first user's personalised answer, and a correct answer goes stale after the data changes.
- Scope every key: include the user or account, the data version, and the language. When the version changes, old answers die with it.
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.
- Good batch candidates: nightly summaries, bulk extraction, backfills, eval runs.
- Batch APIs from many providers price queued work lower, often around half price, in exchange for a turnaround window that can run up to a day. Check the provider's current terms.
- Keep live what is live: chat, checkout help, anything a user is staring at. Batching those trades a saving for an angry user.
Next: the whole system in one picture.
The picture: three doors, one meter
Press Next to walk the flow.
- A user question or a scheduled job. First: has this exact thing been answered safely before?
- The key covers the question, user scope and data version. A hit returns the stored answer at near zero model cost.
- The prompt opens with the stable, cached prefix, so repeated instructions bill at the cached rate.
- Jobs with no one waiting skip the live tier and queue for batch pricing.
- Cached, generated or batched, the user sees one answer. The route is invisible to them.
- 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
- Tag every call with its feature, so spend lands in the right bucket as it happens.
- Set a budget per feature and alert at, say, 50, 80 and 100 percent of it. The habit is the pattern.
- Track cost per successful outcome beside raw spend, so a "cheap" feature that fails often shows its true price.
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.
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
- Cost per call is the metric. No: cost per successful outcome. Retries and failures are spend too.
- Similar means same. No: a semantic cache can hand one user's answer to another. Scope keys by user and data version.
- Batching is just a discount. No: it is a trade. You pay with waiting time, so only patient work qualifies.
- Caching makes input free. No: cached input is usually discounted, not free, and terms change. Check current pricing.
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.
- Input tokens usually dominate. Measure cost per successful outcome, split by feature.
- Prefix caching: stable text first, byte-identical, variable text last. Discounts vary; check current pricing.
- Exact-match answer caches are safe. Semantic caches need keys scoped by user and data version.
- Batch anything nobody is waiting for. Often around half price, window up to a day; check current terms.
- Worked example: $11,000 baseline, prefix caching to $6,500, batching 80 percent to $3,900 (all illustrative).
- Every feature gets a meter and a budget. No meter, no control.
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".