Reliability and Cost Engineering › Caching and Its Discontents

Michael BrenndoerferDecember 2, 20263 min read
Language AI HandbookAll chapters
Part 64 of 67Compound AI System Design
Chapter 490Reliability and Cost Engineering › Caching and Its Discontents
Reading section 7 of 12 · Caching and Its Discontents · 3 minSection 7 of 12 · 3 min

Section 7 of 12 · 3 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

A cache can skip a retrieval or generation call. That saves work on a hit, but a response retained under an outdated policy can still look plausible. Confirm what a cache is allowed to reuse before evaluating its hit rate.

A response cache can sit alongside a provider's cache of model state.

  • Exact-match response cache. Reuse a previous answer only when request semantics, dependency versions, and current authorization allow it. Include the inputs and relevant state in the key or invalidate entries when that state changes.
  • Provider prompt cache. A provider may reuse model prefill state for an eligible matching prefix. Some pricing distinguishes cached and uncached input, as in OpenAI's prompt-caching documentation. This mechanism saves prefix processing while generation still runs.
  • Semantic cache. A match may reuse a response when a query embedding is close enough to the stored one. A weaker threshold can admit more candidates on a fixed workload. The share of wrong hits depends on which candidates it admits.

Use the hit rate to account for how often each path is taken.

E[T]=h Thit+(1−h) Tmiss,E[C]=h chit+(1−h) cmiss\mathbb{E}[T] = h \, T_{\text{hit}} + (1-h)\, T_{\text{miss}}, \qquad \mathbb{E}[C] = h\,c_{\text{hit}} + (1-h)\, c_{\text{miss}}

Here hh is the probability of a cache hit. Each TT is a conditional mean latency and each cc is a conditional mean cost, weighted by the probability of that path. For a semantic cache, if ff is the fraction of hits that give a wrong answer, the probability of a wrong cache hit per request is hfhf. That quantity says nothing about miss-path mistakes. The two operating points below are stipulated examples, not measured threshold performance.

PythonIn [18]
CollapseExpand
# Cost/latency model for a retrieval + generation step.
T_HIT, T_MISS = 0.03, 0.9  # seconds
C_HIT, C_MISS = 0.0, 0.012  # dollars per request


def cache_expected(
    hit_rate, t_hit=T_HIT, t_miss=T_MISS, c_hit=C_HIT, c_miss=C_MISS
):
    t = hit_rate * t_hit + (1 - hit_rate) * t_miss
    c = hit_rate * c_hit + (1 - hit_rate) * c_miss
    return t, c


# Two operating points for a SEMANTIC cache with a similarity threshold.
# Illustrative hit and wrong-hit fractions, not fitted to a workload.
settings = {
    "conservative (sim>=0.95)": dict(hit_rate=0.20, false_positive=0.002),
    "aggressive   (sim>=0.85)": dict(hit_rate=0.55, false_positive=0.060),
}

for name, s in settings.items():
    t, c = cache_expected(s["hit_rate"])
    # Probability a served response is WRONG due to a bad cache hit:
    # wrong only on hits, and only the false-positive fraction of those.
    p_wrong = s["hit_rate"] * s["false_positive"]
    print(f"{name}")
    print(
        f"   hit_rate={s['hit_rate']:.2f}  E[T]={t:.4f}s  "
        f"E[cost]=${c:.4f}  P(wrong via cache)={p_wrong:.4f}"
    )
ConsoleOut [18]
CollapseExpand
conservative (sim>=0.95)
   hit_rate=0.20  E[T]=0.7260s  E[cost]=$0.0096  P(wrong via cache)=0.0004
aggressive   (sim>=0.85)
   hit_rate=0.55  E[T]=0.4215s  E[cost]=$0.0054  P(wrong via cache)=0.0330

The two settings give mean latencies of 0.726 and 0.4215 seconds and mean costs of $0.0096 and $0.0054 per request. Wrong cache hits rise from 0.0004 to 0.033 per request: 40 versus 3,300 per 100,000 requests in expectation. Sample and label cache hits; a hit-rate dashboard alone cannot tell you whether that trade is acceptable.

Cache keys are a correctness specification

Bind reuse to the authorized tenant and current permissions. Include model and prompt versions, retrieval context or its version, and locale when they affect the answer. A TTL can limit the lifetime of a stale entry but does not replace access checks or invalidation when permissions or source data change.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy