LLM Prompt Caching: How to Reduce Token Costs
Prompt caching can reduce LLM input costs by reusing stable context instead of charging for it at full price on every request. This article explains prefix matching, cache writes and reads, TTL trade-offs, break-even economics, and the engineering practices needed to turn cache hits into measurable savings. It also covers tenant isolation, cache consistency, and workloads where caching is unlikely to pay off.
When large language models are billed by token, the biggest budget drain is often not the changing question and answer. It is the same system prompt, knowledge passages, tool definitions, and conversation prefix being sent with every request. For the application, this is stable context. On the bill, it can still be full-price input every time.
Prompt caching—also called context caching or prefix caching—stores a reusable copy of this largely unchanged prefix on the model service. Creating the cache has a cost, but subsequent cache hits are billed at a lower input rate. In high-frequency workloads, that can reduce the effective cost of input tokens.
Why Repeated Context Becomes a Cost Sink
A typical production prompt contains three layers:
- Relatively fixed system instructions and safety constraints.
- Knowledge or retrieved content that changes less frequently.
- The current user question and recent conversation, which change with each turn.
In customer support, coding assistants, document Q&A, and batch extraction, the first two layers can account for most input tokens. Without reuse at the gateway or application level, each call is billed for the full input. As request volume grows, costs rise roughly in proportion to the number of repeated tokens multiplied by their unit price.
Caching addresses this by turning a repeatable, matching prefix from full-price input into context that is created once and read multiple times at a discount.
Common billing structures distinguish three input categories: ordinary input, cache writes or creation, and cache reads or hits. Cache reads typically cost substantially less than ordinary input. The savings therefore come primarily from the cache hit rate, not from the model itself becoming cheaper.
Cache writes may cost slightly more than ordinary input to cover indexing, storage, and alignment overhead. If the time to live (TTL) is short or the prefix changes frequently, write costs can consume the savings. Caching does not automatically mean “90% cheaper.” It pays off when a stable prefix is reused enough times within the cache lifetime.
What Determines a Cache Hit?
Most inference services use exact prefix matching: the consecutive token sequence from the beginning of the prompt must match a cached prefix. An inserted space, reordered knowledge sections, or a small change to the system prompt can invalidate the match.
Place variable content at the end of the prompt. This includes the current user question, dynamic retrieval results, timestamps, and session IDs. Keep system roles, tool JSON schemas, fixed few-shot examples, and long-lived knowledge at the beginning, and manage them with explicit versions.
Cache granularity also matters. Services often align caches to fixed token blocks. A short prefix may not meet the minimum caching threshold, while a long prefix can lose a large amount of reusable context after a single change.
Build prompts structurally, using clear delimiters and a fixed field order. Apply content hashes to the knowledge layer: reuse the layer when its hash is unchanged, and replace it when the hash changes, accepting a new cache write. Do not indiscriminately place the entire conversation history in the cacheable prefix unless the intended design explicitly uses a fixed window of the most recent N turns.
Some services return cache creation and cache read token counts in their responses. These are direct signals of cache effectiveness:
- A high read share suggests that the prefix structure is working.
- A high creation share may indicate a short TTL, unstable prefixes, or many concurrent first requests.
Record these metrics in logs and monitoring, grouped by endpoint, tenant, and prompt version. Total spending alone does not explain whether caching is working.
Matching TTL and Write Costs to the Workload
Caches have a lifecycle. A short TTL, such as a few minutes, suits requests that reuse the same session or document in quick succession. Examples include follow-up questions about a support ticket, repeated code completions for the same file, and batch processing with a shared prompt template.
Once the TTL expires, the cache must be created again. If requests arrive farther apart than the TTL, the workload may repeatedly incur the write premium.
Longer TTLs or explicit cache keys can be a better fit for a gateway that shares the same system prompt and tool definitions throughout the day. However, they also involve greater storage and consistency costs. Choose based on the service’s actual capabilities rather than assuming every platform exposes the same controls.
Estimating the Break-Even Point
The write premium determines how much reuse is needed before caching saves money.
Normalize the ordinary input price to 1. Let the cache read price be α, where α < 1, and the cache write price be 1 + β. If the same prefix is used n times within its TTL, including the first request, the approximate cost relative to paying full price every time is:
Cost ratio = [(1 + β) + (n − 1)α] / n
As n increases, the ratio approaches α. When n is close to 1, caching may cost more than ordinary input.
Estimate the expected number of reuses for each endpoint. If reuse falls below the break-even point, disable caching or shorten the cached prefix so that it contains only the genuinely stable system context.
Application-Side Caching Is a Separate Layer
Offline caching on local disks or in object storage serves a different purpose. Storing retrieval results, document chunks, or embeddings can reduce repeated retrieval and prompt-building work, lowering overhead before a request reaches the model.
It does not replace model-side prefix caching. When using both, ensure that the final prefix sent to the model remains byte-for-byte stable; otherwise, model-side cache hits can still fail.
Engineering Practices for Production
1. Treat Prompts as Interface Contracts
Version system prompt changes. During a gradual rollout, separate old and new versions with distinct cache keys. Mixing versions can lead to repeated writes and overlapping cache costs.
2. Centralize Prompt Assembly
Have the gateway enforce a consistent assembly order. Do not allow application code to insert debugging fields into the stable prefix.
3. Separate Stable Knowledge from Dynamic Content
Use layers for large knowledge inputs. Put stable manuals in the cacheable section, while record-specific content belongs in the dynamic tail or is supplied through retrieval. Avoid placing an entire knowledge base in the prefix.
4. Prewarm Frequently Used Prefixes
Concurrent first requests can create a burst of cache writes. For frequently reused prefixes, consider sending a cache-creation request after startup or deployment to prewarm the cache.
5. Measure Effective Input Cost
Evaluate results using:
Effective input price = Total input cost / Total input tokens
Track cache hit rates separately. Request counts alone do not show whether repeated tokens are being billed at a discount.
Security and Consistency
Cached prefixes may contain internal procedures or persistent user memory. Isolate caches by tenant, and do not share cache keys across tenants. For prefixes containing personal data, use shorter TTLs or limit reuse to the session.
A cached prefix is a snapshot. Knowledge updates require a new version and may temporarily incur costs for both old and new caches. Changing a source file does not automatically update the context already stored in the cache.
When Prompt Caching Is Worth Using
Prompt caching is a strong fit when:
- Many users share the same system prompt.
- Multiple questions about the same document or ticket arrive within a few minutes.
- Batch requests share a template and change only the trailing fields.
- Agent loops retain the same tool definitions while observation results change.
It is a poor fit when:
- Retrieval results change substantially on every request and appear near the beginning of the prompt.
- Prompts are frequently edited during experimentation.
- Request intervals are much longer than the TTL.
- The prefix is too short to meet the caching threshold.
For these workloads, prioritize prompt compression, response caching—which caches model outputs rather than inputs—or routing to a smaller model instead of forcing context caching into the design.
Conclusion
The key to lower costs is increasing the number of hits on a stable prefix so that discounted reads amortize cache creation and storage costs.
Structure and version prompts, make cache behavior observable, and enable caching according to expected reuse. These practices turn theoretically cheap cache hits into verifiable savings on the bill, rather than an extra layer of write charges.
