Skip to content
Academy ofAI Learning

~/academy $ claude learn prompt-caching

Prompt caching: pay less for repeated context

How Claude API prompt caching works, how to use cache_control, where to put cache breakpoints and how much it saves at current prices. 8 min

Watch it done: cache a long prompt and see the saving

$

Step 1 of 5

Start from the same setup as your first API call: the SDK installed in a virtual environment and your own key in this variable.

  1. Start from the same setup as your first API call: the SDK installed in a virtual environment and your own key in this variable. Type: export ANTHROPIC_API_KEY="your-api-key-here".
  2. Save a long text in the same folder as handbook.txt, for example a few pages of a made-up staff handbook. Caching only pays off for long, repeated content. on Claude Opus 5.5 a prefix under 512 tokens is not cached, and the API gives no error.
  3. Create cache_demo.py. The cache_control marker on the handbook block is the breakpoint: everything up to it is cached for 5 minutes. The question comes after it, because it changes every time. Create cache_demo.py containing: import sys import anthropic client = anthropic.Anthropic() handbook = open("handbook.txt", encoding="utf-8").read() response = client.messages.create( model="claude-opus-5-5", max_tokens=1024, system=[ {"type": "text", "text": "You answer staff questions using the handbook below."}, {"type": "text", "text": handbook, "cache_control": {"type": "ephemeral"}}, ], messages=[{"role": "user", "content": sys.argv[1]}], ) print(response.usage.model_dump_json())
  4. The first request writes the handbook to the cache. A 5-minute cache write costs 1.25 times the normal input price. Type: python cache_demo.py "How many days of annual leave do new starters get?". the usage shows cache_creation_input_tokens of roughly the size of your handbook, and cache_read_input_tokens of 0.
  5. Ask a different question. The handbook is read from the cache at a fraction of the input price (0.05 times on Opus 5.5), and each read keeps the cache alive for another 5 minutes. Type: python cache_demo.py "How much notice do I need to give before a holiday?". run within 5 minutes, cache_read_input_tokens now shows the handbook tokens and cache_creation_input_tokens is 0.

Code from the official Claude API prompt caching docs, checked 18 November 2026. Lines marked Note describe what happens and are not real output.

Step 1 of 6

$ Read

What it saves

Many apps send the same long start to every request: a system prompt, tool definitions, a handbook or a codebase. Prompt caching lets the API reuse that processed prefix, so later requests are cheaper and faster.

Prices are multipliers on the base input price (checked 18 November 2026). Writing to the 5-minute cache costs 1.25x, writing to the 1-hour cache 2x, and a cache read 0.1x on most models. Opus 5.5 and Sonnet 5.5 read at 0.05x: $0.20 and $0.10 per million tokens. Haiku 5.5 reads at $0.01. A 5-minute cache pays for itself after one read; a 1-hour cache after two.

Cached reads also do not count towards your input tokens per minute rate limit, so caching raises your real throughput too.

Lesson notes: Prompt caching: pay less for repeated context

Everything in this lesson as text, for revision or to read without the interactive steps. By the end you will be able to: You cache the stable part of your prompts, confirm hits in the usage fields and cut the cost of repeated context.

  1. Step 1 of 6 · Read

    What it saves

    Many apps send the same long start to every request: a system prompt, tool definitions, a handbook or a codebase. Prompt caching lets the API reuse that processed prefix, so later requests are cheaper and faster.

    Prices are multipliers on the base input price (checked 18 November 2026). Writing to the 5-minute cache costs 1.25x, writing to the 1-hour cache 2x, and a cache read 0.1x on most models. Opus 5.5 and Sonnet 5.5 read at 0.05x: $0.20 and $0.10 per million tokens. Haiku 5.5 reads at $0.01. A 5-minute cache pays for itself after one read; a 1-hour cache after two.

    Cached reads also do not count towards your input tokens per minute rate limit, so caching raises your real throughput too.

  2. Step 2 of 6 · Read

    Turn it on

    Automatic caching is the recommended start: add one top-level cache_control field and the API places the breakpoint on the last cacheable block, moving it forward as a conversation grows.

    Explicit breakpoints put cache_control on a specific block, such as the end of a long document in the system prompt. You can use up to four breakpoints in a request, and automatic caching uses one of them.

    Automatic
    response = client.messages.create(
        model="claude-opus-5-5",
        max_tokens=1024,
        cache_control={"type": "ephemeral"},
        system=HANDBOOK_TEXT,  # the same long text on every request
        messages=[{"role": "user", "content": "How many days of annual leave do new starters get?"}],
    )
    Explicit breakpoint
    response = client.messages.create(
        model="claude-opus-5-5",
        max_tokens=1024,
        system=[
            {"type": "text", "text": "You answer staff questions using the handbook below."},
            {"type": "text", "text": HANDBOOK_TEXT, "cache_control": {"type": "ephemeral"}},
        ],
        messages=[{"role": "user", "content": question}],
    )
  3. Step 3 of 6 · Read

    Where breakpoints go

    The cache is a prefix match in a fixed order: tools, then system, then messages. Any change to anything before the breakpoint means a miss. So put stable content first and anything that changes per request (the time, a user ID, the new question) after the last breakpoint.

    Changing tool definitions invalidates everything. Changing the system prompt invalidates the system and message levels. Changing tool_choice, images, the thinking settings or effort invalidates cached messages. The prefix must also be long enough: 512 tokens on Opus 5.5, Sonnet 5.5 and Haiku 5.5. Shorter prefixes are simply not cached, with no error.

    The default lifetime is 5 minutes, refreshed free each time the cache is read. For traffic that arrives less often, or for batches, use {"type": "ephemeral", "ttl": "1h"}.

  4. Step 4 of 6 · Read

    Check it worked, and the maths

    usage shows three input numbers: cache_creation_input_tokens (written), cache_read_input_tokens (read) and input_tokens (after the last breakpoint, not cached). Total input is all three added together. If both cache fields stay at 0 on repeated requests, something in your prefix is changing, or it is too short.

    Example on Opus 5.5: a 50,000-token handbook and 100 questions, each within 5 minutes of the last. Without caching, the handbook alone costs 5M input tokens, about $20. With caching, one write (50,000 tokens at $5 per million, $0.25) plus 99 reads (4.95M tokens at $0.20 per million, $0.99) comes to about $1.24, before the questions and answers.

  5. Step 5 of 6 · Quiz

    Check your understanding

    `cache_read_input_tokens` is always 0, even for identical questions a minute apart. Your system prompt begins "Today is 18 November 2026, 14:02:11." What is wrong?

    • Caching only works with batches.
    • You need a beta header.
    • Opus 5.5 does not support caching.
    • The timestamp changes every request, so the prefix never matches. Move it after the cached content.
    Show the answer

    The timestamp changes every request, so the prefix never matches. Move it after the cached content. Any byte change before the breakpoint is a miss. Volatile details belong after the last breakpoint.

  6. Step 6 of 6 · Sort

    Before the breakpoint or after?

    Sort each part of the request.

    • Tool definitions that never change
    • A 40-page policy handbook
    • The current date and time
    • The user's new question
    • The fixed role and rules of your assistant
    • A per-request customer ID
    Show the answers
    • Tool definitions that never change: Cache it (before the breakpoint). Tools come first in the prefix. Keep them stable and in a fixed order.
    • A 40-page policy handbook: Cache it (before the breakpoint). Large, reused and stable: the ideal thing to cache.
    • The current date and time: Keep it after the breakpoint. It changes every request and would break every cache hit.
    • The user's new question: Keep it after the breakpoint. Different each time, so it goes last.
    • The fixed role and rules of your assistant: Cache it (before the breakpoint). Stable system text belongs in the cached prefix.
    • A per-request customer ID: Keep it after the breakpoint. Varies per request. Put it after the breakpoint.