A Practical Guide to Estimating LLM API Costs with Blended Pricing

    LLM providers charge separately for cached input, uncached input, and output tokens, so comparing models by a single unit price can be misleading. This article explains how to compute a blended (weighted average) price based on your own traffic composition, provides a Python implementation, walks through a worked example showing the impact of cache hit rates, and covers key factors and engineering practices for accurate cost estimation and model selection.

    A Practical Guide to Estimating LLM API Costs with Blended Pricing

    1. Why the Price List Alone Is Not Enough

    LLM services typically bill by token, but price lists are split across multiple dimensions: the read price for cache hits, the input price for cache misses, and the output price. The proportions of these three categories vary widely across workloads. Applications built around long system prompts and multi-turn conversations that reuse context may see a high share of cached input. Content-generation applications, by contrast, tend to have a much higher share of output tokens. Comparing models using any single unit price can lead to conclusions that contradict your actual bill.

    A more reliable approach is to first measure your own traffic composition, then compute a weighted average to get a blended price, and use that as a consistent basis for comparing models.

    2. Building the Calculation Model

    Let the token counts in a billing period be:

    • C: cached input tokens
    • I: uncached input tokens
    • O: output tokens

    The corresponding unit prices are p_c, p_i, and p_o, in currency units per million tokens. The total cost for the period is:

    Total cost = C × p_c + I × p_i + O × p_o

    The blended price is the total cost divided by all tokens:

    Blended price = Total cost / (C + I + O)

    There is a common pitfall here. The denominator must include all three token categories, including cached input. If you divide only by uncached input plus output, the blended price will be artificially inflated.

    Equivalently, you can compute the share of each token category and then apply the weights. Let r_c, r_i, and r_o be the proportions of the three categories:

    Blended price = r_c × p_c + r_i × p_i + r_o × p_o

    Both formulas produce the same result; which one you use depends on which fields are more convenient to extract from your logs.

    3. Implementing the Calculation in Code

    Usage data usually comes from server-side logs or the usage fields in API responses. Field names differ across providers, so map them to a unified structure according to the official documentation of each provider.

    1. from dataclasses import dataclass
    2. @dataclass
    3. class Pricing:
    4. cache_read: float # cached input price, per million tokens
    5. input: float # uncached input price, per million tokens
    6. output: float # output price, per million tokens
    7. @dataclass
    8. class Usage:
    9. cached_input: int # cached input tokens
    10. uncached_input: int # uncached input tokens
    11. output: int # output tokens
    12. def blended_price(usage: Usage, price: Pricing) -> float:
    13. total = usage.cached_input + usage.uncached_input + usage.output
    14. if total == 0:
    15. raise ValueError("No token records in the period")
    16. cost = (usage.cached_input * price.cache_read
    17. + usage.uncached_input * price.input
    18. + usage.output * price.output) / 1_000_000
    19. return cost / total * 1_000_000

    The final step converts the result back to per-million-token units so it stays consistent with the price list.

    4. A Worked Example and Sensitivity Analysis

    Suppose an application records the following traffic in one period: 6 million cached input tokens, 3 million uncached input tokens, and 1 million output tokens. The following prices are hypothetical values for demonstration only and do not represent any real pricing:

    • Cached input: 1 per million tokens
    • Uncached input: 4 per million tokens
    • Output: 16 per million tokens

    The three cost components are 6, 12, and 16, totaling 34. With 10 million tokens in total, the blended price is 3.4 per million tokens.

    Now consider what happens when the cache hit rate drops. Keeping the total volume the same, if cached input falls to 2 million tokens while uncached input rises to 7 million, the costs become 2, 28, and 16, totaling 46. The blended price climbs to 4.6 per million tokens — an increase of roughly 35%.

    This shows that the cache hit rate is one of the key variables affecting the blended price. Before comparing models, you should establish what cache hit rate your own workload actually achieves.

    5. Factors That Must Be Included in the Calculation

    1. Time-based pricing. Some providers charge different rates during peak and off-peak hours. Bucket requests by actual call time, compute each bucket separately, and then aggregate.

    2. Long-context tiers. When pricing is tiered by input length, count each request according to the tier it actually falls into. Do not use a single tier to represent all traffic.

    3. Intermediate reasoning tokens. Some models generate intermediate tokens that never appear in the visible response but are still billed. Calibrate this usage against the provider’s billing records rather than estimating it from the displayed output length.

    4. Cache hit rate volatility. Cache effectiveness depends on whether the prompt prefix is stable, the interval between requests, and the cache expiration policy. Frequent changes to the system prompt will steadily degrade the hit rate.

    5. Failed and retried requests. Failed requests that were still billed, along with retries, must be included in the statistics. Otherwise the blended price will be underestimated.

    6. Engineering Best Practices

    • Track usage by business scenario. Different scenarios have different traffic compositions. Applying a global average to every scenario usually produces distorted results.
    • Recalculate regularly. Recompute the blended price weekly, or whenever a new prompt version goes live, and watch how it trends.
    • Externalize pricing configuration. Store pricing in a configuration center or database rather than hardcoding it, so price changes can be applied promptly.
    • Reconcile against bills. Compare your computed total cost against the provider’s bill and trigger an alert when the deviation exceeds a defined threshold.
    • Keep request prefixes stable. Place unchanged system instructions and examples at the beginning of the prompt and dynamic content afterward — this helps improve the cache hit rate.

    7. Conclusion

    The blended price combines the three pricing dimensions — cached input, uncached input, and output — into a single comparable figure, making it well suited for cost estimation and the first round of model selection. However, the result depends heavily on traffic composition: the same model can have very different blended prices across different workloads. Final selection should also factor in output quality, response latency, and service reliability, and should be validated against actual billing data through regular reconciliation.