LLM API Cost Analysis: Cache Hit Rates and Plan Selection

    A practical framework for comparing pay-as-you-go pricing with subscription plans for large language model APIs. Learn how cache hit rates, input/output token ratios, peak-hour rules, and unused quotas affect the real cost of your AI workloads. Use the included formula and Python example to estimate costs and make better pricing decisions.

    Why List Prices Are Not Enough

    Large language model services usually publish prices per million tokens, but the final bill depends on the composition of each request. Even when two services have similar list prices, different cache hit rates can result in costs that differ by more than 2x.

    Before comparing providers, establish a consistent baseline: use the same workload and calculate the total cost with the same token distribution.

    At a minimum, separate tokens into three categories:

    • Cached input tokens
    • Uncached input tokens
    • Output tokens

    The prices for these categories can vary significantly. Cached input tokens are usually the least expensive, while output tokens are often the most expensive.

    Three Variables That Drive API Costs

    1. Cache hit rate

    A cache hit means the service reuses a previously processed prefix and charges a lower price for those tokens. Workloads with stable system prompts, long multi-turn conversation histories, or repeated retrieval passages from a knowledge base often achieve higher cache hit rates.

    The impact of a one-percentage-point change in the cache hit rate depends on the difference between cached and uncached input prices. The larger the price gap, the more sensitive the total cost is to cache performance.

    2. Input-to-output ratio

    Some workloads, such as code generation and long-document summarization, use far more input tokens than output tokens. Others, such as writing and structured content generation, produce a larger share of output tokens.

    Because output tokens are generally much more expensive than input tokens, even a small change in the output ratio can noticeably affect the bill.

    3. Billing periods

    Some services apply peak-hour rules during certain periods on business days. These rules may affect either the price or the rate at which subscription quotas are consumed.

    Calculations should reflect the actual time distribution of API calls. Avoid relying only on an all-day average.

    Calculating Pay-as-You-Go Costs

    The total cost for usage-based billing can be expressed as:

    1. Total cost = Total tokens × (
    2. Cache ratio × Cache price
    3. + Uncached input ratio × Input price
    4. + Output ratio × Output price
    5. )

    The following Python function implements this calculation and demonstrates how changes in the cache hit rate affect total cost. The sample prices are for illustration only and do not represent the pricing of any particular service.

    1. def estimate_cost(total_tokens_m, cache_ratio, input_ratio, output_ratio,
    2. price_cache, price_input, price_output):
    3. """
    4. total_tokens_m: Total tokens, in millions
    5. *_ratio: The ratio for each token category; the sum must equal 1
    6. price_*: Price per million tokens
    7. """
    8. if abs(cache_ratio + input_ratio + output_ratio - 1) > 1e-9:
    9. raise ValueError("The three token ratios must sum to 1")
    10. return total_tokens_m * (
    11. cache_ratio * price_cache
    12. + input_ratio * price_input
    13. + output_ratio * price_output
    14. )
    15. # Example prices: 0.1 per million cached tokens,
    16. # 1 per million uncached input tokens, and 4 per million output tokens
    17. print(estimate_cost(100, 0.98, 0.015, 0.005, 0.1, 1, 4)) # 13.3
    18. print(estimate_cost(100, 0.95, 0.045, 0.005, 0.1, 1, 4)) # 16.0

    The two examples show that when the cache hit rate drops from 98% to 95%, the cost for the same 100 million tokens rises from 13.3 to 16.0, an increase of approximately 20%.

    A three-percentage-point decline in the cache hit rate can therefore have a substantial financial impact. This is why caching deserves dedicated optimization rather than being treated as a secondary implementation detail.

    Calculating the Effective Price of a Subscription Plan

    A subscription plan typically charges a fixed monthly fee and provides an allowance measured in tokens or requests. To compare it with usage-based billing, calculate its effective price:

    1. Effective price ratio =
    2. Monthly fee ÷ (Monthly allowance × Comparable pay-as-you-go price)

    For example, suppose a plan costs 100 per month and its allowance, converted using the same token distribution, would cost 150 under usage-based billing. The plan’s effective price is approximately 67% of the pay-as-you-go price.

    The lower this ratio, the more attractive the subscription plan appears. However, the actual discount depends on whether the allowance can be fully used. If real usage is far below the quota, the effective cost per token will be higher than the initial estimate.

    How Peak Hours Affect Subscription Quotas

    Some plans consume quotas more quickly during peak business hours. If the calculation uses only the non-peak quota rate, the estimated discount will be overly optimistic.

    Adjust the calculation as follows:

    1. Measure request volume across the week and separate peak-hour traffic from non-peak traffic.
    2. Multiply peak-hour request volume by the applicable quota consumption factor to obtain the equivalent quota usage.
    3. Compare the adjusted quota consumption with actual usage, then recalculate the effective price.

    If most requests occur during peak working hours, the plan’s real discount may be much lower than its advertised value. In that situation, pay-as-you-go billing or a hybrid approach may be more suitable.

    Engineering Recommendations

    • Stabilize system prompts and prefixes: Place fixed instructions at the beginning of requests and minimize changes to the variable portion to improve cache hit rates.
    • Control conversation history length: Summarize or truncate multi-turn histories to prevent input token usage from growing indefinitely.
    • Limit output length: Use clear formatting requirements and maximum output limits to reduce the share of output tokens.
    • Monitor costs continuously: Track the usage of all three token categories by module and periodically recalculate costs with the model above.
    • Review billing rules regularly: Pricing and subscription policies can change. Keep the cost model parameterized so that it can be updated quickly.

    Conclusion

    The cost of a large language model API cannot be determined from the price table alone. Cache hit rates, the input-to-output token structure, and billing periods all contribute to the final bill.

    By breaking costs into measurable variables and calculating effective prices using a consistent baseline, teams can make better-informed decisions between pay-as-you-go billing and subscription plans. The same analysis also provides quantitative guidance for cache optimization and context management.