AIDiscount优汇智
Home / Blog

Prompt Caching: How It Works and How to Save Money With It

How prompt caching bills input tokens at a lower rate on repeated requests, how to structure a prompt so the cache actually hits, and current cache prices.

2026-11-25 · 3 min read · AIDiscount team

Prompt cachingPricingTokensAPI

Prompt caching lets a model skip re-processing the part of a prompt it has already seen recently. When a new request starts with the same text as a request from the last few minutes, the repeated prefix is billed at a lower cache-read rate instead of the normal input rate. The first request that creates the cache usually costs slightly more (a cache-write rate), and every matching request after that is cheaper.

Why it matters

Many real apps resend a large block of fixed text with every request: a system prompt, a set of tool definitions, a long document the user is asking questions about, or a codebase's worth of context for a coding agent. Without caching, you pay the full input rate for that block every single time. With caching, you pay once and then pay the cache-read rate, which is typically a fraction of the normal input price, on every later request that reuses it.

How to structure a prompt for cache hits

The cache matches a prefix: the identical text from the start of the prompt up to some point. Put content in this order:

  1. Fixed content first: system prompt, tool definitions, long reference documents, few-shot examples. This should be byte-for-byte identical across requests.
  2. Variable content last: the user's current message, the latest turn of a conversation, anything that changes on every call.

If you change even one character in the fixed part, the cache misses from that point on, and the request falls back to the normal input rate for everything after the change. Common ways people accidentally break the cache: inserting a timestamp into the system prompt, reordering tool definitions, or re-serializing JSON with different key order or whitespace.

Example: a long system prompt

from openai import OpenAI

client = OpenAI(base_url="https://aidiscount.ai/v1", api_key=os.environ["AIDISCOUNT_API_KEY"])

SYSTEM_PROMPT = open("system_prompt.txt").read()  # several thousand tokens, identical every call

def ask(user_message):
    return client.chat.completions.create(
        model="claude-opus-4-8",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": user_message},
        ],
    )

Because SYSTEM_PROMPT is identical on every call and comes first, the second and later calls within the cache window bill that block at the cache-read rate.

Checking whether the cache actually hit

The response's usage field reports cached tokens separately, so you can confirm caching is working instead of assuming it:

resp = ask("What does section 4 say about refunds?")
print(resp.usage.prompt_tokens, getattr(resp.usage, "prompt_tokens_details", None))
# prompt_tokens_details.cached_tokens > 0 means the cache hit

If cached_tokens stays at 0 across repeated calls with the same prefix, something in the fixed part is changing between requests, or the gap between requests is longer than the cache's retention window.

Anthropic Messages format

Anthropic's format marks the cache boundary explicitly with cache_control on a content block, rather than relying only on prefix matching from the start:

resp = client.messages.create(
    model="claude-opus-4-8", max_tokens=1024,
    system=[{"type": "text", "text": SYSTEM_PROMPT, "cache_control": {"type": "ephemeral"}}],
    messages=[{"role": "user", "content": "What does section 4 say about refunds?"}],
)
print(resp.usage.cache_read_input_tokens, resp.usage.cache_creation_input_tokens)

What does and doesn't benefit

  • Good fit: a chatbot with a long fixed system prompt and short user turns; a coding agent that resends the same large file; a document Q&A app with one document and many questions.
  • Little benefit: short one-off prompts with no shared prefix between requests; prompts where the only large part is the part that changes every time.
  • Multi-turn chat: each new message extends the previous conversation, so the growing history itself becomes the cached prefix if the earlier turns are not modified. Avoid editing earlier messages in place if you want the cache to keep working.

Current cache prices

Cache pricing is per model; not every model offers it. Where a model supports caching, its page in the marketplace lists the read and write rates alongside the normal input and output price.

ModelInput / 1M tokensOutput / 1M tokensvs. official price
Claude Opus 4.8
claude-opus-4-8
$0.375$1.87592.5% off
Claude Haiku 4.5
claude-haiku-4-5-20251001
$0.075$0.37592.5% off
DeepSeek V4 Pro
deepseek-v4-pro
$0.3857$1.157141.6% off
GPT-6.2 Sol
gpt-6.2-sol
$0.20$1.0090% off

Prices are read from the live model marketplace when this page is generated and may change. The marketplace always shows the current rate.

Try it with AIDiscount. One API key works with OpenAI- and Anthropic-compatible tools. Pay as you go, no monthly fee.
Create a free accountRead the API docs

Related guides

Languages

English · 简体中文 · 繁體中文 · 日本語 · 한국어 · Español · Français · Deutsch · Português · Русский