Back to Home
Cost Optimization9 min readSource: Admesh 技术团队

The Complete Guide to Prompt Caching: How It Works, Cost Model, and Implementation

Prompt caching can reduce the input token cost of repeated prefixes by an order of magnitude. This article breaks down the differences between Anthropic's and OpenAI's cache implementations, which scenarios actually save money, and how to reuse the same caching strategy across multiple models with a unified gateway.

What problem does prompt caching solve

In most LLM calls, a substantial portion of the input is the same every time: system prompts, few-shot examples, retrieved fixed documents, and tool definitions (tool schema). Under normal billing, you pay for these repeated input tokens every time, and the attention is recomputed every time as well.

The idea behind prompt caching is straightforward: the server stores the computed result of this prefix, and on the next request, if the prefix is exactly the same, it reuses it directly and only recomputes the newly added part. Anthropic positions it in its [prompt caching documentation](https://platform.claude.com/docs/en/build-with-claude/prompt-caching) as a way to reduce latency and cost; on the OpenAI side, the corresponding mechanisms are automatic prompt caching and batch discounts.

Which scenarios actually save money

Prompt caching is not a switch you flip to save money. It has a clear break-even boundary: number of times the same prefix is reused × prefix length > the extra overhead of writing the cache.

  • Long system prompt + multi-turn conversation: system prompts of several thousand tokens with many conversation turns almost always pay off.
  • RAG + fixed document set: put unchanging policy documents/knowledge bases in the prefix and let only the user question vary.
  • Code agents / tool calls: tool schemas and repository structure descriptions are usually repeated in every call.
  • One-off calls with short prefixes: the cost of writing the cache may exceed the savings; do not force caching.

Five common mistakes that invalidate the cache

  • Mixing timestamps or random IDs into the prefix: even a single Date.now() at a fixed position changes the entire prefix.
  • Dynamically assembling the system prompt: different users get different prefixes, diluting the cache hit rate to near zero.
  • Summarizing and compressing historical messages: rewriting once per turn makes the prefix unstable.
  • Putting user input first: this is the most common structural mistake; variables should be as late as possible.
  • Looking only at the bill and not the hit rate: without a hit rate metric, cache benefits cannot be attributed at all.

Reusing the same caching strategy across multiple models

If your product uses both Claude and GPT, cache semantics are not fully consistent across vendors (who declares cache breakpoints, how long the TTL is, how billing works). Putting this adaptation behind a unified gateway has the benefit that business code maintains only one prompt structure and does not need to be rewritten when switching models.

Admesh's gateway maintains an OpenAI-compatible protocol downstream, so the same prompt structure can switch between vendors by changing only the model field; cache hit billing is reflected uniformly in the usage details of the bill, so you do not need to integrate each vendor's billing semantics separately.

Admesh live rates (excerpt)

Prices come from the live Admesh configuration, in USD per million tokens. See the model catalog for the full list.

Admesh.ai Model Price List: Prices and Context Windows for 8 Enabled Models
Model Name and IDContext WindowEnter price / 1MOutput Price / 1MCache Hits / 1M
Claude Fable 5anthropic/claude-fable-5
1M$3.25 / 1M$16.25 / 1M$0.325 / 1M
Claude Fable 5.1anthropic/claude-fable-5-1
1M$3.25 / 1M$16.25 / 1M$0.08125 / 1M
Claude Haiku 4.5anthropic/claude-haiku-4-5-20251001
200K$0.325 / 1M$1.625 / 1M$0.0325 / 1M
Claude Haiku 5.5anthropic/claude-haiku-5-5
1M$0.0325 / 1M$0.1625 / 1M$0.00325 / 1M
Claude Opus 4.5anthropic/claude-opus-4-5-20251101
200K$1.625 / 1M$8.125 / 1M$0.1625 / 1M
Claude Opus 4.6anthropic/claude-opus-4-6
1M$1.625 / 1M$8.125 / 1M$0.1625 / 1M
Claude Opus 4.7anthropic/claude-opus-4-7
1M$1.625 / 1M$8.125 / 1M$0.1625 / 1M
Claude Opus 4.8anthropic/claude-opus-4-8
1M$1.625 / 1M$8.125 / 1M$0.1625 / 1M

The above are the standard unit prices for public channels; billing is based on actual token usage. View the complete model catalog

View billing units and cached token documentation →

Implementation checklist

  1. Split the prompt into three parts: 'stable prefix / semi-stable part / user input', with the stable part first.
  2. Ensure the stable prefix contains no timestamps, random numbers, or user IDs.
  3. Track cache read/write tokens separately in the bill and convert them into a real hit rate.
  4. Use a fixed set of evaluation requests for regular regression to confirm that prompt changes have not broken the hit rate.
  5. Set a lower bound for cache benefit (for example, roll back the prompt structure if it falls below 5%) to avoid optimizing for optimization's sake.

FAQ

How much can prompt caching save?
It depends on how many times the prefix is reused and the prefix length; there is no universal number. Empirically, savings are most obvious when the system prompt exceeds 2K tokens, the same conversation exceeds 3 turns, or the same document is retrieved repeatedly. It is recommended to calculate the actual hit rate as cached tokens ÷ total input tokens from the bill, then decide whether to adjust the prompt structure.
Can other users read the cached output?
No. The cache stores the prefix computation state under the same API key/account; it is not shared across tenants and does not expose the result of any response to other accounts. However, for security reasons, it is still not advisable to put any sensitive data in a prompt prefix that may be reused.
Does the cache remain after switching models?
No. The cache granularity is 'model + prefix'; switching models is equivalent to switching to a different cache space and requires re-warming. This is also why applications that switch frequently between multiple models often have a low cache hit rate.

References

Call these models through one gateway

Admesh provides a unified OpenAI-compatible API: one Base URL and one key for Claude, GPT, DeepSeek and more. New accounts get trial credit that never expires.