Calculating Real LLM API Costs: How Cache Hits and I/O Ratios Affect Your Bill

    LLM API pricing is rarely as simple as looking at the nominal token price. This article breaks down the actual billing structure of LLM APIs, focusing on the critical roles of input/output ratios and cache hit rates. By understanding the mechanisms behind KV caching and applying a straightforward calculation formula, developers and businesses can accurately estimate their comprehensive costs. We also provide actionable engineering practices to optimize prefix stability, monitor hit rates, and ultimately control LLM API expenditures.

    1. Breaking Down the Billing Structure

    LLM API billing is typically measured in tokens, commonly falling into three pricing tiers:

    • Output Price: Billed per million tokens when the model generates content; this is the highest unit price.
    • Input Unhit Price: The price for input content that is not reused by the server-side cache.
    • Input Hit Price: The price when the input prefix matches a recent request and can directly reuse the cache; this is the lowest unit price.

    The following values are examples set for calculation purposes only and do not represent the pricing of any specific product:

    Tier Previous Gen (Example) New Gen (Example) Multiplier
    Output 27 RMB/Million Tokens 100 RMB/Million Tokens ~3.7x
    Input Unhit 6.5 RMB/Million Tokens 20 RMB/Million Tokens ~3.1x
    Input Hit 1.1 RMB/Million Tokens 2 RMB/Million Tokens ~1.8x

    On the surface, the unit prices for all three tiers are increasing. However, the rate of increase varies across tiers. The hit price has the smallest increase, while the output price has the largest. After the price adjustment, the gap between unhit and hit prices widened from about 6x to 10x, and the gap between output and hit prices expanded from about 25x to 50x. This means the value of cache reuse has become significantly higher, while the cost pressure of output tokens has become more prominent.

    2. Nominal Prices Can Be Misleading

    The nominal price is the cost of a single token. Your actual bill depends on two factors: the ratio of input to output, and the proportion of input that hits the cache. A change in either of these will alter your comprehensive unit price.

    Therefore, when evaluating the impact of a price adjustment, you should first estimate two parameters for your specific business:

    1. Input/Output Ratio: The average number of input tokens versus generated output tokens per request.
    2. Cache Hit Rate: The proportion of input that can be reused by the server-side cache.

    Once you determine these two parameters, you can calculate the comprehensive unit price for your specific use case.

    3. How Cache Hits Work

    When an LLM processes input, it must perform computations across the entire input to generate intermediate states (commonly known as the KV cache). If the beginning of a new request perfectly matches a previous one, the server can directly reuse these intermediate results and skip the redundant computations. The reused portion is billed at the hit price, while the rest is billed at the unhit price.

    This mechanism relies on several key prerequisites:

    • Hits depend on prefix consistency. The cache matches segments sequentially from the beginning of the input. If a change occurs at any point in the prefix, subsequent content cannot be reused.
    • A minimum cacheable length exists. Inputs that are too short typically cannot hit the cache. Refer to the service provider’s documentation for specific thresholds.
    • Caches have a TTL (Time to Live). Caches that are not reused for an extended period will be evicted. Therefore, request frequency also impacts the hit rate.

    A common industry practice is to split prefill (processing input) and decode (generating output) into different resource pools, scheduling around the cache to improve hit rates and throughput. The effectiveness of such architectures depends heavily on the degree of prefix reuse in your business; you cannot simply apply a fixed number without considering the specific scenario.

    4. Calculating the Comprehensive Unit Price

    The formula for calculating the comprehensive unit price is as follows:

    1. Effective Input Price = Hit Rate * Hit Price + (1 - Hit Rate) * Unhit Price
    2. Comprehensive Price = (Input Volume * Effective Input Price + Output Volume * Output Price) / (Input Volume + Output Volume)

    The formula implies: first, weight the hit and unhit inputs proportionally to get the actual input price; then, weight the input and output by their token volumes to get the comprehensive cost per token.

    Let’s demonstrate this calculation using two typical scenarios. The prices used follow the example values above.

    Scenario A: Long-context Coding Agent

    • Input/Output Ratio: 20:1
    • Cache Hit Rate: 90%
    • Effective Input Price = 2 0.9 + 20 0.1 = 3.8 RMB/Million Tokens
    • Comprehensive Price = (20 3.8 + 1 100) / 21 ≈ 8.4 RMB/Million Tokens

    Scenario B: Individual Developer with Occasional Calls and Highly Varied Prompts

    • Input/Output Ratio: 5:1
    • Cache Hit Rate: 20%
    • Effective Input Price = 2 0.2 + 20 0.8 = 16.4 RMB/Million Tokens
    • Comprehensive Price = (5 16.4 + 1 100) / 6 ≈ 30.3 RMB/Million Tokens

    Although both scenarios use the exact same pricing, the comprehensive prices differ by about 3.6x. This gap primarily stems from the hit rate. In Scenario A, most of the input is billed at the hit price, whereas in Scenario B, almost all input is billed at the unhit price.

    Hit Rate Sensitivity

    In the scenario with a 20:1 input/output ratio, every 10-percentage-point change in the hit rate alters the comprehensive price by approximately 1.7 RMB. The calculation is as follows:

    • 90% Hit Rate: Comprehensive price ≈ 8.4 RMB
    • 80% Hit Rate: Effective input price is 5.6 RMB, comprehensive price ≈ 10.1 RMB

    This demonstrates that improving the hit rate is one of the most direct ways to reduce costs, an effect that is especially pronounced in long-input scenarios.

    5. Impacts Vary Across User Groups

    A price adjustment does not affect all users equally; it depends on two factors:

    • Businesses with long-term reuse of fixed prefixes, such as enterprise services using fixed system prompts and fixed tool definitions. These businesses have high hit rates, and their comprehensive price might be lower than what they perceived prior to the adjustment.
    • Businesses with highly varied request content, such as one-off Q&A or randomly constructed prompts. These have low hit rates, making their comprehensive price close to the unhit price, meaning the impact of the price adjustment is more noticeable.

    Therefore, when assessing the impact of a price change, do not just look at the nominal price increase. Instead, use your actual logs to calculate the hit rate and input/output ratio, and plug them into the formula.

    Additionally, billing rules vary among service providers—such as how hits are determined, minimum cache lengths, cache TTLs, and whether hit prices apply to all input content. Always refer to the official billing documentation of your service provider rather than making assumptions based on past experience.

    6. Engineering Best Practices

    1. Maintain Prefix Stability

    Place unchanging content—such as system prompts, role settings, and tool definitions—at the very beginning of the request, ensuring the order and text are completely identical for every request. Tool lists should be arranged in a fixed order to avoid invalidating the prefix due to sequence changes.

    2. Place Dynamic Content at the End

    User questions, retrieved results, current timestamps, and other variable content should be placed after the fixed prefix. Do not embed timestamps, request IDs, or any fields that change with every request inside the prefix.

    3. Monitor Hit Rates

    Track hit rates by API endpoint, user type, or task type to identify areas with unusually low hit rates. Record the input volume, output volume, and hit volume for each request to facilitate regular reviews of the comprehensive price.

    4. Control Output Length

    Since the output unit price is much higher than the input unit price, reducing unnecessary output yields more savings than reducing an equivalent amount of input. You can control output by setting explicit output format requirements, defining reasonable maximum generation lengths, and removing redundant explanatory text.

    5. Compress Context

    Summarize historical conversations and retain only information relevant to the current task. This reduces the volume of unhit input and makes the prefix more stable.

    6. Choose Strategies Based on Scenarios

    For high-frequency tasks using fixed templates, prioritize prefix stability and hit rates. For low-frequency, one-off tasks, focus on input and output lengths to avoid unnecessary context accumulation.

    7. Conclusion

    Evaluating price adjustments requires combining the billing structure with your business characteristics. Looking only at nominal price increases can easily lead to overestimating or underestimating the actual impact. Developers should first measure the input/output ratio and cache hit rate of their own business, use the comprehensive price formula to calculate costs, and adjust their prompt structures and request methods accordingly. For businesses relying on long contexts and fixed prefixes, maintaining a stable prefix is typically the most effective method for controlling costs.