Claude API
How do you implement prompt caching in the Claude API?
QUICK ANSWER
Implement prompt caching by attaching the cache_control metadata block '{"type": "ephemeral"}' to blocks in your prompt (system prompts, tools, or messages) that are longer than 1024 tokens.
Caching Code Example
{
"model": "claude-3-5-sonnet-20241022",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "...very long resource document here...",
"cache_control": {"type": "ephemeral"}
},
{
"type": "text",
"text": "Analyze the document above."
}
]
}
]
}
Verified against: Anthropic API Caching Specifications v1