download

Prompt caching is on, but the hit rate is zero. Why?

System design~12 minReviewed 1 Nov 2026Free · No sign-in

Question

A support bot sends each message to an LLM API with prompt caching turned on. Every prompt is built in this order:

  1. the current time and the user's name (30 tokens);
  2. the support rules and product docs, the same for every user (9,000 tokens);
  3. the user's question (about 50 tokens).

The usage data shows zero cached tokens on every request.

Why does the cache never hit? Change the prompt so most of it is cached. Then name two other reasons a cache can still miss.

Show hints (3)
  1. "WHERE THEY DIFFER"

    The cache helps only when a prompt starts with exactly the same tokens as an earlier one. Two requests arrive seconds apart. Where do their prompts first differ?

  2. "THE FIRST TOKEN"

    The time comes first, and it changes every second. So the shared start is empty, and the 9,000 stable tokens after it never count.

  3. "STABLE FIRST"

    Move the rules and docs to the very start. Put the time, the user and the question after them. What do two requests share now?

Show checked explanation

The cache matches an exact prefix. This prompt starts with the time and the user's name, which change on every request, so no two prompts share a start. Put the stable part first: rules and docs, then the time, the user and the question. Then 9,000 of about 9,080 tokens can come from the cache.

"SOLUTION · PYTHON"
STABLE = [("support rules", 3000), ("product docs", 6000)]  # (block, tokens)
requests = [("09:00:01", "Ana", "Where is my order?"),
            ("09:00:07", "Ben", "Can I return shoes?"),
            ("09:00:12", "Ana", "And the refund?")]

for stable_first in (False, True):
    seen, cached = set(), []  # every prefix sent so far; cached tokens
    for now, user, question in requests:
        varying = [(f"time {now}", 20), (f"user {user}", 10)]
        blocks = STABLE + varying if stable_first else varying + STABLE
        prompt = blocks + [(question, 50)]
        prefixes = [tuple(prompt[:k]) for k in range(1, len(prompt) + 1)]
        hits = [p for p in prefixes if p in seen]  # shortest to longest
        cached.append(sum(tokens for _, tokens in hits[-1]) if hits else 0)
        seen.update(prefixes)
    order = "stable first" if stable_first else "time first"
    print(f"{order}: cached tokens {cached}")
"OUTPUT"
time first: cached tokens [0, 0, 0]
stable first: cached tokens [0, 9000, 9000]

Code run on CPython 3.12 against the example above. Checked by an editor other than the author.

Walkthrough

Prompt caching saves the work the model did on the start of a prompt. A later request reuses it only if its prompt begins with exactly the same tokens. One changed character ends the match, and everything after it is processed again.

requesttime first: cached tokensstable first: cached tokens
09:00:01, Ana00
09:00:07, Ben09,000
09:00:12, Ana09,000

With the time first, even Ana's second message misses, because the time changed. With the rules and docs first, the first request fills the cache and the next ones reuse 9,000 tokens. Only about 80 tokens are new each time. Cached tokens cost less and are processed faster.

So order a prompt from the most stable part to the least: tools, system rules, shared documents, older chat turns, then the newest message.

Other common reasons for a miss:

  • The order of tools changes. Build the tool list from a Python set and its order can differ between worker processes, because string hashes are randomized per process. Use a fixed, sorted list.
  • The prefix is too short. Providers cache only prompts above a minimum length that depends on the model. Below it, nothing is cached, often with no error.
  • The entry expired. Entries expire after a while without use, sometimes after only a few minutes.
  • Caching is not on. Some providers cache automatically. Others need you to mark the cacheable prefix in the request.

Common mistakes

  • Rewriting old chat turns on every request, for example by summarising the history again. The cache misses from the first changed turn. Append new turns and leave old ones alone.
  • Expecting parallel first requests to share the cache. An entry may not exist until the first response starts, so let one request warm the cache first.
  • Judging the fix by speed alone. Read the cached-token count the API returns with each response. Zero means the start still changes.

Follow-up questions

  • "Traffic is one request every 10 minutes. Does caching help?" Only if entries outlive the 10-minute gap. Some lifetimes are only a few minutes. Then every call writes and none reads, and where a cache write costs more than normal input, the bill goes up. Choose a longer lifetime if offered, or turn caching off for this prompt.
  • "Where should the chat history go?" After the shared rules and docs, oldest turn first, newest message last. Each new turn is appended, so the earlier conversation stays an exact prefix and can be cached too.

What a strong answer covers

Knows the cache matches an exact prefix and spots the time at the top. Orders the prompt from stable to changing. Names other misses: tool order, a too-short prefix, an expired entry. Checks the cached-token count instead of guessing.

Open in interactive practice deck →

Original OfferVibe question. Topic idea from System Design with AI Interview Guide (Rohit Bhardwaj, 2026); text, examples and code are our own.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.