Prompt caching is the most underused cost optimization technique for Claude API users. It can reduce costs by 90% for workloads that reuse the same context repeatedly. Here's how to implement it and when it actually helps.
What is Prompt Caching?
When you call the Claude API, you send input tokens (your prompt) and receive output tokens (Claude's response). Input tokens normally cost $3.00 per million tokens for Claude Sonnet.
Prompt caching lets you mark parts of your prompt (usually large, static context like system instructions or documentation) to be cached on Anthropic's servers. The first time you send it, you pay full price. Every subsequent request that reuses the cached content pays only $0.30 per million tokens — a 90% discount.
When Prompt Caching Delivers Big Savings
Prompt caching works best when:
- You send the same large context repeatedly (product docs, codebase, company knowledge base)
- The cached portion is significantly larger than the variable portion
- You're making high-volume API calls (hundreds or thousands per day)
Example: Customer Support Bot
Every user question includes your full product documentation (50,000 tokens) plus the user's specific question (500 tokens). Without caching:
- Input cost: 50,500 tokens per request
- At $3.00/million tokens = $0.1515 per request
- 1,000 requests/day = $151.50/day
With prompt caching:
- First request: 50,500 tokens at full price = $0.1515
- Subsequent requests: 50,000 cached tokens at $0.30/million + 500 new tokens at $3.00/million = $0.0165 per request
- 1,000 requests/day ≈ $16.50/day
Savings: ~89% reduction in input costs.
How to Implement Prompt Caching
Anthropic's API supports prompt caching via the cache_control parameter. Mark the content you want cached, and the API handles the rest.
Example Implementation (Python)
import anthropic
client = anthropic.Anthropic(api_key="your_api_key")
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
system=[
{
"type": "text",
"text": "You are a customer support assistant. Use the product documentation below to answer questions.",
},
{
"type": "text",
"text": "<your 50,000 token product documentation here>",
"cache_control": {"type": "ephemeral"}
}
],
messages=[
{"role": "user", "content": "How do I reset my password?"}
]
)The cache_control parameter tells the API to cache the product documentation. Every subsequent request that includes the same cached text pays the reduced rate.
What Gets Cached and For How Long
- Cache duration: 5 minutes of inactivity. If you don't make another request within 5 minutes, the cache expires and you pay full price on the next call.
- Cache key: The exact content of the cached block. If you change even one character, it's treated as new content and cached separately.
- Max cached content: Up to 200,000 tokens can be cached per request (across all cached blocks).
When Prompt Caching Doesn\'t Help
Prompt caching only saves money if you reuse the same content multiple times within the 5-minute window. It doesn't help when:
- Your prompts are always unique (no repeated content)
- The cached portion is small relative to the variable portion
- Your request volume is low (fewer than ~10 requests per 5-minute window)
- You're frequently updating the "cached" content (invalidates the cache)
Other Cost Optimization Strategies
1. Use Smaller Models When Possible
Claude Haiku costs $0.25 per million input tokens (vs $3.00 for Sonnet). For simple tasks like classification, extraction, or basic Q&A, Haiku is often enough.
2. Reduce Output Length
Output tokens cost $15.00 per million for Sonnet. Set max_tokens to the minimum you need. If you only need a yes/no answer, don't let the model write three paragraphs.
3. Batch Requests
Anthropic offers a Message Batches API with a 50% discount on input and output tokens. Requests are processed asynchronously (within 24 hours). Use it for non-urgent workloads like data processing or report generation.
4. Use Tool Calling Instead of Freeform Responses
When you need structured output (JSON, function calls), use Claude's tool calling feature. It's more reliable than parsing freeform text and often produces shorter responses.
Measuring ROI
Before optimizing, measure your current costs:
- Check the Anthropic console for total tokens used (input and output)
- Calculate cost per request: (input_tokens × input_price) + (output_tokens × output_price)
- Identify which requests send large, repeated context
- Estimate savings from caching those requests
If prompt caching can save 50% or more on input costs, it's worth implementing.
Need Help Optimizing Claude API Costs?
Ez IT Expert helps businesses audit their Claude API usage and implement cost optimizations. We analyze your prompts, identify caching opportunities, and recommend model/architecture changes that reduce costs without sacrificing quality.
Get a Free API Cost Audit →