Prompt & Context Engineering·Question 22 of 68Intermediate

How does prompt caching work, and how should it change the way you structure prompts?

Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.

Model answer: the part to lead with

Prompt caching lets the provider reuse the computed KV-cache (attention keys/values) for a prompt prefix it has seen before, skipping prefill compute for those tokens. The critical mechanic: caching is exact-prefix-based. The request's token sequence is matched from position 0; the first differing token invalidates everything after it. As of late 2026: Anthropic offers automatic caching through a single top-level cache_control field, or explicit breakpoints, or both. The minimum cacheable prefix ranges from 512 to 4,096 tokens by model, and the newest models have the lowest: 512 on the 5.x generation, 1,024 on Opus 4.8 and Sonnet 5, 4,096 on Opus 4.5 and 4.6. Writes cost 1.25× input for the 5-minute TTL (refreshed on hit) and 2× for 1 hour; reads cost 0.1× on most models, 0.05× on Opus 5.5 and Sonnet 5.5 and 0.025× on Fable 5.1. OpenAI caches implicitly by default for prompts over 1,024 tokens; from GPT-5.6 you can switch to explicit breakpoints, and writes cost 1.25× for the default 30-minute lifetime or 2.5× for 12 hours, with reads at 0.1×, or 0.05× on GPT-6.1 Sol and Terra (GPT-5.5 and earlier have no write fee). Gemini caches implicitly by default; explicit caching exists only on supported Gemini 3 and 2.5 models, and Gemini 4 Argon is implicit only. (Numbers shift with every model generation, so check current pricing; the structure implications don't.)

Full worked answer with diagram

Prompt caching lets the provider reuse the computed KV-cache (attention keys/values) for a prompt prefix it has seen before, skipping prefill compute for those tokens. The critical mechanic: caching is exact-prefix-based. The request's token sequence is matched from position 0; the first differing token invalidates everything after it. As of late 2026: Anthropic offers automatic caching through a single top-level cache_control field, or explicit breakpoints, or both. The minimum cacheable prefix ranges from 512 to 4,096 tokens by model, and the newest models have the lowest: 512 on the 5.x generation, 1,024 on Opus 4.8 and Sonnet 5, 4,096 on Opus 4.5 and 4.6. Writes cost 1.25× input for the 5-minute TTL (refreshed on hit) and 2× for 1 hour; reads cost 0.1× on most models, 0.05× on Opus 5.5 and Sonnet 5.5 and 0.025× on Fable 5.1. OpenAI caches implicitly by default for prompts over 1,024 tokens; from GPT-5.6 you can switch to explicit breakpoints, and writes cost 1.25× for the default 30-minute lifetime or 2.5× for 12 hours, with reads at 0.1×, or 0.05× on GPT-6.1 Sol and Terra (GPT-5.5 and earlier have no write fee). Gemini caches implicitly by default; explicit caching exists only on supported Gemini 3 and 2.5 models, and Gemini 4 Argon is implicit only. (Numbers shift with every model generation, so check current pricing; the structure implications don't.)

Structural consequences - this is the part interviews probe:

  • Order content stable → volatile. System prompt and tool definitions first (identical across all users), then few-shot examples, then conversation history (append-only, so each turn extends the cached prefix), then per-request content last.
  • One volatile token up top kills everything downstream. Interpolating a timestamp, request ID, or user name into the top of the system prompt silently zeroes your cache hit rate. Move volatility down, or quantize it (a date that changes daily rather than a per-request timestamp).
  • Append, don't mutate. In agent loops, never rewrite earlier history in place (e.g., re-summarising turn 3 mid-conversation) - that's a full cache invalidation. Compaction should happen at deliberate breakpoints where you accept paying the re-prefill cost.
  • Place cache breakpoints at stable/volatile boundaries: end of tools+system, end of few-shot block, end of history.
  • Nondeterministic serialization is a silent killer - dict ordering, float formatting, or a library that reorders JSON keys produces byte-different prompts that never hit cache.

Why it matters so much for agents: an agent loop re-sends the entire growing context every step; with caching, each step pays full price only for the newly appended tokens. That's routinely a 5-10× cost reduction and a large TTFT (prefill latency) win. Cache hit rate belongs on your dashboard next to cost and latency.

Worth sketching. The lower branch is the one that wins the question: it shows exactly what a single injected timestamp costs you.

flowchart LR
    A["System prompt"] --> B["Tool schemas"]
    B --> C["Few-shot block"]
    C --> D["History, append only"]
    D --> E["Per-request query"]
    D --> F["Breakpoint: reuse the KV cache,<br/>prefill only new tokens"]
    G["One volatile token<br/>anywhere in the prefix"] --> H["Everything after it<br/>re-prefills at full price"]

What a good answer covers

  • Prompt caching lets the provider reuse the computed KV-cache (attention keys/values) for a prompt prefix…
  • The critical mechanic: caching is exact-prefix-based
  • The request's token sequence is matched from position 0; the first differing token invalidates…
  • As of late 2026: Anthropic offers automatic caching through a single top-level cache_control field, or…
  • The minimum cacheable prefix ranges from 512 to 4,096 tokens by model, and the newest models have the lowest

Key terms: exact-prefix-based · Order content stable → volatile · Append, don't mutate

Interviewer follow-ups to expect

  1. Dynamic few-shot retrieval versus static examples - how does caching change that calculus?

  2. Where would you place breakpoints in a multi-tenant agent with shared tools but per-user memory?

  3. Your cache hit rate dropped to near zero after a deploy - what do you look for?

Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep