Context Cache
For repeated system prompts, long document context, or fixed prefixes in multi-turn conversations, enabling context caching can save up to 90% on input costs.
How It Works
1
Mark the Cache
Add a cache_control annotation to a content block
2
First Request
The marked portion is cached, billed at 1.25x the input price
3
Later Requests
Cache hit, billed at 0.1x the input price (90% savings)
Billing Rules
Cache Conditions
- Minimum tokens: explicit caching requires the marked content to be ≥ 1024 tokens (256/512 for some models)
- Cache TTL: ephemeral caches last about 5 minutes, during which requests with the same prefix hit automatically
- Max markers: up to 4
cache_controlmarkers per request - Implicit vs explicit: implicit caching (no markers) is decided automatically with no configuration; explicit caching uses markers to precisely control cache boundaries
- Mutually exclusive: explicit and implicit caching are mutually exclusive in one request; when markers are present, explicit takes precedence
Usage: OpenAI Protocol
In a /v1/chat/completions request, change content to an array and add cache_control to the text blocks you want cached:
Usage: Anthropic Protocol
In a /v1/messages request, similarly add the marker to a content block in system or messages:
Python SDK Example
Supported Models
Response Format
Cache hit information is returned via the usage field, with slightly different formats per protocol:
OpenAI Protocol Response
usage.prompt_tokens_details: cached_tokens: 1804 cache_creation_input_tokens: 0
Anthropic Protocol Response
usage: cache_read_input_tokens: 1804 cache_creation_input_tokens: 0
Best Practices
- Place unchanging long content (system prompt, reference documents, code context) at the start of messages and mark it for caching
- Put user messages last — the cache spans from the start of the messages array to the marker, so changing content placed after it does not affect cache hits
- Good fits: RAG document injection, fixed system prompts in multi-turn chat, agent tool definitions, code repository context
- Poor fits: requests with completely different content each time, or prompts below the minimum length
Chat Completions
Complete OpenAI format reference
Anthropic Messages
Calling via the Anthropic protocol
Model Pricing
View full tiered pricing