All glossary terms

Serving

Prefix cache

A saved set of KV states for text that repeats across requests, usually a shared system prompt. A cached prefix is read, not recomputed, so it costs less.

Every request in most applications carries the same leading text: a system prompt with instructions, a persona, a tools list. Without caching, that identical text is prefilled again and again. A prefix cache keeps the KV states of the shared prefix and reuses them, so the model only prefills the part that actually changed.

The savings appear on both sides of the ledger. Time to first token drops, because the expensive prefill of maybe thousands of tokens disappears. And the bill drops, because providers bill cached input tokens at a large discount - reusing stored work is cheaper than recomputing it.

The design questions that follow it are the interview material. Hit rate is everything: a prefix only caches if requests actually share one, so long per-user histories may gain nothing while a fixed system prompt gains on every call. Cache eviction and capacity bound how long the states survive. And routing matters - the request has to reach the machine holding the cache, which is why provider docs sometimes ask you to pin traffic.

Watch the interaction with privacy and correctness: a cache holds computed state from one request and serves it under another, so tenants that truly must not share anything need distinct routing. The mechanics are otherwise exactly the KV cache reuse you would expect.

A worked example

An app sends 50,000 requests a day, each carrying the same 2,000-token system prompt. Assumed provider prices: $3.00 per million uncached prompt tokens and $0.30 when the prefix cache hits, with a 90 percent hit rate.

  1. Prompt tokens per day: 50,000 x 2,000 = 100 million.
  2. No cache: 100 million x $3.00 = $300 per day.
  3. Cache hits cover 90 percent: 90 million x $0.30 = $27; misses 10 million x $3.00 = $30.
  4. Total with the cache: $27 + $30 = $57 per day.

The prefix cache saves $243 per day on prompt tokens that all read the same - the hit rate, not the token count, decides the bill.

Where this shows up

Test yourself

Related terms