import anthropic client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from the environment # Must clear the model's minimum cacheable length or nothing is cached at all. LONG_INSTRUCTIONS = "You are a precise technical editor. " * 400 def ask(question: str): return client.messages.create( model="claude-sonnet-4-6", max_tokens=512, system=[ { "type": "text", "text": LONG_INSTRUCTIONS, "cache_control": {"type": "ephemeral"}, # everything above is cacheable } ], messages=[{"role": "user", "content": question}], ) for question in ["Summarize section 3.", "Now rewrite it for a beginner."]: resp = ask(question) u = resp.usage print( f"write={u.cache_creation_input_tokens} " f"read={u.cache_read_input_tokens} " f"uncached={u.input_tokens}" ) # Output: "write=2823 read=0 uncached=14" "write=0 read=2823 uncached=17"