How to Calculate the Real Cost of LLM Inference: A Practical Guide

    Comparing per-million-token prices is not a reliable way to choose an LLM inference service. This guide breaks real inference cost into a three-part model (cache hits, uncached input, output), shows how the break-even cache hit rate determines which option is cheaper, explains how to convert subscription plans into comparable unit prices, and quantifies peak-hour pricing with a time-weighting factor. It also includes a runnable cost script and a practical optimization checklist covering stable prefixes, output length control, tiered routing, observability, and elastic capacity. Use these four variables to make model and pricing decisions based on data rather than impressions.

    1. Why “Unit Price Comparisons” Often Lead to Wrong Conclusions

    When evaluating LLM inference services, the most common approach is to place the “price per million tokens” of two options side by side. This is not sound from an engineering perspective, for three reasons:

    1. Billing is multi-dimensional. A single request’s bill is typically made up of three parts: the cache-hit portion, the uncached input portion, and the output portion. The unit prices of these three can differ by an order of magnitude. Comparing only one of them will inevitably distort the conclusion.
    2. The real workload mix is a variable. The cache hit rate and the input-to-output ratio fluctuate dramatically with the type of business. Scenarios with long contexts and repeated prefixes can approach saturation in hit rate, while multi-turn, divergent conversations may see very low hit rates.
    3. The time dimension is ignored. Many services use differentiated peak/off-peak pricing, and the length and position of the peak window directly determine the monthly bill.

    The correct approach, therefore, is to build a cost model rather than rely on memorized unit prices.

    2. The Three-Part Breakdown of the Cost Model

    Let the total number of tokens for a call be T, the cache hit rate be h, and the output share be o. Then:

    • Cache-hit portion: T * h, billed at the cache unit price p_cache;
    • Uncached input portion: T * (1 - h - o), billed at the input unit price p_in;
    • Output portion: T * o, billed at the output unit price p_out.

    The cost per token is:

    1. cost_per_token = h * p_cache + (1 - h - o) * p_in + o * p_out

    Note an easily overlooked constraint: h + o <= 1. When the output share is high, the proportion of input available for caching is squeezed, and the cost naturally rises.

    3. Hit-Rate Sensitivity: The Crossover Point Determines the Choice

    Treating h as the independent variable, the cost function is a straight line in h, with a slope determined by (p_cache - p_in). Since the cache unit price is usually significantly lower than the uncached input unit price, this line slopes downward. The lines of two options must have a crossover point:

    • When the actual hit rate is above the crossover point, the option with the lower cache unit price is actually more expensive;
    • When the actual hit rate is below the crossover point, the option with the lower cache unit price starts to win.

    This is counterintuitive, yet fully consistent with the formula. The engineering conclusion is clear: do not rank by the advertised cache unit price; rank by the hit-rate range measured on your own business. It is recommended to first sample small traffic for a week, compute the P50 and P5 percentiles of h, and then plug them into the model.

    An illustrative calculation script:

    1. def cost_per_million(h, o, p_cache, p_in, p_out):
    2. assert 0 <= o <= 1 and 0 <= h <= 1 - o
    3. return 1e6 * (h * p_cache + (1 - h - o) * p_in + o * p_out)
    4. for h in (0.98, 0.95, 0.90, 0.80):
    5. print(h, cost_per_million(h, 0.005, 0.115, 8.0, 30.0))

    4. Converting “Plans” into Comparable Unit Prices

    Evaluating a subscription coding plan should not focus on the monthly fee, but on “how many tokens you get per dollar.” The conversion steps:

    1. Take the official monthly/weekly token allowance Q (note the difference between a weekly and a monthly allowance; a weekly allowance should be multiplied by about 4.3);
    2. Compute the nominal unit price p_plan = monthly fee / Q;
    3. Compute the original pay-as-you-go price for the same allowance, p_api * Q, to get the discount rate p_plan / (p_api * Q).

    In practice, the plan discount rate often falls in the 50%–70% range, equivalent to 50%–70% off pay-as-you-go. But two correction terms must be added:

    • Peak-window discount: if the peak period is a few hours each workday, then roughly 20 hours of the weekly allowance are constrained, and this must be weighted by the time distribution of your business requests;
    • Allowance waste rate: unused plan allowance is sunk, so the effective unit price should be calculated as monthly fee / actual consumption, not by the nominal allowance.

    5. Quantifying the Peak Window

    Let a week have 168 hours total, the peak window be H hours per week, the share of business requests falling within the peak be r, and the peak price be k times the off-peak price. The time-weighting factor is:

    1. w = (1 - r) + r * k

    The smaller H is and the lower r is, the closer w is to 1. For coding scenarios dominated by human interaction, request times are relatively spread out and r is usually not high, so a shorter peak window has limited impact on the bill. For scheduled batch jobs, if the schedule happens to fall within the peak, w will significantly amplify the cost. Shifting batch jobs to off-peak windows via a message queue for staggered scheduling is one of the highest-ROI actions in cost optimization.

    6. An Actionable Optimization Checklist

    • Stable prefixes: place invariant content such as system prompts, tool definitions, and long documents at the front and keep it byte-for-byte stable, avoiding any timestamps or random IDs that break prefix consistency. This is the number one factor for improving the hit rate.
    • Control output length: the output unit price is usually several times the input price, so limiting redundant explanations reduces cost linearly.
    • Tiered routing: send simple requests to a lightweight model and complex requests to a large-parameter model, attributing cost by label with a monitoring and alerting system.
    • Observability first: log h, input/output tokens, and time-slot labels for each call in your logging service. Without measurement there is no optimization.
    • Reserve elasticity: handle burst traffic with pay-as-you-go and cover the stable baseline with a plan, avoiding paying long-term for peak capacity.

    7. Conclusion

    Real cost = three-part unit price model × measured hit rate × time-weighting factor ÷ allowance utilization. Any unit price comparison that departs from these four variables has no decision-making value. It is recommended that teams wire the above script into CI or a cost dashboard, recompute the crossover point whenever the model or pricing changes, and make selection decisions with data rather than impressions.