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

    Comparing per-token list prices is the most common mistake teams make when estimating LLM API costs. This guide shows why real spending depends on cache hit rate, input/output mix, and peak versus off-peak pricing, and how to build a reusable cost model to find the break-even point between pricing schemes. It also covers how peak-hour definitions affect scheduling, how to convert subscription plans into equivalent API credit, and practical steps for matching your billing structure to your actual traffic.

    1. Why Comparing Unit Prices Often Gives the Wrong Answer

    When evaluating LLM API costs, the most common mistake is comparing cache read prices directly. One platform quotes cache reads at 0.115 CNY per million tokens, while another quotes an off-peak rate of 0.05 CNY per million tokens. The first looks more than twice as expensive, but that conclusion only holds under the specific assumption of a very high cache hit rate. A real bill has four components: cache-hit input, cache-miss input, output, and the price multiplier for different time windows. Comparing only one of them means substituting a part for the whole.

    More importantly, the cache hit rate is not a property of the platform; it is a property of your workload. Scenarios with a fixed system prompt, stable tool definitions, and conversation context that keeps growing naturally see high hit rates. Scenarios that rewrite the prompt every turn or switch task templates frequently see hit rates drop sharply. Cost estimation therefore has to be built on your own traffic profile.

    2. Building a Reusable Cost Model

    Let the total token volume be T, the cache hit ratio be h, the cache-miss input ratio be a, and the output ratio be b, with h + a + b = 1. With cache read price P_c, input price P_i, and output price P_o, the unit cost formula is:

    1. def cost_per_token(h, a, b, p_c, p_i, p_o):
    2. assert abs(h + a + b - 1) < 1e-9
    3. return h * p_c + a * p_i + b * p_o
    4. # Cost per 100 million tokens = unit cost * 1e8

    In a high-cache-hit scenario (98% hit, 1.5% input, 0.5% output), the two pricing schemes cost roughly 12.57 CNY and 9.40 CNY per 100 million tokens, so the one with the lower cache price wins. When the hit rate falls back to 95%, the result flips to roughly 14.92 CNY and 18.25 CNY, and the scheme with the higher cache price but cheaper input and output turns out to be the better deal. The crossover point is the basis for the decision, not the unit price label.

    3. Peak and Off-Peak Windows: An Underrated Cost Lever

    Many platforms use time-of-day pricing, where off-peak rates can be a fraction of peak rates. The definition of “peak hours” then directly determines cost. If peak is limited to 4 hours per weekday, that is only 20 hours a week and everything else gets the discount. If peak covers the entire weekday, the schedulable window is squeezed into nights and weekends. For latency-insensitive work such as offline batch processing, log summarization, and code repository index building, shifting traffic wholesale into off-peak hours often pays off more than optimizing prompts.

    In practice, it helps to add a scheduling layer that tags every call with its time window:

    1. def price_factor(now, peak_windows):
    2. return 1.0 if any(s <= now.time() < e for s, e in peak_windows) else 0.6

    Combined with a queue to smooth out peaks, deferrable requests can be held and released in bulk during off-peak hours.

    4. Subscription Plans: Equivalent Tokens and Discount Ranges

    To compare a monthly subscription plan, you have to convert it into an “equivalent list-price API credit.” Take a coding plan as an example: the three monthly tiers are roughly 118, 538, and 1078 CNY. Under a high cache hit rate and entirely off-peak usage, they correspond to about 317 million, 1.9 billion, and 4.433 billion tokens per week, which converts to roughly 160, 955, and 2229 CNY of list-price API usage. That puts the plan’s effective unit price at roughly 50% to 70% of list price.

    Three rules follow. First, the discount improves as you move up tiers, so heavy users should prefer the higher tiers. Second, plan quotas are settled weekly, so anything unused is a sunk cost, and you need to estimate average weekly consumption first. Third, plans usually come with peak-hour restrictions and revert to list price once you exceed them, so “nominal quota” and “usable quota” must be accounted for separately.

    5. Practical Recommendations

    Start by collecting real traffic for one week to measure the hit rate, the input/output split, and the time-of-day distribution, then plug those numbers into the model to find the crossover point. Keep list-price API as a fallback for online paths with strict stability requirements, and run batch pipelines on plans and off-peak windows. At the same time, raise the hit rate by trimming the system prompt, reusing tool definitions, and pruning historical context. The essence of cost optimization is making the billing structure match the traffic structure, rather than chasing a single quoted price.