LearnThatStack Ace your next interview
AI System Design · question
Question 5 of 54

What is prompt (prefix) caching, and why does it matter for cost and latency?

beginner
← All AI System Design questions
Re-explain
Visual ↓

Prompt caching stores the model's internal state for a prompt prefix, its KV cache, so a later request with the same prefix skips recomputing it. The match is prefix-based from the first token, so put stable parts first and the changing user turn last. One edited character early invalidates everything after it.

// Anthropic-style: mark where the stable prefix ends
const req = {
  system: [
    { type: 'text', text: instructions + toolDocs,
      cache_control: { type: 'ephemeral' } }      // cache up to here
  ],
  messages: [{ role: 'user', content: userTurn }] // changes every call
};

Cost falls because cached tokens are read back at a steep discount. Writing the cache can cost more than plain input, so it pays off only on repeats. Providers also set a minimum prefix size, so short prompts never cache.

Latency falls because caching shortens prefill, the pass over the input before the first output token. Time-to-first-token drops, but generation speed is unchanged. Entries usually expire after minutes of inactivity, so repeated, bursty traffic gains most.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for AI System Design One question at a time. Answer out loud, get graded, see what you missed.
See it - interactive diagram
Prefix caching across three calls
const req = {
system: [{
type: 'text',
text: instructions + toolDocs,
// cache up to here
cache_control: {
type: 'ephemeral'
}
}],
messages: [{ role: 'user',
// changes every call
content: userTurn }]
};
Promptmatched from the start
Be brief.Cite facts.search()fetch()Track order
rulestool docsuser turn
Cost and latencyafter each step
cacheempty
prefixcomputed
prefix costfull price
first tokenslow
generationnormal speed
1step

Call 1 sends long rules and tool docs first, the user turn last. With an empty cache, prefill, the pass before any output, computes every token.

1 / 5

Caching matches from the first token, so stable parts go first. Edit one early character and the model recomputes everything from there at full price.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • Your cache hit rate in production is near zero. What would you check first? Something changing early in the prefix, like a timestamp, user ID or reordered tools. Also gaps longer than the TTL, or a prefix under the minimum size.
  • How is prompt caching different from caching the model's full response? Prefix caching skips recomputing the input but still generates a new answer. A response cache skips the call and can serve stale or mismatched answers.
  • In a long multi-turn chat, how do you keep caching useful as the history grows? Append new turns and never edit old ones. Trimming or summarizing early turns changes the prefix from there on and forces a new cache write.
  • When would prompt caching cost you more than it saves? Writes can cost more than plain input, so one-off prompts, gaps longer than the TTL, or a prefix that changes every call pay extra and never get a read.

What you can say

  1. Prompt caching means the provider keeps the model's computed state for a prompt prefix, the KV cache, so the next request with that same prefix skips recomputing it.
  2. The match starts at the first token, so I put stable parts like instructions and tool docs first and the changing user turn last, because one changed character early breaks everything after it.
  3. On cost, the provider bills cached reads at a steep discount, but writing the cache can cost more than normal input, so it only pays off when the prefix repeats.
  4. On latency, it shortens prefill, the pass over the input before the first output token, so time-to-first-token drops while generation speed stays the same.
  5. Entries usually expire after minutes of inactivity, and prompts under the provider's minimum size never cache, so it helps most with repeated, bursty traffic.

Weak answers to avoid

  • Confuses it with response caching Returning a stored answer is a different technique. Prompt caching reuses only the input computation, and the model still generates a fresh answer.
  • Ignores prefix order A timestamp or user name at the top of the system prompt misses the cache on every call. Put stable content first and the changing turn last.
  • Claims it speeds up the whole response Caching shortens prefill, so time-to-first-token drops. Token generation runs at the same speed, so long outputs barely get faster.
  • Calls it free savings on every call Cache writes can cost more than plain input, entries expire, and short prompts never cache. Say it pays off only on prefixes that repeat often.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the AI System Design cheatsheet.

← Back to all AI System Design questions
Pro · $10/mo

47 of 54 AI System Design answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime