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

    Comparing cached input prices across vendors often leads to wrong conclusions. This guide explains how to model LLM API cost using cache hit rate, input and output token ratios, and peak-hour pricing, then use break-even analysis to decide between pay-as-you-go and subscription plans. You will find a reusable measurement workflow for tracking real usage, locating the crossover point between candidate options, and scheduling offline workloads for discounts.

    1. Why Comparing Unit Prices Often Gives the Wrong Answer

    When evaluating LLM API costs, the most common mistake is comparing “cache hit prices” from different vendors side by side and concluding that “Option A is twice as expensive as Option B.” This comparison is unreliable because it compresses a multi-variable problem into a single-variable one.

    A real bill is usually made up of three types of tokens: input tokens that hit the cache, input tokens that miss the cache, and output tokens generated by the model. Their unit prices often differ by an order of magnitude, so the main driver of cost is not any single unit price, but the ratio structure among the three.

    Let:

    • p_cache be the unit price of a cached input token
    • p_in be the unit price of an uncached input token
    • p_out be the unit price of an output token
    • h be the cache hit rate, r_in the share of input tokens in total tokens, and r_out the share of output tokens

    The expected cost per unit token is:

    1. cost = h * r_in * p_cache
    2. + (1 - h) * r_in * p_in
    3. + r_out * p_out

    This formula shows that the cache price only has a decisive impact when h is high enough. Once the hit rate drops, the uncached input term grows quickly, wiping out the cache price advantage completely and even turning it into a disadvantage.

    2. Hit Rate Sensitivity: The Crossover Point Is What Matters

    The two sets of figures in the original calculation form a typical “crossover” pattern: at a 98% hit rate, Option A costs about 12.57 CNY per 100 million tokens and Option B about 9.40 CNY, so B is better; at a 95% hit rate, Option A costs about 14.92 CNY and Option B about 18.25 CNY, so A is better instead.

    The engineering implication is: the two cost curves intersect, and the choice depends entirely on which side of that intersection your workload falls on. A drop in hit rate from 98% to 95% looks like only 3 percentage points, but because the uncached input price is usually more than ten times the cache price, those 3 points can add more than 20% to the cost.

    So the right approach is not to copy someone else’s hit rate assumption, but to measure the hit rate distribution of your own workload first:

    1. Measure the token share of fixed prefixes such as the system prompt, tool definitions, and few-shot examples;
    2. Measure the proportion of historical messages reused in multi-turn conversations;
    3. Separate the “stable prefix” from the “content that changes every turn,” since the latter is the main killer of hit rates.

    One often-overlooked detail: if anything in the request prefix changes at the byte level, the cache may be invalidated. Putting timestamps, random IDs, or retrieval results before the system prompt turns parts that could have hit into misses.

    3. Factoring Peak Hours into the Cost Model

    Beyond token structure, the time dimension also affects spending. The industry commonly uses differentiated peak and off-peak pricing: prices rise during peak hours and discounts apply off-peak. Time windows vary widely across platforms—some have peak windows lasting several hours and covering the whole workday, while others cover only about four hours on weekday afternoons.

    The narrower the window, the more room there is to schedule around it. For offline batch tasks, shifting the entire task queue into the off-peak window can often secure a meaningful discount without changing a single line of model code. Practical engineering options include:

    • Using a message queue to absorb bursts, with consumers processing them in the off-peak window;
    • Setting up delay queues for less time-sensitive scenarios (log summarization, batch labeling, offline evaluation);
    • Watching time-based call curves through monitoring and alerts to keep tuning the scheduling strategy.

    Illustrative scheduling logic:

    1. def should_delay(now, peak_windows):
    2. """Release offline tasks only in off-peak windows"""
    3. return not any(start <= now.hour < end
    4. for start, end in peak_windows)

    4. Converting Subscription Plans to Pay-as-You-Go Equivalents

    Subscription coding plans charge a fixed monthly fee in exchange for a token allowance cap. To judge whether one is worth it, convert the allowance into an “equivalent list-price API amount” and then compute the discount rate:

    1. equivalent list price = monthly fee / discount rate
    2. discount rate = monthly fee / (weekly allowance * 4.33 * list unit price)

    In the original calculation, under an off-peak and 98% hit rate assumption, the three plan tiers had equivalent list prices of about 160 CNY, 955 CNY, and 2,229 CNY, against monthly fees of 118 CNY, 538 CNY, and 1,078 CNY, with discount rates in the 50%–70% range.

    This range reveals the essence of subscription plans: vendors trade discounts for predictable load and controllable timing. The price is that unused allowance is non-refundable, peak hours may be rate-limited, and usage above the cap returns to pay-as-you-go billing. Plans therefore suit teams with stable, predictable call volume; for spiky workloads with sparse peaks, pay-as-you-go is often more economical.

    5. A Reusable Calculation Workflow

    1. Instrument your system to collect the real h, r_in, and r_out metrics over at least one full business cycle;
    2. Plug them into the expected cost formula to compute the cost per 100 million tokens for each candidate option;
    3. Solve for the crossover hit rate of the two cost curves and determine which side you are on;
    4. Add the time-window discount factor and assess how much work can be shifted;
    5. Run a break-even analysis between the plan discount rate and pay-as-you-go cost to size the allowance;
    6. Keep regressing after launch, and re-evaluate vendors whenever the hit rate drifts past a threshold.

    In practice at Baidu AI Cloud, this model can be combined with general capabilities such as monitoring and alerts, logging, message queues, and object storage to form a “collect–calculate–schedule–review” loop, so cost optimization rests on verifiable data rather than intuition from a single quoted price.