LLM Inference Cost Modeling and Caching Optimization in Practice

    This article explains why comparing per-million-token prices often leads to wrong conclusions about LLM inference costs, and how to build a reusable cost model instead. It covers the blended pricing formula, why cache hit rate is the most sensitive variable, how output ratio amplifies costs in coding and agent workloads, off-peak pricing and plan-to-API credit conversion, and a practical checklist for collecting real traffic ratios, monitoring hit rates, and finding the crossover point where provider rankings flip.

    1. Why Per-Token Price Comparisons Almost Always Lead to the Wrong Conclusion

    When evaluating LLM inference services, the most common approach is to put the “price per million tokens” of two options side by side and pick the smaller number. The flaw in this approach is that a real bill is not determined by a single unit price. It is determined by a weighted combination of multiple billing components, which include at least four variables:

    1. The price of the cache-hit portion (usually significantly lower than the regular input price);
    2. The price of the uncached input portion;
    3. The price of the output portion (usually significantly higher than the input price);
    4. Time-based dynamic pricing (peak and off-peak hours are priced differently).

    If the proportion of any one of these changes, the ranking of two options can flip. The right approach is therefore not to compare unit prices, but to build a cost model and then run sensitivity analysis on the key variables.

    2. Building a Reusable Cost Model

    Let the total number of tokens processed be N, the cache hit rate be h, the uncached input ratio be i, and the output ratio be o. Then:

    1. Total cost per request = N × [ h × P_cache + i × P_in + o × P_out ]
    2. Average blended unit price = h × P_cache + i × P_in + o × P_out

    Here P_cache, P_in, and P_out are the unit prices for cache hits, regular input, and output respectively. In practice, it is a good idea to turn this logic into a function so you can compare multiple options in bulk:

    1. def blended_price(p_cache, p_in, p_out, hit_rate, in_ratio, out_ratio):
    2. return hit_rate * p_cache + in_ratio * p_in + out_ratio * p_out
    3. # Example: batch summarization with a high hit rate and a very low output ratio
    4. print(blended_price(0.115, 12.0, 60.0, 0.98, 0.015, 0.005))

    There is one detail here that is very easy to overlook: the input ratio and the output ratio do not add up to 1, because the vast majority of tokens are absorbed by cache hits. Many incorrect cost comparisons come precisely from forcibly normalizing i + o to 100%, which erases the cache benefit entirely.

    3. Cache Hit Rate Is the Most Sensitive Variable

    Dropping the hit rate from 98% to 95% looks like a difference of only 3 percentage points, but the impact on the bill can far exceed expectations. The reason is that there is a several-fold price gap between the cache price and the regular input price. For every 1 percentage point lost in the hit portion, those tokens are re-billed at the regular input price, and the cost increase equals 1% × N × (P_in − P_cache).

    This leads to two important conclusions:

    • When two options have a small cache price gap, the ranking is decided by the hit rate. When the hit rate is very high, the cache price gap is amplified into the dominant term, and the option with the cheaper cache wins. When the hit rate falls back into the low-to-medium range, regular input and output prices dominate again, and the ranking can flip.
    • The hit rate is not a constant; it is a function of workload characteristics. It depends on the stability of request prefixes, whether the system prompt is fixed, whether sessions are continuous, and how batch tasks are sharded.

    Common engineering practices for improving the hit rate include: placing fixed system instructions and tool descriptions at the front and keeping them stable; avoiding prepending volatile content such as timestamps or random IDs at the very beginning of the prompt; splitting long documents into reusable stable prefixes; and appending to the prefix rather than rewriting it across multi-turn conversations.

    4. Output Ratio Is an Underestimated Cost Amplifier

    In coding and agent workloads, the output ratio is often much higher than in chat scenarios. Since the output unit price is usually several times the input price, a small increase in o gets amplified by that price multiple. Control measures include: constraining the structured output format, setting a reasonable maximum generation length, splitting long-text generation into multiple smaller, verifiable steps, and adding application-layer caching for reusable generation results.

    5. Off-Peak Pricing and Plan-to-Credit Conversion

    Some services distinguish between peak and off-peak hours, and vendors define “peak” very differently: some count only a few hours per day, while others cover the entire workday. This directly affects task scheduling strategy—moving batch, offline, and retryable tasks (such as log summarization, code repository indexing, and batch review) into off-peak windows is a zero-cost optimization.

    Subscription-style billing should be compared by converting it into “equivalent regular API credit value”:

    1. Plan cost-effectiveness = usable volume in the plan × corresponding regular unit price / monthly plan fee

    In practice, the effective unit price of such plans usually falls in the range of 50% to 70% of the regular API price. Whether it is worthwhile depends on whether your actual usage can fully consume the allowance. Teams with volatile usage and uncertain peaks are better off with pay-as-you-go; teams with stable and predictable usage are better off with a plan.

    6. Implementation Checklist

    • Collect the three ratios h, i, and o from one week of real traffic before making a selection;
    • Write the cost model as code and include it in CI so it recalculates automatically when prices or ratios change;
    • Set up monitoring and alerts for the hit rate; an abnormal drop in the hit rate usually means the prompt structure has been changed;
    • Build a “cache hit rate vs. blended unit price” curve to find the crossover point where the ranking of options flips;
    • Use logging services and monitoring/alerting systems for usage attribution, and break down costs by business line.

    Summary: The real cost of LLM inference is a weighted result of multiple variables. The cache hit rate, the input/output ratio, and time-of-day strategy together determine the final bill. Mastering the blended unit price formula and continuously calibrating its parameters with real traffic is more valuable for decision-making than any one-off unit price comparison.