Claude API Prompt Caching: Complete Cost Guide for 2026
If you're calling Claude API frequently with the same context — like a chatbot system prompt, a large code repository, or reference documentation — prompt caching can cut your input token costs by 70% to 90%. But most developers still don't know how it works, what it costs, or how to structure prompts to activate it.
This 2026 guide explains Claude's prompt caching mechanism, shows real pricing math, and walks through the setup you need to actually see savings.
What Is Claude Prompt Caching?
Anthropic's prompt caching lets you mark part of your input — typically a system message, a long code base, or retrieval context — as cacheable. When you send the same text across multiple API calls within a short time window, Claude skips re-processing those tokens and charges you a much lower "cache read" rate instead of the full input rate.
Cache hits stay valid for about 5 minutes of inactivity. If you're building a chat application or running repeated agent loops, most of your calls will hit the cache.
2026 Claude API Pricing With Prompt Caching
Here's what Anthropic charges per million tokens as of August 2026:
| Model | Input (full) | Cache Write | Cache Read | Output |
|---|---|---|---|---|
| Claude Opus 4.8 | $15 | $18.75 | $1.50 | $75 |
| Claude Sonnet 5 | $3 | $3.75 | $0.30 | $15 |
| Claude Haiku 4 | $0.80 | $1.00 | $0.08 | $4 |
Cache write is what you pay the first time Claude sees a cacheable block — it's 25% more than the standard input rate because Anthropic stores it. Cache read is what you pay on subsequent calls when that block is still in cache — 90% cheaper than the full input rate.
Real Savings Example
Suppose you're building a coding assistant with a 10,000-token system prompt and code context, calling Claude Sonnet 5 fifty times in one session:
- Without caching: 50 calls × 10,000 tokens × $3/M = $1.50
- With caching: (1 write × 10k × $3.75/M) + (49 reads × 10k × $0.30/M) = $0.0375 + $0.147 = $0.18
That's an 88% reduction in input costs for the cached portion.
How to Enable Prompt Caching in Your Code
You control caching by marking messages or content blocks with a cache_control parameter. Only content before the marker gets cached — typically your system message or large reference material.
Python SDK Example
import anthropic
client = anthropic.Anthropic(api_key="your-key")
response = client.messages.create(
model="[REDACTED]",
max_tokens=1024,
system=[
{
"type": "text",
"text": "You are a helpful coding assistant...",
"cache_control": {"type": "ephemeral"}
}
],
messages=[
{"role": "user", "content": "Explain this function..."}
]
)
print(response.usage) # shows cache_creation_input_tokens or cache_read_input_tokens
The first call creates the cache. Every call within ~5 minutes reuses it at the cheaper rate.
Cursor, Claude Code, and Other Tools
Most AI coding tools that use the official Anthropic SDK automatically benefit from prompt caching when you use a custom API endpoint that supports it. If you're routing through a relay, make sure it forwards the cache_control field and returns cache usage stats.
Prompt Caching Gotchas
- Minimum size: Cacheable blocks must be at least 1,024 tokens (roughly 750 words). Smaller prompts don't qualify.
- Exact match only: Changing even one character in your cached content invalidates the cache. Use stable, parameterized templates.
- TTL is ~5 minutes: If you wait longer between calls, the cache expires and you pay the write cost again.
- Only works on newer models: Claude Opus 4.8, Sonnet 5, and Haiku 4 support caching. Older models ignore the parameter.
Combining Caching With a Cheaper Relay
Official Anthropic API pricing is already competitive, but if you're outside the US and don't have a credit card on file, or you want to further reduce costs, a third-party relay can help.
Safa API is one option that supports prompt caching, offers lower per-token pricing, and accepts Alipay. It provides a single OpenAI-compatible endpoint for Claude, GPT, and Gemini, so you can switch models without changing your code. No US credit card needed — just top up with Alipay or WeChat Pay and start calling.
Prompt caching still works the same way through the relay: mark your system message with cache_control, and Safa forwards it to Anthropic's backend. You get the same 90% cache-read discount, but at a lower base rate.
When Prompt Caching Actually Saves Money
Caching delivers the biggest wins when:
- You have a large, static context (system prompt, code repository, knowledge base) that stays the same across many calls
- You're making repeated calls in quick succession (chat sessions, agent loops, batch evaluations)
- The cached portion is much larger than the varying user input
If every call has a completely different input, or you're only calling the API once every few minutes, caching won't help much.
Common Questions
Does caching work with streaming responses?
Yes. Cache behavior is controlled on the input side. Whether you use stream=True or wait for the full response doesn't affect cache hits.
Can I cache user messages or code files?
You can cache any message or content block, but only content before the cache_control marker gets cached. Typically you cache the system message and large static context, not the per-turn user input.
How do I know if caching is working?
Check the usage object in the API response. On the first call you'll see cache_creation_input_tokens. On subsequent calls, you'll see cache_read_input_tokens and much lower billed input.
What happens when the cache expires?
After about 5 minutes of inactivity, the next call pays the cache-write cost again. For long-running applications, this still averages out to big savings because most calls happen in bursts.
2026 Recommendation
If you're calling Claude API with any kind of repeated context — chatbot system prompts, RAG pipelines, coding assistants, or agent frameworks — turn on prompt caching. The setup is one extra parameter, and the savings are immediate.
For developers who want the lowest possible cost and don't have a US credit card, using a relay like Safa API cuts the base rate further while keeping full caching support. You get one unified endpoint for Claude, GPT, and Gemini, Alipay payment, and no geographic restrictions.
Visit aisafa.xyz to try it, or check aisafa.xyz/pricing for the latest per-model rates.
官方直连 · 一个接口接入 Claude / GPT / Gemini · 7×24 稳定
免费注册试用 →