AI-ENGINEERING
    intermediate

    Prompt Caching

    AI Engineer path · lesson 60 of 124
    The idea
    Prompt caching is like keeping a popular book open to the right page on a bookstand. Instead of finding the book and flipping to page 200 every single time a customer asks a question about page 201, you just read from where you left off — saving massive amounts of time and effort.
    How to use
    Toggle caching on and off, fire multiple API calls, and watch the per-call cost drop after the first cache write. Toggle individual prompt blocks to see how reducing the cacheable prefix shrinks your savings.

    Toggle prompt blocks on/off, enable caching, and fire API calls. Watch how costs change when the stable prefix is cached.

    Prompt structure
    System
    Tool
    Few-shot
    RAG
    Conversation
    User
    ← cacheable prefix (2600 tok) →← dynamic suffix (3500 tok) →
    Call #0
    6,100
    total tokens
    2,600 tok
    cacheable
    —
    cost this call
    —
    savings

    Key insight

    The first call with caching is slightly more expensive (cache write at 1.25×), but every subsequent call reads cached tokens at 0.10× — a 90 % discount. After just 2 calls, you break even; by call 10, you've saved significantly. Toggle caching on, fire 3+ calls, and watch the per-call cost drop.

    Lay out a cacheable prompt

    Sequence

    Order the prompt so the cache actually hits.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    1. 1Drop a step here
    2. 2Drop a step here
    3. 3Drop a step here
    4. 4Drop a step here
    5. 5Drop a step here
    5 pieces left to place

    Test your knowledge

    20 questions
    Question 1 of 20
    Score: 0

    What does prompt caching actually store on the server?

    The short version

    Prompt caching keeps a popular book open on a bookstand. Instead of pulling it off the shelf and flipping to page 200 every time a customer asks about page 201, you read from where you left off — nearly free.

    In more depth

    Prompt caching

    Providers cache the KV-cache (attention states) for a prompt's prefix. On a subsequent request whose prefix matches exactly, the model reuses those states instead of paying the prefill compute again.

    The golden rule

    Put stable content first and dynamic content last. Caches are keyed by exact byte sequence — a single whitespace change in the prefix invalidates every token after it.

    Provider differences

    Anthropic: explicit cache_control breakpoints. Write ~1.25×, read ~0.10× (90% discount). ~5-min ephemeral TTL.
    OpenAI: automatic prefix detection; read ~0.50× of input price.
    Google (Gemini): explicit cachedContent API, minimum ~32K tokens, configurable TTL, best fit for large stable corpora.

    What breaks the cache

    A new timestamp or user id at the top of the prompt
    Reordering tool definitions between calls
    Changing the model version or the sampling parameters that alter tokenization
    Anthropic: exceeding the TTL under low QPS

    Composes with everything else

    Caching is cheapest first-line optimization. Compose with routing, semantic caching (paraphrase-level), and prompt compression on the dynamic tail.

    Interview Questions

    Anthropic
    OpenAI
    Google
    Anthropic
    Databricks
    OpenAI
    Amazon
    Anthropic
    OpenAI
    Anthropic
    Databricks
    Anthropic
    Google
    Cohere

    Love this visualization?

    Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.

    100% free • No credit card required