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

    Comparing LLM API prices by unit rate alone almost always leads to wrong conclusions, because prompt caching creates a large gap between nominal and effective pricing. This guide breaks down a four-part billing model, turns it into a reusable cost function, and shows how cache hit rate, input/output ratio, and time-of-day windows determine actual spend. It also explains how to convert subscription plans into equivalent per-token prices and ends with a practical selection checklist for budgeting at scale.

    1. Why Unit-Price Comparisons Almost Always Get It Wrong

    When comparing LLM services, the most common approach is to divide one unit price by another and conclude that a service is “twice as expensive” or “half the price.” In engineering practice, this almost inevitably distorts reality, because cache-based billing creates an order-of-magnitude gap between the nominal unit price and the effective unit price.

    Mainstream inference services typically split billing into four parts: cached input, uncached input, output, and optional storage or session add-on fees. The cached-input rate is often one tenth of the uncached input rate or lower. So when the hit rate is high, cost is dominated by the cache price; when the hit rate drops by a few percentage points, the high uncached input rate rapidly amplifies total spend.

    The first conclusion follows directly: any cost comparison must be anchored to three premises — hit rate, input/output ratio, and time window. A unit-price comparison stripped of those premises has no decision value.

    2. The Cost Model: Writing the Bill as a Function

    Let total tokens be N, cache hit rate be h, input share be a, output share be b (a + b = 1), and the cached input, uncached input, and output rates be p_c, p_i, and p_o. Unit cost can then be written as:

    1. def cost_per_1e8(n=1e8, h=0.98, a=0.015, b=0.005,
    2. p_c=0.115, p_i=4.0, p_o=12.0):
    3. """Return the cost per 100 million tokens (CNY); rates are CNY per million tokens"""
    4. hit = n * h * p_c / 1e6
    5. miss = n * a * (1 - h) * p_i / 1e6
    6. out = n * b * p_o / 1e6
    7. return hit + miss + out

    The value of this function is that it turns “feels expensive” into “recomputable.” Take a service with a cache price of 0.115 CNY per million tokens: at a 98% hit rate, 1.5% input, and 0.5% output, the cache-side spend per 100 million tokens is about 11.27 CNY, and with a small amount of uncached and output cost the total lands around 12 CNY. Drop the hit rate to 95% and the uncached input term jumps from roughly 0.03 CNY to over 0.2 CNY, pushing the total to somewhere near 15 CNY.

    Run the same substitution for a service with a cache price of 0.05 CNY per million tokens: at a 98% hit rate it comes to just over 9 CNY, but at a 95% hit rate it actually rises to around 18 CNY. In other words, a low cache price does not mean a low total cost — it places more demanding requirements on the hit rate.

    3. Hit Rate: The Real Inflection Point of the Cost Curve

    Hit rate is not a marketing parameter but an engineering metric determined by the shape of your workload. Prefixes that can be reused stably include system prompts, tool and function definitions, few-shot examples, and long document bodies. Suffixes that change on every request include the user’s current-turn input, timestamps, and random identifiers.

    Practical recommendations:

    • Put stable content first and variable content last; avoid inserting volatile fields such as timestamps at the start of a prompt.
    • Control session length and truncation strategy so the prefix is not accidentally rewritten and the cache invalidated.
    • Use hit rate, not request volume, as the core monitoring metric for capacity and budget.

    In practice, templated workloads, batch summarization, and code completion can easily exceed 95%; open-ended multi-turn conversations usually sit in the 80%–90% range. When estimating, plug in measured values from your own workload rather than optimistic ones.

    4. Time Windows and Plans: Converting a Monthly Fee into Equivalent Tokens

    Many services apply different policies during peak hours, and the length of the peak window directly determines the real value of a plan. Let the monthly plan fee be M and the off-peak weekly token allowance be Q. Converted over four weeks, the equivalent unit price is:

    1. def plan_unit_price(month_fee, weekly_tokens, weeks=4.345):
    2. return month_fee / (weekly_tokens * weeks / 1e6) # CNY per million tokens

    A plan at 118 CNY per month with roughly 317 million tokens per week works out to about 8.6 CNY per million tokens; 538 CNY per month with roughly 1.9 billion tokens per week is about 6.5 CNY; 1078 CNY per month with roughly 4.433 billion tokens per week is about 5.6 CNY. Against pay-as-you-go equivalents of 160 CNY, 955 CNY, and 2229 CNY, the plans land at roughly 50% to 70% of list price, with higher tiers discounting more clearly.

    One caveat: plan token allowances are usually quoted on an off-peak basis. Peak windows — for example a four-hour daily window such as 14:00–18:00 on weekdays — consume extra quota or trigger throttling, so real usable volume should be discounted again according to your team’s actual call-time distribution.

    5. Selection Checklist

    1. Measure the hit rate before discussing unit price: replay one week of real traffic to get the distribution of h, not just the average.
    2. Recompute side by side with one shared script: fix N, a, and b, vary only the rates and h, and output cost per 100 million tokens.
    3. Prefer a plan when peak-hour traffic is under 20%; above 40%, pay-as-you-go is more controllable.
    4. Bring cache hit rate, timeout rate, and cost per token into monitoring and alerting together.
    5. Build your own cost dashboard on top of general-purpose model services plus monitoring, alerting, and logging capabilities from platforms such as Baidu AI Cloud, so decisions are not driven by a single price sheet.

    The conclusion is clear: real cost is determined by hit rate and time-window structure — unit price is only the entry ticket. Script the estimation and engineer the hit rate, and you can hold the budget line when LLMs go into production at scale.