Skip to main content

How to Cache a Long System Prompt with Claude Prompt Caching in Python

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Many apps send Claude the same long instructions with every question: rules, a handbook, a set of examples. Prompt caching stores that unchanging start of the request, so later calls read it from the cache for a small part of the normal price, and faster. This guide caches a system prompt and proves it with the usage numbers, with real output from Claude Opus 5.5.

Syntax​

Syntax
message = client.messages.create(
model="<model ID>",
max_tokens=<limit>,
system=[{"type": "text", "text": <long text>, "cache_control": {"type": "ephemeral"}}],
messages=[...],
)
PartWhat it means
system=[{...}]The system prompt as a list of blocks, so a block can carry cache_control
"cache_control": {"type": "ephemeral"}Caches everything up to and including this block
usage.cache_creation_input_tokensTokens written to the cache by this call
usage.cache_read_input_tokensTokens read from the cache by this call

Anthropic's prompt caching documentation: "By default, the cache has a 5-minute lifetime. The cache is refreshed for no additional cost each time the cached content is used." The minimum cacheable length is 512 tokens for Claude Opus 5.5, and "Shorter prompts cannot be cached, even if marked with cache_control."

What It Costs​

On Claude Opus 5.5, normal input costs $4 per million tokens, writing to the 5-minute cache $5, and reading from it $0.20, one twentieth of the normal price (Anthropic's pricing). The first call pays a little extra to write; every call after it within five minutes saves.

Prerequisites​

  • A Claude API key, in a .env file as ANTHROPIC_API_KEY=....
  • The Anthropic Python SDK in a uv project. This guide uses anthropic 2.14.0, added with uv add anthropic, on Python 3.15.2.

Step 1: Save the Long Instructions​

The example uses the rules of a made-up library, 39 short rules in library_rules.txt, saved next to the script. Its start:

library_rules.txt (start)
You answer questions from members of the Maple Street Library. Use only these rules, and keep answers to one or two sentences.

Membership
1. Membership is free for anyone who lives, works or studies in the city.
2. To join, a person brings a photo ID and a document with their current address.

Any text over 512 tokens works; this one is 524 words.

Step 2: Write the Script​

cache_prompt.py
from pathlib import Path

import anthropic

client = anthropic.Anthropic()
rules = Path("library_rules.txt").read_text(encoding="utf-8")

for question in ["How long can I keep a DVD?", "Can I bring my dog?"]:
message = client.messages.create(
model="claude-opus-5-5",
max_tokens=1024,
system=[{"type": "text", "text": rules, "cache_control": {"type": "ephemeral"}}],
messages=[{"role": "user", "content": question}],
)
usage = message.usage
print(question, message.content[-1].text)
print(f" cache write {usage.cache_creation_input_tokens}, cache read {usage.cache_read_input_tokens}, other input {usage.input_tokens}")

The cache_prompt.py script in a macOS-style code window: the library rules are read from a file and sent as a system block with cache_control ephemeral, for two questions in a loop, printing each answer and its cache write, cache read and other input tokens.

Both calls send exactly the same system block; only the question changes.

Step 3: Run It​

Terminal
uv run --env-file .env cache_prompt.py
Output
How long can I keep a DVD? You can keep a DVD for 7 days. You can renew it twice, online or at the desk, unless another member has reserved it.
cache write 1018, cache read 0, other input 17
Can I bring my dog? Dogs aren't allowed in the library, but assistance dogs are welcome.
cache write 0, cache read 1018, other input 15

The output in a terminal: the DVD answer with cache write 1018, cache read 0 and other input 17, then the dog answer with cache write 0, cache read 1018 and other input 15.

The first call wrote the 1,018-token system prompt to the cache; the second read it back. input_tokens counts only the rest, the question. Those 1,018 tokens cost about $0.0051 to write and $0.0002 to read, against $0.0041 each time without the cache.

Common Mistakes​

MistakeWhat happensDo this instead
Caching fewer than 512 tokens on Claude Opus 5.5Nothing is cached, and no error says so: both cache counts stay 0Cache only long, unchanging text
Changing the cached text between calls, even by one characterThe cache misses and is written againKeep the cached part identical; put changing parts after it
Calls more than five minutes apartThe cache has expired, and the next call writes it againUse the 1-hour cache for slower traffic

Troubleshooting​

SymptomLikely causeFix
cache_read_input_tokens stays 0 on the second callThe text is under the minimum, or changedCheck both counts and compare the cached text

FAQ​

Do I need a beta header?​

No. Prompt caching works on the normal Messages API.

What else can be cached?​

Tools and earlier conversation turns too: everything before the block that carries cache_control, in the order tools, system, messages.

Conclusion​

With cache_control on the system block, the first call wrote 1,018 tokens to the cache and the second read them back, so only the question was charged at the full input price.

Additional Resources​

  • Prompt caching — lifetimes, minimums, what can be cached and what invalidates it.
  • Pricing — cache write and read prices per model.