Prompt caching explained: how to cut your AI API bill without changing models

Build Guides

Prompt caching explained: how to cut your AI API bill without changing models

Wall of small wooden card-catalogue drawers with brass label holders
Photo by cottonbro studio on Pexels

Prompt caching lets you pay a fraction of the price for input the model has seen before. Here's how it works, what it costs on Claude, Gemini, GPT-6 and MiniMax, and how to structure prompts so the cache actually hits.

In this article
  1. The idea in one paragraph
  2. What it costs on Claude
  3. Other providers
  4. The one rule: stable stuff first
  5. How to turn it on (Claude)
  6. Check that it's working
  7. One more cost trap: tokenizers
  8. Where you're already using it

If you run a coding agent or a chatbot on an API, most of what you pay for is input you've already sent. Every turn resends the same system prompt, tool definitions and project docs. Prompt caching is how you stop paying full price for that.

The idea in one paragraph

The provider keeps the processed version of the start of your prompt for a few minutes. If the next request begins with exactly the same content, it reuses that work and bills you a fraction of the normal input price. Only the new part at the end is billed in full.

What it costs on Claude

From Anthropic's pricing and caching docs:

Multiplier on base input price
Cache write, 5-minute lifetime 1.25×
Cache write, 1-hour lifetime 2×
Cache read (hit) 0.1×
Cache read on Opus 5.5 and Sonnet 5.5 0.05×

In dollars per million input tokens:

Model Base input Cache hit
Opus 5.5 $4 $0.20
Sonnet 5.5 $2 $0.10

So on Sonnet 5.5, a 50,000-token system prompt plus project context costs $0.10 to send uncached and $0.005 on a cache hit. Over a 40-turn agent session, that difference adds up fast. Batch processing stacks a further 50% discount on top.

Other providers

Every major API now discounts cached input, but the details differ:

Provider / model Cached input
Gemini 4 Argon 95% off input during the intro period (to Nov 30, 2026)
MiniMax M3.1 Flash $0.015 per M (vs $0.15 input)
GPT-6 Astra Cache reads and writes are priced separately from input
Mercury 3 Cached input 90% off

See our full API pricing comparison for base prices, and the Gemini 4 Argon pricing breakdown for what changes after Nov 30.

The one rule: stable stuff first

Caches match an exact prefix. On Claude, the prefix is built in this order:

  1. Tools (tool definitions)
  2. System (system prompt)
  3. Messages (the conversation)

If anything changes early in that order, everything after it is a miss. The classic mistake:

System: You are a helpful assistant. Today is 2026-10-22 14:03:11. User: Sam.

That timestamp changes every request, so nothing after it is ever cached. Move changing details to the end, in the latest user message.

A cache-friendly layout:

[tools: same every call]
[system: instructions + style guide + project docs]   ← cache breakpoint here
[conversation history]                                ← optional second breakpoint
[new user message: includes today's date, user name]

How to turn it on (Claude)

Add a cache_control marker to the last block of the part you want cached. A Python sketch:

from anthropic import Anthropic

client = Anthropic()

response = client.messages.create(
    model="YOUR_MODEL_ID",
    max_tokens=1024,
    system=[
        {
            "type": "text",
            "text": LONG_PROJECT_INSTRUCTIONS,  # thousands of tokens that don't change
            "cache_control": {"type": "ephemeral"},  # 5-minute cache
        }
    ],
    messages=[{"role": "user", "content": "Add a test for formatPrice()."}],
)

print(response.usage)

Things to know:

  • You can set up to 4 breakpoints.
  • On the Claude 5.x models, the cached prefix must be at least 512 tokens. Shorter prefixes simply aren't cached.
  • For a 1-hour cache, use {"type": "ephemeral", "ttl": "1h"}. It's worth it only if you return to the same prefix often over a longer window, because the write costs 2× instead of 1.25×.

Check that it's working

Look at the usage object in the response:

  • cache_creation_input_tokens > 0 → you wrote to the cache (paid the write premium).
  • cache_read_input_tokens > 0 → you hit the cache (paid the discount).

If you only ever see creation and never reads, something in your prefix changes between calls. Diff two consecutive requests.

One more cost trap: tokenizers

Anthropic notes that the tokenizer on Claude 4.7 and later models produces roughly 30% more tokens for the same text than earlier models. If your bill jumped after a model upgrade, that's part of it, and caching is the cleanest way to win it back. Our context windows explainer covers how tokens add up in agent sessions.

Where you're already using it

Coding agents like Claude Code and Codex use caching behind the scenes, which is why long sessions cost less than you'd expect. Writing a good CLAUDE.md or AGENTS.md that stays the same between sessions helps the cache too.

FAQ

How much does prompt caching save?

On Claude, a cache read is 10% of the normal input price, and 5% on Opus 5.5 and Sonnet 5.5. Writing to the cache costs extra (1.25× or 2×), so you save money once the same prefix is reused a few times within the cache lifetime.

Why isn't my prompt cache hitting?

The usual causes: something changes near the start of the prompt (a timestamp or user name in the system prompt), the cache expired, or the cached part is below the minimum size. On Claude 5.x models the minimum is 512 tokens.

Do I have to change my code to use caching?

On Claude you mark where the cacheable prefix ends with a cache_control block, with up to 4 breakpoints. Other providers cache automatically or have their own settings, so check each provider's docs.

Does caching change the model's answers?

No. Caching only changes how the provider processes and bills the repeated input. The model sees the same prompt.

Sources

  1. Anthropic docs, Prompt cachinghttps://platform.claude.com/docs/en/build-with-claude/prompt-caching
  2. Anthropic docs, Pricinghttps://platform.claude.com/docs/en/about-claude/pricing
  3. Google, “Gemini 4 Argon is now available for developers”https://blog.google/innovation-and-ai/technology/developers-tools/build-with-gemini-4-argon/
  4. MiniMax, M3.1 Flash launch posthttps://www.minimax.io/blog/minimax-m31f

Newsletter

The Codema publication. Consent is required. You are not subscribed until you confirm from the email, and nothing is stored if email delivery is not connected.