LLM API Cost Estimation: A Guide to Cache-Weighted Blended Pricing

    This article explains how to compare LLM API costs using a blended price metric that weights three billing categories—cached input (read), uncached input, and output tokens—by their actual share of traffic. It covers the meaning of each billing unit, the weighted formula and how to derive weights from call logs, a worked example comparing three hypothetical models, caveats such as tiered and time-based pricing, a reference Python implementation, and practical ways to reduce overall cost by improving cache hit rates and routing tasks by complexity.

    1. Why a Single Unit Price Is Not Enough

    LLM providers typically publish several unit prices. Looking only at the input price ignores the discount applied to cache hits; looking only at the output price ignores the cost of large volumes of repeated input. The actual bill is the sum of each token category multiplied by its corresponding price, so when comparing options you need to merge these prices into a single comparable metric.

    The blended price is not an official billing rule but an estimation tool. It weights each billable item by its actual share in your workload and produces an average cost per million tokens, making it easier to do an initial ranking of candidate models.

    2. The Three Billing Units

    Tokens in a single model call generally fall into three categories:

    • Cached input (read): The request prefix matches previously processed content, so the server can reuse intermediate computation results. This category usually has the lowest unit price.
    • Uncached input: New input tokens that must be fully computed. The price sits in the middle.
    • Output: Tokens generated by the model. This is usually the most expensive category, because generation requires token-by-token inference.

    The exact prices for these three categories are subject to each provider’s official pricing documentation. This article assumes no specific prices; all numbers in the examples are fictional values used for demonstration only.

    3. The Blended Price Formula and Weight Design

    Blended price = 0.8 × read price + 0.1 × input price + 0.1 × output price

    The weights represent the traffic share of each token category in your workload. A read share of 0.8 fits scenarios such as multi-turn conversations, long system prompts, and RAG pipelines where the document prefix remains stable. Weights should come from statistics on your own call logs and should not be copied directly from examples.

    When computing weights, keep the following in mind:

    1. Weight by token count, not by request count. Long requests affect cost far more than short ones; counting requests underestimates the share of long requests.
    2. Cache-hit determination must match the server’s rules. If even one character of the prefix differs, the subsequent content typically cannot hit the cache.
    3. Weights need regular updates. When business scenarios, prompt structures, or user behavior change, the cache hit rate changes with them.

    Units must also be consistent. A common pricing unit is “per million tokens,” but some providers price per thousand tokens—convert before calculating. If different currencies are involved, unify the exchange rate and pricing basis first.

    4. A Worked Example

    Assume three models with the following prices (fictional values, for demonstration only, in cost per million tokens):

    Model Read Input Output
    Model A 0.30 1.00 2.00
    Model B 0.50 0.80 4.00
    Model C 0.10 2.50 6.00

    Applying the 0.8 / 0.1 / 0.1 weights:

    • Model A: 0.8 × 0.30 + 0.1 × 1.00 + 0.1 × 2.00 = 0.54
    • Model B: 0.8 × 0.50 + 0.1 × 0.80 + 0.1 × 4.00 = 0.88
    • Model C: 0.8 × 0.10 + 0.1 × 2.50 + 0.1 × 6.00 = 0.93

    Model C has the lowest read price, but its input and output prices are high, so its blended price ends up being the highest. This shows that the read price is only one factor, and changing the weights can change the conclusion.

    If the cache hit rate drops so that the read share falls to 0.5, with input and output shares adjusted to 0.3 and 0.2, the blended prices become 0.85, 1.29, and 2.00 respectively. The ranking stays the same, but the gap widens significantly. Therefore, you should run a sensitivity analysis on the weights before drawing selection conclusions.

    5. Caveats When Ranking and Comparing

    • Tiered pricing: Some providers adjust the price once request length exceeds a threshold. Compute each actual request length segment separately, then take the weighted average—do not use just one tier’s price.
    • Time-based pricing: Peak and off-peak prices may differ. Weight by the actual time-of-day distribution of your calls.
    • Unverified prices: Prices you cannot confirm should be marked as “unverified” and excluded from rankings, to avoid misleading decisions with estimated values.
    • Price is not total cost: The blended price only reflects the cost per token, not output quality, latency, or stability. If a cheaper model needs more turns to complete a task, the total cost may be higher.
    • Timeliness: Prices change over time, so a ranking only reflects the situation at a specific point in time.

    6. Reference Implementation

    The following code shows how to compute blended prices and rank candidates by cost. Weights and prices are passed as parameters so they can be replaced with real data.

    1. from dataclasses import dataclass
    2. @dataclass
    3. class PriceProfile:
    4. name: str
    5. read: float # cached input price, per million tokens
    6. inp: float # uncached input price, per million tokens
    7. out: float # output price, per million tokens
    8. def blended_price(p: PriceProfile,
    9. w_read: float = 0.8,
    10. w_in: float = 0.1,
    11. w_out: float = 0.1) -> float:
    12. total = w_read + w_in + w_out
    13. if abs(total - 1.0) > 1e-9:
    14. raise ValueError(f"Weights should sum to 1, got {total}")
    15. return w_read * p.read + w_in * p.inp + w_out * p.out
    16. def rank_by_cost(profiles: list[PriceProfile], **weights) -> list[tuple[str, float]]:
    17. scored = [(p.name, blended_price(p, **weights)) for p in profiles]
    18. return sorted(scored, key=lambda item: item[1])
    19. if __name__ == "__main__":
    20. candidates = [
    21. PriceProfile("Model A", read=0.30, inp=1.00, out=2.00),
    22. PriceProfile("Model B", read=0.50, inp=0.80, out=4.00),
    23. PriceProfile("Model C", read=0.10, inp=2.50, out=6.00),
    24. ]
    25. for name, price in rank_by_cost(candidates):
    26. print(f"{name}: {price:.4f} per million tokens")

    In practice, it is recommended to derive the weights from your call logs and to segment each request by length so that the corresponding price tier is applied to each segment.

    7. Ways to Reduce Overall Cost

    1. Improve the cache hit rate. Place stable system instructions and fixed reference material at the front of the request and variable content at the end, minimizing prefix changes.
    2. Control output length. Set a reasonable maximum output token limit and ask for structured, concise answers to avoid wasted tokens.
    3. Route tasks by complexity. Send simple tasks to cheaper models and reserve more capable models for complex tasks, validating the routing thresholds with an evaluation set.
    4. Build a usage dashboard. Track the token counts and costs of all three categories by model and by business scenario, and continuously refine the weights with real data.

    If you use model services from Baidu AI Cloud, refer to the billing rules and current prices published in its console, and plug that data into the method described above.

    8. Conclusion

    The blended price is well suited for the initial screening of candidate models, compressing multiple billing items into a single comparable number. A real selection decision still needs to incorporate output quality evaluation, latency requirements, and real business traffic. Only by comparing candidates with the same set of weights and the same underlying data will the conclusion be reliable.