Prompt caching
When many requests share the same long prefix — a system prompt, a document, tool definitions, a codebase — that prefix can be cached. Cached input tokens are billed at a fraction of the normal input price.
Claude models
Section titled “Claude models”Mark the end of the reusable prefix with cache_control using the
Anthropic Messages format:
import anthropic, os
client = anthropic.Anthropic(base_url="https://api.wraitherix.com", api_key=os.environ["WRAITHERIX_API_KEY"])
long_document = open("contract.txt").read()
for question in ["Who are the parties?", "What is the termination clause?"]: m = client.messages.create( model="claude-opus-5-5", max_tokens=500, system=[{ "type": "text", "text": long_document, "cache_control": {"type": "ephemeral"}, }], messages=[{"role": "user", "content": question}], ) print(m.usage.cache_creation_input_tokens, m.usage.cache_read_input_tokens)The first call writes the cache (cache_creation_input_tokens > 0); calls
within the next ~5 minutes read it (cache_read_input_tokens > 0):
3637 0 ← first request: prefix written to cache0 3637 ← second request: prefix read from cache| Token type | Price relative to normal input |
|---|---|
| Cache write | ×1.25 |
| Cache read | ×0.1 |
The prefix must be at least ~1,024 tokens to be cacheable.
GPT and Gemini models
Section titled “GPT and Gemini models”Caching is automatic for long prompts: no parameters needed. Cached tokens are
reported in usage.prompt_tokens_details.cached_tokens and billed at the
reduced rate.
Getting the most out of caching
Section titled “Getting the most out of caching”- Put stable content first (system prompt, documents, tools) and the changing part (the user question) last.
- Do not insert timestamps or random IDs into the cached prefix.
- Reuse the cache within its lifetime — bursts of related requests benefit most.