What Is LLM Cache Hit and How to Reduce Token Costs
This article explains what a cache hit means in LLM API usage, why bills do not grow linearly with repeated calls, and how prefix caching reuses KV Cache rather than text. It covers the hard conditions for a hit, the pricing logic behind the roughly 10x discount, the engineering constraints caching imposes on inference services and application design, actionable practices for improving hit rates, and common misconceptions.
1. A Counterintuitive Pattern: Hundreds of Calls, No Linear Bill Growth
An automated coding task may invoke the model hundreds of times from start to finish. Intuitively, if every step resends the full conversation history, the history grows linearly with each turn, cumulative processed tokens should approach the order of “turns squared,” and the bill should spiral out of control quickly.
Real bills are often far below that estimate. The reason is not mysterious: usage data usually contains two parallel fields, one for regular input tokens and one for tokens that hit the cache. Naming differs across platforms — some call it cache read volume, others cache input volume — but the semantics are the same: this portion was not fully recomputed on the server; it reused intermediate results already computed earlier.
For coding agents, the measured shape looks roughly like this: a single-step request carries a history prefix of about 119K tokens, while the genuinely new content that needs full computation is only about 875 tokens; most of the history prefix is billed at a discounted fraction of the regular input price (commonly around one-tenth). In other words, the cost structure shifts from “full repeated computation” to “incremental computation plus cheap reuse.”
This rule may look like a billing detail, but it simultaneously constrains API pricing models, GPU memory management in inference clusters, request scheduling strategies, and how upper-layer agent frameworks organize context. Understanding it is a prerequisite for cost governance in LLM applications.
2. The Technical Essence of a Cache Hit: Reusing KV Cache, Not Text
The core cost of autoregressive generation comes from the key-value cache (KV Cache) in the attention mechanism. For every token processed, the model produces Key and Value vectors for each layer and each attention head and keeps them in GPU memory for later attention computation. Generating the Nth token theoretically requires the KV of the previous N-1 tokens.
The problem is that if the same prefix is submitted repeatedly, the server repeats the same projection computation, and that compute is pure waste. Prefix Caching takes a direct approach: keep the KV tensors for a given prefix and index them by the prefix content. When a new request’s prefix matches the cached content exactly, the KV is loaded directly and all forward computation for that prefix is skipped.
This yields several hard conditions for a cache hit:
- The prefix must be byte-for-byte identical. Cache indexes are typically hashes over the token sequence or even the raw bytes; one space, one newline, or one punctuation difference changes the hash.
- The match must be prefix-based. Matching starts only from the beginning; inserting or modifying content in the middle invalidates everything after the insertion point.
- Hits are subject to time and capacity limits. Cache entries expire and are evicted, GPU memory or cache pool capacity is finite, and entries can be pushed out under high concurrency.
- A hit is not free. The matched portion still counts as input tokens, just at a lower unit price, and there is usually a one-time cache write cost.
These four points explain most field questions of the form “why didn’t my cache hit?”
3. Billing Logic: Why the Discount Can Reach One-Tenth
Regular input token pricing covers the full matrix multiplications and attention computation; cache read pricing covers only memory movement and a small amount of concatenation work. Compute consumption differs by an order of magnitude, so price naturally differs by an order of magnitude. Cache writes are the opposite: they require an extra full computation and persisting the KV to storage or keeping it in GPU memory, so the first request may cost slightly more.
This leads to an important cost judgment: caching is not unconditionally worthwhile. Only when the same prefix is reused enough times do the saved read costs cover the write premium. A minimal model:
Total cost without cache ≈ N * L * P_inputTotal cost with cache ≈ L * P_write + (N - 1) * L * P_read + N * D * P_inputwhere N is the number of request turns, L is the shared prefix length,D is the new tokens per turn,P_read is typically about 0.1 of P_input, and P_write is slightly above P_input
When N is large and L is much larger than D, the second option has a clear advantage — exactly the typical shape of agent workloads. Conversely, one-off long-document summarization or batches of unrelated short requests may gain nothing from caching, or even lose slightly.
4. Engineering Constraints: How Caching Shapes System Design Backwards
Once the caching mechanism becomes the cost baseline, it propagates constraints upward.
On the inference service side, KV Cache is one of the main consumers of GPU memory, and its capacity directly determines the concurrency ceiling. Providers must trade off “retain more prefixes to raise the hit rate” against “free GPU memory to improve batching throughput,” which is why layered caching (GPU memory plus main memory/local storage), eviction by热度, and request routing affinity are common: requests carrying the same prefix are scheduled to the same instance as much as possible so the cache is not scattered.
On the application side, the conclusion is more direct: how you organize context determines the bill. Putting stable, unchanging content first and volatile content last is the single most cost-effective rule.
5. Actionable Practices to Improve Hit Rate
- Put the stable prefix first. Fixed content such as system prompts, tool definitions, specification notes, and long-term memory goes at the very front of the context and must not be rewritten mid-session.
- Move or remove volatile information such as timestamps. Inserting “current time 12:04:33” at the start of a prompt invalidates the entire prefix every second — the most common hit-rate killer.
- Use append-only history carefully. Appending without retroactive edits is the most prefix-cache-friendly form; when a correction is needed, append a correction note at the end.
- Control the order and content of tool definitions. The JSON serialization of the tool list must be stable; avoid random ordering and conditional additions or removals.
- Normalize long documents. Normalize whitespace and line breaks so the same document does not produce multiple cache entries due to formatting differences.
- Tier by reuse frequency. High-frequency shared prefixes can have their cache lifetime explicitly extended; one-off content does not need caching.
- Observability first. Continuously collect metrics such as cache read ratio, hit rate, and average prefix length, and build a cost dashboard with logging services and monitoring alerts, treating “cache hit rate” as a first-class performance metric.
An illustrative context layout:
[Stable zone] System role description → output spec → tool definitions (fixed order) → long-term project conventions[Semi-stable zone] List of files already read → history summary[Volatile zone] New instructions for this turn → latest tool results → dynamic info such as time/environment
6. Common Misconceptions
- “A cache hit is not billed.” Wrong — it is still input tokens, just at a discounted price.
- “Repeated content is enough to hit.” Wrong — it must be a shared prefix and exactly identical.
- “Caching always saves money.” Wrong — with low reuse frequency, the write premium can eat the gains.
- “Hit rate is a model capability issue.” Wrong — it is mainly determined by the caller’s context engineering.
7. Conclusion
Cache hits turn LLM cost from “billed per call” into “billed per incremental computation.” For developers, this moves part of cost optimization from the backend to context design: stable prefix first, volatile content last, append only and never rewrite, and observable metrics. As agents take on more long-running tasks, these constraints are not an optional optimization but a basic skill of system design. If you build on cloud-based model services, you can run inference and business workloads on a container platform, store long documents in object storage, and continuously track cache metrics with a logging and monitoring system, turning cost governance into a sustainable engineering loop.
