LLM API Cost Estimation: How Cache Hit Rate Affects Total Price

    This article explains why the lowest LLM API list price may not produce the lowest bill. It breaks down cached input, uncached input, output tokens, and time-based pricing; distinguishes cache share from cache hit rate; provides a Python cost-estimation function; and uses a sensitivity example to show how cache share affects total cost.

    When choosing an LLM API service, the plan with the lowest list price is not necessarily the most cost-effective. The actual bill is determined jointly by call volume, cache hit status, input-output ratio, and billing period. This article starts from a cost model, explains step by step how to estimate real costs, and uses examples to show the impact of key parameters.

    1. What Determines Cost

    The cost of a single call can usually be split into three parts:

    1. Cached input tokens: The request prefix matches previous requests, so the server can reuse already computed intermediate results, resulting in a lower unit price.
    2. Uncached input tokens: New or changed content must be fully computed, resulting in a higher unit price.
    3. Output tokens: Content generated by the model, usually priced noticeably higher than input.

    In addition, some services differentiate prices by time period, with peak-hour unit prices higher than off-peak prices. Monthly packages exchange a fixed monthly fee for a certain quota, and their actual cost depends on whether the quota can be fully used.

    These four factors interact. Looking at only one unit price can easily lead to a wrong conclusion. For example, a plan with a very low cache unit price may not have a low total cost if its cache hit rate is not high.

    2. Unit Prices Cannot Be Compared in Isolation

    Many teams compare providers only by “how much per million tokens.” This comparison has two problems.

    First, different unit prices correspond to different token types. Cached input, regular input, and output may differ in price by several times, so comparisons must look at the overall cost.

    Second, the same unit price produces different results under different business structures. For long-document question answering, input is long and output is short, so the cache share determines most of the cost. For code generation, output tokens account for a higher share, so the output unit price has a greater impact. Therefore, before comparing plans, you should first measure the token structure of your own business.

    3. Build an Estimation Model

    Two concepts need to be distinguished here. Cache share refers to the number of cache-hit tokens as a proportion of total tokens (input plus output). Cache hit rate usually refers to the number of cache-hit tokens as a proportion of input tokens. The conversion relationship is:

    Hit rate = Cache share ÷ (1 − Output share)

    The code below calculates the cost per batch by share. The prices are example values, not actual prices of any service, and should be replaced with the provider’s official prices when used.

    1. # Example prices, unit: CNY per million tokens
    2. PRICE_A = {"cache_hit": 0.10, "miss": 1.00, "output": 2.00}
    3. def estimate_cost(total_m, cache_share, output_share, price):
    4. """
    5. total_m: Total tokens, in millions
    6. cache_share: Cache-hit tokens as a share of total tokens, e.g. 0.98
    7. output_share: Output tokens as a share of total tokens, e.g. 0.005
    8. price: Price dictionary, unit: CNY per million tokens
    9. Returns: cost in CNY
    10. """
    11. miss_share = 1 - cache_share - output_share
    12. if miss_share < 0:
    13. raise ValueError("The sum of cache share and output share cannot exceed 1")
    14. return total_m * (
    15. cache_share * price["cache_hit"]
    16. + miss_share * price["miss"]
    17. + output_share * price["output"]
    18. )
    19. # Cost per 100 million tokens
    20. print(estimate_cost(100, 0.98, 0.005, PRICE_A)) # about 12.3
    21. print(estimate_cost(100, 0.95, 0.005, PRICE_A)) # about 15.0

    The core of this code is to split the total volume into three segments, multiply each by its unit price, and then sum them. The model is simple but sufficient for plan comparison.

    4. Observe Sensitivity with an Example

    Assume Plan A uses PRICE_A above. When the total volume is 100 million tokens and the output share is 0.5%, the result is as follows:

    Cache share Corresponding hit rate (input basis) Cost per 100M tokens (CNY)
    98% ~98.5% 12.3