
Prompt Caching Caches Your Prompt Prefix, Not Claude's Answers
- كاتب موثّق
- Claude API
محتويات المقال
cache_read_input_tokens was 0 on every single request.
The common misconception
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.
What actually happens
The cache holds your prompt's prefix
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.
Proof: a cache warmed with no answer at all
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
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
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, thensystem, thenmessages. 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.
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?")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": FAQ_DOCUMENT, # byte-identical on every call "cache_control": {"type": "ephemeral"}, }], messages=[{"role": "user", "content": f"Now: {now}\n\n{question}"}], ) print("cache read:", reply.usage.cache_read_input_tokens) # 0, then hitsanswer("How do I reset my password?")answer("Can I change my plan mid-month?")Short-lived and private
How to apply it in your project
Separate the two caches Prompt 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. Order the prompt from stable to volatile Tools, then the system prompt, then documents, then per-request data. Put the breakpoint on the last block that's identical across requests. Check the size and the timing Make sure the cached prefix reaches your model's minimum, and use the 1-hour lifetime when requests are more than 5 minutes apart. Warm before you fan out For batch jobs and parallel calls, write the cache once and let the rest read it. Watch the reads Log cache_read_input_tokensper 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.
التعليقات (0)