الانتقال إلى المحتوى
Claude وأدوات الـ AI للبرمجةPrompt Caching· 7 دقائق للقراءة

Prompt Caching Caches Your Prompt Prefix, Not Claude's Answers

The cache holds the processed start of your prompt. Claude still writes every answer from scratch, so only input gets cheaper.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • Claude API
محتويات المقال

A support team turns on prompt caching for its FAQ assistant and, in the same pull request, deletes the Redis cache of answers: "the API caches now." A week later, three surprises arrive together. The same question asked twice got two differently worded answers, one of them contradicting the help center. The output line of the bill didn't move. And cache_read_input_tokens was 0 on every single request.

The common misconception

"Prompt caching stores Claude's responses, so sending the same question again returns a cached answer." For a web developer, that's the natural reading:

  • In web development, "cache" usually means stored responses: HTTP caches, CDNs, Redis in front of a slow query.
  • The parameter is called cache_control, like the HTTP header that governs response caching.
  • Send the same request twice with caching on and the second one is cheaper and starts answering sooner, which looks exactly like a cache hit on the answer.
  • Most explanations stop at "cached tokens are cheaper" without saying which tokens.

The half-truth: something really is reused, and it really does cut cost and latency. But it's the work of reading your input, not the work of writing the answer.

What actually happens

The cache holds your prompt's prefix

In the docs' words, prompt caching "references the entire prompt: tools, system, and messages (in that order), up to and including the block designated with cache_control." That marked block is the . The cache key is a cryptographic hash of everything before and including it, so only a request whose prefix matches byte for byte can read the entry.

What's kept is the model's processed state for those tokens. The docs' data-retention note says that " representations and cryptographic hashes are held in memory for the cache TTL." On a hit, the API uses that state instead of processing the prefix again, and works only on what comes after it. Then Claude generates the response the usual way, token by token. The docs are explicit: "Prompt caching has no effect on output token generation. The response you receive is identical to what you would get if prompt caching were not used."

So two identical questions produce two fresh generations. With caching or without, they can differ, and neither is "the cached answer."

Proof: a cache warmed with no answer at all

The docs describe pre-warming: send a request with max_tokens: 0 and the API reads your prompt, writes the cache at your breakpoint and returns at once, with an empty content array and zero output tokens billed. No answer exists, yet the next real request reads the cache. That only makes sense if the cache was never about answers.

What you pay, and what you don't

On Claude Opus 5.5, the pricing table lists base input at $4 per million tokens, a 5-minute cache write at $5, a 1-hour cache write at $8 and a cache hit at $0.20. Output stays at $20 per million, cached or not. The response's usage shows the split: cache_read_input_tokens for the prefix read from the cache, cache_creation_input_tokens for the prefix written now, and input_tokens for what comes after the last breakpoint. There's a throughput bonus too: for most models, cache reads don't count toward your input-tokens-per-minute rate limit.

When a prefix doesn't match

Three rules from the docs explain most misses:

  • Writes happen only at your breakpoint. The system then looks back at most 20 blocks for entries earlier requests wrote; it never discovers stable content on its own.
  • Order matters: tools, then system, then messages. Changing a tool definition invalidates the whole cache.
  • Small prompts don't qualify. On Claude Opus 5.5 the minimum cacheable prompt is 512 tokens; shorter ones are processed without caching, and no error is returned.

That first rule is what broke the FAQ bot. Its system prompt began with the current time, and the breakpoint sat on that block, so every request wrote a new entry and no request ever read one:

Python faq.py
import datetimeimport anthropicclient = anthropic.Anthropic()def answer(question: str):    now = datetime.datetime.now(datetime.UTC).isoformat()    reply = client.messages.create(        model="claude-opus-5-5", max_tokens=1024,        system=[{            "type": "text",            "text": f"Current time: {now}\n\n{FAQ_DOCUMENT}",  # new every call            "cache_control": {"type": "ephemeral"},        }],        messages=[{"role": "user", "content": question}],    )    print("cache read:", reply.usage.cache_read_input_tokens)  # always 0answer("How do I reset my password?")answer("Can I change my plan mid-month?")

The second version follows the docs' own advice: keep the system prompt a byte-stable constant and move per-request data into the user message, after the breakpoint. The first call writes the entry; every call within its lifetime reads it. Note what this does not fix: the two answers are still generated fresh.

Short-lived and private

The default entry lives 5 minutes, refreshed for free each time it's read, and the clock starts when the request starts, not when the response ends. A 1-hour lifetime costs twice the base input price to write. There's no way to clear the cache manually, and on the Claude API entries are isolated per workspace, so no other organization can ever read yours.

MythReality
The API caches Claude's answersIt caches the processed prompt prefix; every response is generated fresh
Same question, same cached answerOutput generation is unaffected, so answers can still differ
Caching cuts the whole billIt discounts input only; output costs the same
Any repeated text gets cachedOnly an identical prefix up to a breakpoint, at least 512 tokens on Opus 5.5
Entries stay until you clear them5 minutes by default, refreshed on each hit, and you can't clear them

How to apply it in your project

  1. Separate the two cachesPrompt caching makes reading your prompt cheaper. If you need the same answer for the same question, keep your own response cache, keyed on the normalized question and cleared when the source content changes.
  2. Order the prompt from stable to volatileTools, then the system prompt, then documents, then per-request data. Put the breakpoint on the last block that's identical across requests.
  3. Check the size and the timingMake sure the cached prefix reaches your model's minimum, and use the 1-hour lifetime when requests are more than 5 minutes apart.
  4. Warm before you fan outFor batch jobs and parallel calls, write the cache once and let the rest read it.
  5. Watch the readsLog cache_read_input_tokens per endpoint. A steady 0 means a prefix that changes; diagnostics tells you which part.

Key takeaways

  • Prompt caching stores the processed prefix of your prompt, never Claude's response.
  • Every response is generated fresh, so output costs the same and answers can differ between identical requests.
  • A read needs a byte-identical prefix up to a breakpoint, in the order tools, system, messages.
  • On Claude Opus 5.5 the prefix needs at least 512 tokens, and entries last 5 minutes unless reused or set to 1 hour.
  • If you want reusable answers, build a response cache yourself.

📚 المصادر

  1. Prompt caching — Claude API Docs
  2. Cache diagnostics — Claude API Docs
  3. Pricing — Claude API Docs
  4. Rate limits — Claude API Docs

التعليقات (0)