Topic 3 of 5 · 5 min read
The KV cache behind prompt caching
Goal: Explain why a stable prompt prefix makes repeat calls cheaper and faster and spot a prompt layout that breaks the cache.
The KV cache stores the work a model already did on earlier tokens, and prompt caching lets a later call reuse that work when its prompt starts with exactly the same text. The result is lower cost and faster first tokens for calls that repeat a long, unchanged beginning.
Check yourself · 1 of 3
Prompt caching can reuse work only when the new prompt does what?
Why it matters
Many apps send the same long start every time: a system prompt, tool descriptions, a policy document, a long file. Without caching, the model re-reads all of it on each call. With caching, it reads it once and then reuses it.
How it works
While the model reads tokens, it keeps intermediate results for each one (called keys and values, hence KV). These let it write the next token without redoing the earlier work. Inside one call this cache is automatic and it also uses memory, which grows with the length of the conversation. Prompt caching goes one step further: a provider keeps that stored work for a short time so a new call can reuse it.
The catch is that matching is by prefix. Reuse only works up to the first token that differs. Everything after the first change is read again.
How to lay out a prompt for caching
- Put stable content first. System instructions, tool definitions, reference documents.
- Put changing content last. The user's question, today's date, a request id.
- Do not edit the front. A timestamp, a random id or a reordered list near the top breaks the match for everything after it.
- Check the provider's rules. Providers differ on minimum length, how long entries stay, whether you mark the cache point yourself, and how cached tokens are billed. Read the docs for the one you use, and watch the usage fields in the response to confirm hits.
Before and after
Before: The prompt starts with "Current time: 14:03:22", then the long policy, then the question. The first line changes every second, so nothing is ever reused.
After: The policy comes first and the time moves down next to the question. Calls now share a long identical start.
What goes wrong
- Putting dynamic data at the top of the prompt.
- Assuming a cache hit without checking the usage numbers.
- Expecting a cache to last forever; entries expire.
- Caching content that is only used once, which adds cost with no saving.
Examples
A layout that breaks the cache
[1] Current time: 2026-12-02 14:03:22 <- changes every call
[2] System instructions (3,000 tokens)
[3] Product manual (40,000 tokens)
[4] User question
The first line changes each time, so no later token can be reused. Moving the time down beside the user question lets lines 2 and 3 be reused.
Checking that it works
After moving the dynamic parts, an engineer sends the same request twice and reads the usage block in the second response. It reports most input tokens as read from cache. Without that check, the team would only be guessing.
Key points
- The KV cache keeps the model's earlier work so each new token does not redo everything.
- Prompt caching reuses that work across calls when the prompt starts with identical text.
- Only the unchanged prefix is reused; the first difference ends the match.
- Stable content goes first and changing content goes last.
- Providers differ in rules and billing, so confirm cache hits in the usage data.
Common mistakes
- Placing timestamps, ids or per-user data at the start of the prompt.
- Never checking usage fields to see whether the cache was hit.
- Assuming cached content stays forever.
- Caching small or one-off prompts that will not be reused.
Check yourself · 2 of 3
A prompt starts with a live clock line, then a long manual, then the question. Calls never hit the cache. What is the best change?
Check yourself · 3 of 3
How can you check that caching is working for your app?
Practice items, not counted towards your mastery.
Start here
- Optimizing LLMs for speed and memory (Hugging Face Transformers docs) (opens in a new tab)DocsFree
Covers the KV cache, quantization and faster decoding with runnable settings and plain explanations.
- Mixture of Experts Explained (Hugging Face) (opens in a new tab)ArticleFree
A clear walk through experts, routers, and why total size and active size differ.
- Prompt caching (Claude docs) (opens in a new tab)DocsFree
A vendor's own rules for cacheable prefixes, what breaks a cache hit, and how cached reads are billed.