AI Systems

Caching — Reducing Latency and Cost

LLM inference is expensive in both latency and dollars per token. Caching stores and reuses previously computed results so repeated or near-duplicate queries never reach the model at all. A well-tuned caching layer routinely cuts inference spend by 30 to 80 percent and drops p50 latency from seconds to tens of milliseconds on a hit — this is the pattern interviewers and reviewers read as evidence someone thinks about unit economics, not only accuracy.

Quick reference
What it is
Storing and reusing previously computed model outputs so repeated or near-duplicate requests skip inference entirely.
Mental model
Don't ask the expert the same question twice — write the answer down the first time and check your notes before asking again.
Key numbers
A well-tuned cache cuts inference spend 30–80%; semantic cache similarity thresholds typically sit 0.90–0.97 depending on false-positive tolerance.
Reach for it when
Query volume is high and a meaningful fraction of traffic is exact or near-duplicate — the common case for FAQ-style and support traffic.
A query checks the cache; a hit returns immediately, a miss calls the LLM and writes the fresh answer back to the cache for next time User Query Cache Lookup Hit cos_sim > threshold Miss call LLM API LLM write-through to cache
Every miss becomes a hit for the next near-duplicate query — the write-through step is what turns a lookup into a compounding cost saving over time.

Exact-match caching

Hash the normalized prompt (whitespace-trimmed, case-folded where safe) and use it as a key in Redis or Memcached. Seeing the same key again returns the cached response without ever calling the model. Hit rates are low for free-form conversational text where phrasing varies, but high for automated pipelines and batch jobs with canonical inputs. It's trivial to implement, carries zero risk of returning a semantically wrong answer, and should be the first layer in any caching stack because it's nearly free.

Semantic caching

Embed incoming queries and search the cache by cosine similarity rather than exact string match — if a new query's embedding falls within a similarity threshold of a cached query (typically 0.90 to 0.97, depending on tolerance for false positives), the cached response comes back. This lifts hit rates substantially for conversational traffic where users ask the same question differently worded. The threshold is the entire design problem: too low and a wrong cached answer gets served to a different question, too high and hit rate collapses back toward exact-match levels. Tune it empirically against a labelled set of near-duplicate and non-duplicate pairs, and treat it as a metric monitored continuously, not set once at launch.

Prefix and KV caching

This operates at the model-serving layer, not the application layer. When multiple requests share a common prompt prefix — a long system prompt, few-shot exemplars, a large retrieved context reused across turns — the transformer's key-value attention states for that shared prefix are computed once and reused, avoiding redundant forward passes. Most major inference providers expose this automatically or via an explicit cache-control parameter, and savings on time-to-first-token can exceed 80 to 90 percent for long shared prefixes. Understanding this distinguishes candidates who think about model-serving internals from those who only think in application-layer terms.

Productionizing this pattern

What changes past a demo

Real-world applications

Embedded applications

Gotchas

Field notes