LLM API Input Caching: How Cache Hits Work and How to Optimize Costs
Input caching can reduce LLM API costs by reusing computation for shared prompt prefixes, but savings depend on cache hit rates, pricing rules, and request timing. This article explains cached-input billing, outlines a Python cost model, and covers practical optimizations for prompt structure, batch scheduling, and pricing configuration.
1. What Is an Input Cache Hit?
In large language model inference services, input caching—typically implemented as prefix caching—is a server-side optimization. When multiple requests share the same prefix, the service can reuse previously computed intermediate states, such as the KV cache, instead of repeating the full prefill computation for that portion of the input.
API billing therefore often divides input tokens into two categories:
- Cached input tokens: Tokens whose previously computed states can be reused. They require less computation and typically have a substantially lower unit price.
- Uncached input tokens: Tokens that require fresh computation and are billed at the standard input rate.
Some providers also charge for cache writes or storage, adding fees when a cache is first created or while it remains resident.
Understanding these categories is the first step toward interpreting an LLM API bill. In some pricing arrangements, cached input can cost one-tenth of the standard input rate or less. Meanwhile, uncached-input pricing may fluctuate with supply, demand, and compute allocation. As a result, requests totaling the same one million input tokens can produce significantly different bills depending on their cache hit rates.
2. Why Cached-Input Pricing Changes
Since 2026, some model providers have adjusted cached-input prices repeatedly, moving through promotional discounts, subsequent price revisions, tiered rates, and time-based pricing. Three factors help explain these changes.
Cache Resources Have Their Own Costs
Cached states must remain in GPU memory or dedicated storage, consuming scarce infrastructure resources. As cache-heavy traffic increases, competition for cache capacity grows. Providers may use pricing to encourage more efficient resource use.
Compute Demand Varies Over Time
API traffic often peaks during weekday business hours, such as 9 a.m. to noon and 2 p.m. to 6 p.m., while demand may be lower overnight and on weekends.
Providers that support peak and off-peak pricing charge more during busy periods to encourage users to move non-real-time workloads—such as offline batch processing, data cleaning, and evaluation runs—to lower-demand periods. Applicable schedules depend on the provider’s billing rules.
Market Competition Drives Discounts
As competition in the LLM API market intensifies, pricing becomes an increasingly important selection criterion for developers.
Examples of pricing changes have included substantial reductions in cached-input rates, extensions of flagship-model promotions, the conversion of promotional rates into ongoing prices, and weekend-wide off-peak billing.
One reported adjustment increased cached-input pricing from approximately CNY 0.025 to CNY 0.30 per million tokens—more than a tenfold increase. Such changes illustrate how sharply cache-related rates can move and why applications with high cache hit rates can be particularly sensitive to pricing revisions. These examples should not be treated as universally available offers or current rates.
3. Billing Structure and Cost Estimation
A typical LLM API bill can be expressed as:
Total cost = Cached input tokens × Cached-input unit price+ Uncached input tokens × Uncached-input unit price+ Output tokens × Output unit price+ Optional cache-write or storage fees
Developers should build a cost model during provider selection rather than waiting until an application reaches production scale.
The following Python function provides a simple estimate:
def estimate_cost(hit_tokens, miss_tokens, output_tokens,price_hit=0.30, price_miss=4.0, price_output=16.0):"""Prices are in CNY per million tokens.Values are illustrative; use the provider's actual rates."""return (hit_tokens / 1e6 * price_hit+ miss_tokens / 1e6 * price_miss+ output_tokens / 1e6 * price_output)# Example: a long-context conversation with a 90% input cache hit ratecost = estimate_cost(hit_tokens=900_000, miss_tokens=100_000,output_tokens=200_000)print(f"Estimated cost: CNY {cost:.2f}")
This function covers cached input, uncached input, and output charges. Any applicable cache-write or storage fees must be accounted for separately.
Its purpose is not to reproduce every detail of a provider’s invoice. Instead, it makes the cache hit rate an explicit optimization variable. In long-context workloads, increasing the hit rate by 10 percentage points can materially reduce input costs, depending on the difference between cached and uncached rates.
4. Practical Ways to Improve Cache Hits and Control Costs
1. Keep Reusable Prefixes Stable
Place system prompts, tool definitions, and fixed knowledge-base passages at the beginning of requests, and keep their representation consistent.
Small changes—including whitespace, punctuation, or field ordering—can disrupt prefix matching and force affected content to be recomputed. Stable prefixes are therefore central to effective cache reuse.
2. Put Dynamic Content Last
Place frequently changing information, such as live user input, timestamps, and random IDs, toward the end of the prompt.
This keeps volatile content from interrupting the reusable prefix.
3. Prioritize Conversation and Document Reuse
Two workloads are particularly well suited to input caching:
- Multi-turn conversations that repeatedly include the full conversation history.
- Multiple requests that share the same long document.
These scenarios contain substantial repeated input and should be priorities when improving request organization for cache reuse.
4. Schedule Flexible Workloads Outside Peak Hours
On platforms that support time-based pricing, schedule offline tasks—such as embedding generation, batch summarization, and regression evaluations—during off-peak periods or weekends.
Some providers apply off-peak rates throughout the weekend, which can benefit batch-oriented applications. Availability and billing schedules must be checked against the relevant provider’s rules.
For latency-sensitive online services, pay particular attention to rate limits and retry behavior during peak hours rather than assuming workloads can simply be rescheduled.
5. Track Pricing Notices and Billing-Rule Versions
Providers may announce pricing changes in advance through email or other channels, sometimes with a notice period such as 24 hours. Depending on the provider’s terms, users who do not accept a new price may be able to discontinue service and request a refund of their remaining balance.
These notice periods and refund options are provider-specific, not universal guarantees.
At the integration layer, make prices dynamically configurable rather than hard-coding them:
PRICING = load_pricing_config() # Load from configuration management; update after noticesdef on_pricing_change(new_config):global PRICINGPRICING = new_configrecalculate_budget_forecast()
Keeping pricing configuration aligned with the applicable billing rules helps ensure that budget forecasts reflect rate changes.
5. Conclusion
Input cache hits are more than a performance metric: they directly influence the cost structure of LLM API usage.
When cache-based billing is combined with peak and off-peak rates or other time-dependent rules, the same workload can cost substantially different amounts depending on prompt organization and execution time.
Three practices form the foundation of cost optimization:
- Maintain stable, reusable prompt prefixes.
- Schedule flexible workloads during lower-cost periods where supported.
- Update billing configuration as provider pricing changes.
Considering these factors during provider selection and architecture design helps developers balance model capabilities with sustainable API spending.
