How to Understand Changes in LLM Token Costs: From Cache Hit Rates to Blended Unit Pricing

    List prices alone do not determine the cost of using a large language model API. This article explains how to estimate effective input pricing and blended per-token costs by combining cache hit rates, input-to-output ratios, and the prices for cache misses, cache hits, and generated output. It also compares two typical workloads, provides a reusable cost-estimation function, and summarizes engineering practices for improving cache efficiency.

    1. Why List Prices Do Not Directly Represent Cost

    Large language model APIs typically charge per million tokens and use different rates for input tokens, output tokens, and cached input tokens. Looking at the price change for only one category can easily lead to the conclusion that prices have increased across the board.

    In practice, actual spending depends on the proportion of each token type in every request and on the cache hit rate. To determine how a pricing change will affect your workload, first estimate its token distribution and then apply the relevant prices.

    2. Breaking Down the Three Pricing Categories

    • Input cache misses: New content in a request that must be processed in full.
    • Input cache hits: Request prefixes that match recent requests and can reuse previously computed results.
    • Output: Content generated by the model token by token. Output is usually the most expensive category and cannot be reused through input caching.

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

    • Input cache miss: 20 yuan per million tokens
    • Input cache hit: 2 yuan per million tokens
    • Output: 100 yuan per million tokens

    These figures illustrate two important points. First, the price difference between cache hits and cache misses can be substantial. As the hit rate increases, input costs can fall significantly. Second, output tokens are much more expensive than input tokens. The higher the output share, the less impact caching has on total savings.

    3. Converting Cache Hit Rate into an Effective Input Price

    The effective input price can be calculated as follows:

    1. Effective input price =
    2. Cache hit price × Hit rate
    3. + Cache miss price × (1 − Hit rate)

    Using the example prices:

    • At a 90% hit rate: 2 × 0.9 + 20 × 0.1 = 3.8 yuan per million tokens
    • At a 20% hit rate: 2 × 0.2 + 20 × 0.8 = 16.4 yuan per million tokens

    When the hit rate rises from 20% to 90%, the effective input price falls to approximately 23% of its original level. This is the most direct way caching affects cost.

    After calculating the effective input price, combine it with the ratio of input tokens to output tokens to determine the blended unit price. Let r represent the input-to-output token ratio:

    1. Blended unit price =
    2. (r × Effective input price + Output price) / (r + 1)

    This formula calculates a token-weighted average cost per million tokens. The larger r is, the more strongly changes in input pricing affect the blended cost.

    4. Comparing Costs Across Two Typical Workloads

    Scenario A: An Enterprise Agent with Long Context

    Each request includes a fixed system prompt, tool definitions, and a relatively long conversation history. The input-to-output ratio is approximately 20:1, and the cache hit rate is about 90%.

    • Effective input price: 3.8 yuan per million tokens
    • Blended unit price: (20 × 3.8 + 1 × 100) / 21 ≈ 8.4 yuan per million tokens

    Scenario B: A Developer with Infrequent Calls

    The prompt changes substantially from one request to the next. The cache hit rate is about 20%, and the input-to-output ratio is approximately 5:1.

    • Effective input price: 16.4 yuan per million tokens
    • Blended unit price: (5 × 16.4 + 1 × 100) / 6 ≈ 30.3 yuan per million tokens

    The blended unit prices differ by approximately 3.6 times, even though the price table is identical. The cost structure determines actual spending.

    Teams that repeatedly use fixed prefixes can gain a significant cost advantage from high cache hit rates. Users who reconstruct their prompts for every request receive little benefit from caching and will generally feel the impact of the same pricing change more strongly.

    5. Estimating Your Unit Cost with Code

    The following function packages the formulas above into a reusable cost-estimation tool. Replace the parameters with your own usage data:

    1. def blended_unit_cost(miss_price, hit_price, output_price, hit_rate, io_ratio):
    2. # miss_price: Input cache-miss price (yuan per million tokens)
    3. # hit_price: Input cache-hit price (yuan per million tokens)
    4. # output_price: Output price (yuan per million tokens)
    5. # hit_rate: Cache hit rate, from 0 to 1
    6. # io_ratio: Ratio of input tokens to output tokens
    7. # Returns the blended price in yuan per million tokens
    8. effective_input = hit_price * hit_rate + miss_price * (1 - hit_rate)
    9. return (io_ratio * effective_input + output_price) / (io_ratio + 1)
    10. blended_unit_cost(20, 2, 100, 0.9, 20) # Approximately 8.4
    11. blended_unit_cost(20, 2, 100, 0.2, 5) # Approximately 30.3

    For real-world estimates, obtain the cache hit rate and input-to-output ratio from server-side usage metrics. Calculate weighted averages weekly or monthly. Values from a single request can fluctuate significantly and should not be used directly as the basis for a budget.

    6. Engineering Practices for Improving Cache Hit Rates

    1. Use a fixed prefix and place dynamic content afterward. Put the system prompt, tool definitions, and role configuration at the beginning of the request, and keep them byte-for-byte identical. Variables such as timestamps, random IDs, and usernames should be placed at the end.
    2. Reduce prefix variation. Changes to whitespace, line breaks, or field ordering can prevent a prefix from matching. Put templates under version control and update them consistently when changes are required.
    3. Maintain session continuity. For the same Agent, reuse the same prefix across multiple calls whenever possible. Avoid frequently switching prompt versions or tool collections.
    4. Account for cache expiration. Caches typically have a lifetime, and hit rates may decline after a long idle period. For high-frequency workloads, scheduled calls can be used to keep a cache active, but those calls also incur costs and must be evaluated against the expected savings.
    5. Measure hit rates by task. Record input tokens, output tokens, and cache-hit tokens at the gateway or application layer, then aggregate the data by task type. An overall average can hide important differences between high-frequency and low-frequency workloads.

    7. A Checklist for Model Selection and Budget Planning

    • Review the input-to-output ratio for the past 30 days, separating high-frequency fixed tasks from variable tasks.
    • Calculate cache hit rates by task type instead of replacing them with a single overall average.
    • Apply both the old and new prices to calculate the change in blended unit cost.
    • If output represents a large share of usage, prioritize limiting output length and removing unnecessary generated content.
    • Set alert thresholds for the cost per business task rather than monitoring only the total bill.