Prompt Caching
Reuse of long stable prefixes to cut cost and latency on repeated system prompts and docs.
In plain words
If you keep sending the model the same long beginning, it remembers it for a little while so it doesn't have to re-read it.
Think of it like…
A copy shop that keeps a stack of the first 40 pages of a manual already prepared. If your order starts with those same 40 pages it hands you the stack instantly and at a discount; change one word on page 3 and it has to redo everything after that point.
Where you’ve seen it
A contract Q&A tool puts a 30,000-token contract first, then the user's question. The first question pays full price and prefill time; the next questions within the cache window reuse the contract prefix, so input tokens for it are billed at a fraction (providers commonly discount cached reads by roughly 50 to 90 percent) and first-token latency drops from several seconds to under one. Putting the contract after the question would have made every request a miss.
Words you’ll hear
- Prefix cache
- Stored model state for an exact token prefix, reusable by later requests.
- Cache hit / miss
- A hit reuses the stored prefix; a miss processes it again at full price.
- TTL
- How long an entry stays available; reads usually refresh it.
- Cache breakpoint
- A marker (on some APIs) showing where the cacheable prefix ends.
Visual diagram
Theory & deep dive
How a prefix cache works
During prefill the model computes keys and values for every prompt token. Those depend only on the tokens before them, so for an identical prefix the result is identical and can be stored (see the KV cache lesson). The provider hashes the prefix, looks for a stored match when a new request arrives, and skips recomputation for the matched part, processing only the new tail. Some providers cache automatically above a minimum length (for example around 1,024 tokens); others need you to mark breakpoints.
Pricing varies by provider and changes over time, but the pattern is consistent: cached input reads cost a fraction of normal input, some providers add a surcharge for writing a cache entry, and entries last for a short TTL that each hit extends. Always read the current docs and compute break-even: caching pays off when a prefix is reused more than a couple of times within its TTL.
If a write costs 1.25× normal input and reads cost 0.1×, a 20K-token prefix used twice costs 1.25 + 0.1 = 1.35× one uncached call, versus 2× for two uncached calls; by the fifth use it's 1.25 + 4 × 0.1 = 1.65× versus 5×.
Designing prompts that hit the cache
Put the stable material first and volatile material last: system prompt, tool definitions, examples and reference documents, then conversation history, then the new user message. Keep the prefix byte-identical: don't insert timestamps, request ids, user names or randomised ordering near the top, and keep tool definitions in a fixed order. In multi-turn chats, append new turns to the end so earlier turns remain a cached prefix; rewriting or summarising old turns changes the prefix and invalidates the cache from that point.
Monitor it: providers return usage fields (such as cached tokens and cache-write tokens), so log the cache hit rate and cost per request. For bursty traffic, a cache that expires between bursts gives few hits, so consider keep-alive requests or longer TTL options where offered. Caching reduces cost and latency but doesn't change model output, so it's safe to enable broadly once prompts are ordered correctly.
Moving "Current time: 14:32:07" from the first line of the system prompt to the last user message turns a 0% hit rate into a 95% hit rate on the 6,000-token system prompt, with identical answers.
How it works, step by step
Step 1 of 41. Identify the stable prefix
List what repeats across requests: system prompt, tools, examples, reference documents.
- Min length
- provider-specific
When to use it & trade-offs
What it is
Prompt caching lets a provider reuse the work it did on a long, identical prompt prefix from an earlier request, so repeated input is billed at a steep discount and starts faster. The cached part is the model's internal key-value state for those tokens. A request gets a cache hit only if its beginning matches a recent request token for token, and cached entries expire after a TTL (often minutes) unless refreshed by use.
Why it matters: Many applications send the same thousands of tokens on every call: a long system prompt, tool definitions, a policy handbook, a document the user is chatting about. Without caching you pay full price and wait for full prefill each time. With it, input cost and time-to-first-token can fall dramatically, but only if you structure prompts so the stable part comes first and stays byte-identical.
When to reach for it
- Long system prompts and tool definitions
- Chatting over a fixed large document
- Agent loops that resend a growing but append-only history
When to avoid or be careful
- Timestamps, request ids or user names at the top of the prompt
- Rewriting earlier conversation turns each request
- Enabling caching for prefixes used only once
Caching options for repeated context
Strengths: Lower input price and faster first token; no output change.
Watch out: Needs exact prefix match; short TTL.
Strengths: Zero model cost on a hit.
Watch out: Only exact or near-exact repeats; staleness risk.
Strengths: Small prompts overall.
Watch out: Retrieval quality limits answers; no cached long context.
Interview signals & numbers to know
When this topic comes up
- “Our input token bill is huge”
- “First-token latency is slow on long prompts”
- “What's a cache breakpoint?”
- Prompt ordering
- Prefix reuse
Benchmarks & rules of thumb
- ≈ 50 – 90%
- typical discount on cached input reads
- minutes
- typical cache TTL
- ≈ 1,024 tokens
- a common minimum cacheable prefix
Common mistakes & follow-ups
Common interview mistakes
- Putting the user's question before the long document, so every request has a different prefix.
- Adding the current time to the system prompt and silently disabling the cache.
- Assuming a cache is permanent and not budgeting for misses after idle periods.
Likely interviewer follow-ups
- How would you restructure a prompt to maximise cache hits?
- How do you decide whether caching is worth it?
- How is prompt caching different from caching responses?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Prompt Caching with generative AI interview Q&A and hands-on practice.