Claude prompt caching
This page explains how to enable prompt caching for Claude and confirm cache hits. For model IDs and endpoints, see the Claude family overview.
Key pointClaude caching requires an explicit cache_control marker; neither endpoint caches automatically.
Put long, fixed content (system prompts, documents, tool definitions) at the start of the request, and set cache_control on the last fixed content block. Subsequent requests whose prefix is exactly identical will hit the cache.
Anthropic-native API
claude_cache.py
import os
import anthropic
client = anthropic.Anthropic(base_url="https://api.ofox.io/anthropic", api_key=os.environ["OFOX_API_KEY"])
LONG_DOC = open("manual.txt", encoding="utf-8").read() # a long, fixed document
for question in ["What does chapter 3 cover?", "Summarize the key points of the whole document."]:
message = client.messages.create(
model="anthropic/claude-sonnet-4.6",
max_tokens=1024,
system=[
{"type": "text", "text": "Answer questions based on the document below."},
{"type": "text", "text": LONG_DOC, "cache_control": {"type": "ephemeral"}},
],
messages=[{"role": "user", "content": question}],
)
u = message.usage
print("Write:", u.cache_creation_input_tokens, "Read:", u.cache_read_input_tokens)For general information on caching, see Prompt caching.
For other errors, see Common errors.
Last updated on