LLM API Real Cost Calculation Guide

    Comparing per-million-token prices is the most common way to estimate LLM API costs, but it almost always produces the wrong answer. This guide breaks down the four token categories that actually drive a bill, the four variables a reliable cost model needs, a reusable Python script, and practical ways to measure cache hit rate, peak-hour weighting, plan discount rates, and hidden costs. It also explains why the cheaper option flips depending on your hit rate, and gives an action checklist for building your own cost curve.

    1. Why “Price Comparison” Almost Always Gets the Math Wrong

    When evaluating LLM API costs, the most common approach is to put two options’ “price per million tokens” side by side and declare a winner. In engineering practice, this almost always distorts the picture, because token billing is not a linear structure but a piecewise function built from several pricing dimensions stacked together.

    A single request’s tokens are usually split into four categories: the cache-hit portion, the cache-miss input portion, the output portion, and, in some billing systems, reasoning tokens priced separately. The unit prices of these four can differ by an order of magnitude, so what really determines the bill is not the sticker price but the share each category takes in total traffic.

    The pattern described in the original discussion illustrates this rule exactly: one platform’s cache price is more than twice another’s, making it more expensive under one hit-rate assumption; drop that assumption by a few percentage points and the conclusion reverses completely. Unit price is only the entry ticket — hit rate is the lever.

    2. The Four Variables of a Cost Model

    To get the math right, you need to pin down at least these four variables.

    1. Cache hit rate (hit_rate): the share of input tokens served from cache, usually determined by stable prefixes such as system prompts, long documents, and code repository context.
    2. Input-to-output ratio: agent workloads and code completion have a much higher output share than ordinary Q&A, and output price is often several times the input price, so shifts in this ratio move the total cost dramatically.
    3. Peak-hour multiplier: many platforms charge higher rates during specific weekday windows (for example, a few hours of daily concurrency peaks) and offer discounted rates the rest of the time, creating a “valley/peak” dual track.
    4. Plan conversion rate: monthly plans are typically converted into “equivalent list-price API credit,” and the lower the conversion rate, the lower the real cost per token.

    3. A Reusable Calculation Script

    The sample code below parameterizes the variables above so you can plug in your own real traffic profile:

    1. def cost_per_100m(hit, in_price, cache_price, out_price,
    2. in_ratio=0.015, out_ratio=0.005, peak=1.0):
    3. total = 1e8
    4. hit_tok = total * hit
    5. miss_tok = total * in_ratio - hit_tok
    6. out_tok = total * out_ratio
    7. return (hit_tok * cache_price + miss_tok * in_price
    8. + out_tok * out_price) * peak / 1e6

    Plugging in two typical assumption sets (cache price 0.115 vs 0.05 per million tokens, input 2, output 8): at a 98% hit rate, the two come out to roughly 12.6 and 9.4 per 100M tokens; drop the hit rate to 95% and the first is about 14.9 while the second climbs to about 18.3. The crossover sits near 95%, and that is the dividing line for the decision.

    4. Hit Rate Is the Undervalued Core Metric

    Hit rate is not a platform attribute; it is a product of application architecture. Ways to improve it include: placing the system prompt and tool descriptions at the very front of the context and keeping them byte-level stable; turning static content such as long documents and code indexes into reusable prefixes; avoiding volatile content such as timestamps or random IDs inserted mid-conversation; and appending incrementally across turns instead of rewriting history.

    In engineering terms, instrument your system to measure the actual hit rate rather than relying on an official assumption. These metrics work well for regression monitoring:

    1. hit_rate = cached_input_tokens / total_input_tokens
    2. effective_price = total_cost / (total_input_tokens + output_tokens)

    When effective_price deviates from expectations for a sustained period, it usually means prefix stability has been broken.

    5. Peak Windows and Plan Conversion Logic

    Differences in how peak windows are defined often affect the bill more than differences in unit price. If one platform’s peak is 4 hours per weekday and another’s covers a wider range, the annual cost gap widens under the same traffic distribution. When calculating, intersect a histogram of your request timing with the peak window to get the “share of tokens falling in peak hours,” then multiply by the peak multiplier.

    On the plan side, judge value with the conversion rate: divide the monthly plan fee by its claimed equivalent list-price API credit to get the discount rate. In the original data, three plan tiers land between 50% and 70%, meaning roughly 30% to 50% off list price; then factor in the weekly token cap to convert everything into a “weekly effective token price,” which is the only way to compare apples to apples with pay-as-you-go.

    6. Bringing Hidden Costs into the Model

    A real bill also includes parts that are easy to overlook: duplicate tokens from failed retries and timeout replays; increased cache-miss input caused by context bloat; queuing and degradation triggered by rate limits; and the overhead of supporting capabilities such as monitoring, logging, and alerting. Architecturally, set a maximum context length, a retry cap, and a degradation strategy, and build log sampling rates into the cost budget.

    7. Conclusion and Action Checklist

    There is no single answer: with a high and stable hit rate, the option with the lower cache price wins; with an average or volatile hit rate, the option with the lower input price is more economical. The actionable steps are: first measure your own hit rate and input-to-output ratio, then weight by peak-hour distribution, and finally compare pay-as-you-go against plans on a common basis to produce a cost curve of your own. If you use Baidu AI Cloud services, the same advice applies — let measured data drive the decision rather than the sticker price.