The Real Cost of LLM API Usage: A Practical Calculation Guide

    A practical guide to estimating the real cost of LLM API usage beyond per-token list prices. It breaks the bill into cache hit rate, input-to-output ratio, and peak versus off-peak pricing, provides a reusable cost formula and Python function, compares two cache-hit scenarios where the ranking of options reverses, outlines engineering techniques for raising cache hit rates, and explains how to evaluate subscription-based coding plans against pay-as-you-go pricing.

    1. Why a Lower Unit Price Doesn’t Mean a Lower Bill

    When teams evaluate the cost of putting a large language model into production, the discussion often stops at “how much per million tokens.” But the actual bill is driven by several variables working together: cache hit rate, the ratio of input to output tokens, whether calls fall inside peak-hour windows, and whether billing is metered by usage or bundled into a plan. Comparing list prices alone easily leads to conclusions that the opposite of what real measurements show.

    A common pattern: one model’s cached-input price is roughly twice that of another, so it looks “more than twice as expensive.” Yet in high-cache-hit workloads, the end-to-end cost gap per 100 million tokens shrinks to around 30%. And if the cache hit rate drops by a few percentage points, the ranking can even flip. This shows that cost comparisons must be built on a consistent calculation basis.

    2. The Cost Model: Breaking the Bill into Three Computable Parts

    Let the cache hit rate be h, the input share be a, and the output share be b (with a + b = 1). Let the cached-input unit price be p_c, the non-cached input unit price be p_i, and the output unit price be p_o. The expected cost per unit of tokens is:

    1. cost = h * p_c + (1 - h) * p_i + b * p_o
    2. # The input part is normalized by a; the output part is counted by b

    A more robust engineering approach keeps the input share explicit:

    1. def cost_per_1e8(hit, in_ratio, out_ratio, p_cache, p_in, p_out):
    2. # Unit price: CNY per million tokens; returns: CNY per 100 million tokens
    3. per_m = hit * p_cache + (1 - hit) * p_in * in_ratio + p_out * out_ratio
    4. return per_m * 100

    The three variables differ greatly in sensitivity:

    • Cache hit rate: the biggest factor. When the hit rate drops from 98% to 95%, the non-cached input portion is amplified by roughly 2.4x, which is enough to change which of two options comes out ahead.
    • Input/output ratio: the output unit price is usually several times the input price, so a rising output share significantly pushes up total cost. Code-generation workloads tend to produce longer outputs, while chat-summarization workloads tend to have longer inputs.
    • Time of day: some platforms apply different pricing or rate-limiting during peak hours. The narrower the peak window, the more off-peak capacity is available.

    3. Calculation Results for Two Typical Scenarios

    Scenario A: cache hit rate of 98%, input 1.5%, output 0.5%. Here cost is almost entirely dominated by the cached-input price. The two options come to roughly 12.6 CNY and 9.4 CNY per 100 million tokens, so the lower cached-price option wins, but the gap is only about 30%, not the “double” shown on the price sheet.

    Scenario B: cache hit rate of 95%. The non-cached input cost is amplified, and the two options come to roughly 14.9 CNY and 18.3 CNY, so the ranking flips. Conclusion: the option with a higher cached-input price but a better input structure is actually more cost-effective in the low-hit range.

    A reusable rule of thumb: when a workload has poor cache stability (frequently changing long context, frequently rewritten system prompts), prioritize the non-cached input price; when a workload has a stable prefix (fixed system prompt, fixed knowledge-base chunks, batches of similar requests), the cached-input price carries more weight.

    4. Raising the Cache Hit Rate: Engineering Techniques

    The hit rate isn’t something the platform gives you; it’s determined by how you make calls. Common effective practices:

    1. Keep the prefix stable: put the system prompt, tool definitions, and few-shot examples at the very front and don’t dynamically concatenate them; move volatile fields like timestamps and random IDs to the end.
    2. Cache-friendly segmentation: split long documents into stable chunks and avoid reshuffling the entire request each time.
    3. Batch homogeneous requests: submit similar requests together to increase the chance of prefix reuse.
    4. Monitor the hit rate: bring the cache hit rate in as a first-class metric wired into logging and monitoring/alerting, with threshold-based alerts.
    1. [Stable prefix: system prompt + tool schema + examples] <- expected to hit
    2. [Semi-stable segment: knowledge-base chunks]
    3. [Volatile segment: user input, timestamps, session ID] <- not expected to hit

    5. How to Evaluate Subscription-Style Billing

    Beyond pay-as-you-go, another common model is a monthly subscription coding plan, offered in tiers (light / advanced / high-end) and distinguishing peak from off-peak quotas. The way to evaluate it is to convert the plan’s quota into an “equivalent list-price API amount” based on the usable volume for the month, then compute the ratio:

    1. Equivalent list price = monthly token quota × blended pay-as-you-go unit price
    2. Plan value ratio = equivalent list price / monthly plan fee

    Under a 98% cache hit rate and an off-peak basis, the three tiers’ equivalent list prices are about 160 CNY, 955 CNY, and 2229 CNY, against monthly fees of 118 CNY, 538 CNY, and 1078 CNY. That means the plan’s unit price lands roughly in the 50%–70% range of the pay-as-you-go list price. This implies that a plan’s value comes from “large quota + relaxed time windows,” not from the unit price itself; if actual usage is far below the quota, the plan ends up more expensive.

    Decision point: first use two weeks of real call logs to estimate monthly token volume and peak-hour share, then decide which tier fits. A peak window of only 4 hours per day and 5 days per week is easier to avoid than a longer window.

    6. Conclusion

    The correct order for cost evaluation is: first unify the basis (cache / input / output / time of day), then plug into the formula, and finally calibrate with a regression against real logs. The price sheet is only good for an initial screen; the hit rate and time of day are the variables that actually determine the bill. Plans suit stable, high-frequency workloads that can shift off-peak, while pay-as-you-go suits volatile workloads with unstable caching.