Safa API · aisafa.xyz

Prompt Caching Math: How Much You Actually Save on Claude API Costs in 2026

发布于 2026-08-05 · Safa API

Prompt caching cuts Claude API input costs by 90% for repeated context — but most developers misunderstand when it actually saves money and how much they'll save in practice.

This guide runs the real cost math with 2026 pricing, shows which use cases benefit, and explains how to structure prompts so Anthropic's cache recognizes your repeated blocks. If you're hitting budget limits on long-context Claude calls, you need this breakdown.

What Is Prompt Caching and How It Works

Anthropic's prompt caching stores a prefix of your prompt server-side. When your next request reuses that same prefix, you pay a 90% discount on those cached input tokens instead of the full input rate.

The key constraint: only the prefix qualifies. Claude caches from the start of your prompt up to a cache breakpoint you mark, and that cached section must stay exactly identical across requests. Change one character in the cached block and the cache misses.

A cache hit costs 10% of the normal input rate. A write (the first time a block is cached) costs 25% extra. So you break even after four reuses and profit from the fifth onward.

2026 Pricing: The Real Numbers

Here's Anthropic's August 2026 pricing for Claude Sonnet 5, the current default model:

Token Type Cost per Million Tokens
Input (standard) $3.00
Cache write (first use) $3.75
Cache hit (reuse) $0.30
Output $15.00

For Claude Opus 5, input is $5 per million, cache write is $6.25, and cache hit is $0.50. Output remains $25 per million.

Cost Math: When Do You Break Even?

Let's walk the math for a 50,000-token system prompt reused across API calls.

Without caching:
Each call pays full input rate: 50,000 tokens × $3.00 / 1M = $0.15 per call.
Ten calls = $1.50 total.

With caching:
First call (cache write): 50,000 × $3.75 / 1M = $0.1875
Next nine calls (cache hit): 50,000 × $0.30 / 1M = $0.015 each
Total: $0.1875 + (9 × $0.015) = $0.1875 + $0.135 = $0.3225 for ten calls.

You save $1.50 – $0.32 = $1.18 across ten reuses, or 78.5% of the baseline cost. Per-call average drops to $0.032 after the write, compared to $0.15 without caching — that's 79% cheaper per reuse.

The break-even point is call number five. Calls one through four pay back the cache-write premium; call five onward is pure savings.

Use Cases That Benefit Most

Prompt caching shines when:

Caching doesn't help when your prompt changes every call or when your context is short enough that the savings don't justify the integration effort.

How to Structure Prompts for Caching

Anthropic's API requires you to mark cache breakpoints with a cache_control field on individual content blocks. Only blocks at the end of your messages array can be cached, and they must stay exactly identical across calls.

Here's a working Python example with the official SDK:

from anthropic import Anthropic

client = Anthropic(api_key="your_key_here")

response = client.messages.create(
    model="[REDACTED]",
    max_tokens=1024,
    system=[
        {
            "type": "text",
            "text": "You are an expert Python tutor.",
        },
        {
            "type": "text",
            "text": "Here are 50 example Q&A pairs: [your few-shot examples]",
            "cache_control": {"type": "ephemeral"}
        }
    ],
    messages=[
        {"role": "user", "content": "How do I reverse a list?"}
    ]
)

The second system block — the one with few-shot examples — will be cached. The first block and the user message pay standard input rates. On the next call, if you keep the cached block identical, that portion costs 90% less.

Cache lifetime is five minutes of inactivity. If you don't reuse the cached block within five minutes, it expires and the next call writes a fresh cache.

Common Mistakes That Break Caching

Changing the cached section. Adding a single character, reordering lines, or inserting a timestamp into the cached block invalidates the cache. Structure your prompt so the static parts (system instructions, few-shot examples, document content) come first and the dynamic parts (current user question, latest message) come after the cache breakpoint.

Not marking the breakpoint. Anthropic won't cache anything unless you explicitly add cache_control to a content block. Without it, every token pays full price.

Caching the wrong blocks. If the block changes every call, caching it costs you 25% extra on the write and delivers zero reuse savings.

Real Savings Example: SillyTavern Roleplay

A typical SillyTavern session with a detailed character card resends 30,000 tokens of card + chat history every message. Without caching:

With caching:

You save $9.00 – $1.00 = $8.00 on input alone — an 89% reduction. If your backend supports prompt caching, you'd be leaving money on the table by not using it.

Frequently Asked Questions

Does prompt caching work with all Claude models?

Yes. Claude Opus 5, Sonnet 5, and Haiku 4 all support prompt caching as of August 2026. Older models (Opus 4.8, Sonnet 4) do not.

How long does a cache last?

Five minutes from the last reuse. If you don't send another request with the same cached prefix within five minutes, the cache expires and your next call writes a fresh one.

Can I cache the user message or assistant reply?

No. Only system and user message content can be cached, and the cacheable blocks must appear at the end of your messages array, before the final user turn. Output tokens are never cached.

Does my API provider support prompt caching?

Official Anthropic API supports it. Third-party relays vary — some pass through cache_control, others strip it. If you're using a relay for lower pricing or no-credit-card access, confirm caching support before relying on these savings.

Lower Pricing + Prompt Caching: Safa API

If you want both prompt caching and cheaper baseline rates, Safa API is worth checking. It's an OpenAI-compatible relay that supports Claude, GPT, and Gemini through one endpoint, passes through Anthropic's cache_control for full caching support, and charges lower per-token rates than official APIs.

Key advantages for developers outside the US:

Pricing and signup at aisafa.xyz/pricing.

Conclusion: When to Use Prompt Caching

Prompt caching cuts Claude API input costs by up to 90%, but only when you're reusing large static blocks across multiple calls. If your use case matches — long system prompts, repeated documents, few-shot examples, or high-message-count roleplay — the savings are real and the integration is straightforward.

Run the math with your actual token counts and call frequency. If you're crossing the break-even threshold (roughly five reuses), turn caching on. If you're making one-off calls or your context changes every time, standard input pricing is simpler and cheaper.

And if you're already optimizing costs, pair prompt caching with a relay that offers lower baseline rates and flexible payment — you'll stack both discounts and cut your bill further.

立即开始使用 Safa API API 中转

官方直连 · 一个接口接入 Claude / GPT / Gemini · 7×24 稳定

免费注册试用 →