Calculating LLM API Costs: Cache Hit Rates and Subscription Plan Selection

    Evaluating the true cost of Large Language Model (LLM) APIs goes beyond checking list prices. Factors like context caching and varying cache hit rates can cause actual expenses to differ significantly. This article explains how to calculate real API costs using a weighted formula, compares subscription plans with pay-as-you-go pricing, and outlines practical steps for accurate cost estimation.

    The real cost of LLM APIs is difficult to judge solely by their list price. Many services offer context caching, where repeated prefixes are billed at a lower rate. With the same unit price, actual spending can vary by several folds depending on the cache hit rate and the input-to-output ratio. This article explains how to calculate real costs, how to choose between subscription plans and pay-as-you-go billing, and which details are easily overlooked.

    Why Looking Only at List Prices Leads to Wrong Choices

    Two main reasons distort list prices.

    First, the cache hit rate is the most significant variable. Using the following example prices: cache price at 0.115 RMB/million tokens, standard input at 1 RMB/million tokens, and output at 4 RMB/million tokens. When the hit rate increases from 90% to 98%, the unit cost drops from approximately 0.2185 RMB/million tokens to 0.1477 RMB/million tokens, a decrease of about 32%.

    Second, the hit rate varies greatly across different business scenarios. Long conversational coding assistants and Q&A systems based on a fixed knowledge base typically have high hit rates. Conversely, tasks like batch data processing, where entirely new content is inputted each time, have a hit rate close to zero.

    Therefore, comparing only list prices without considering your business’s hit rate often leads to unreliable conclusions.

    Weighted Cost Formula

    Calculating the real cost requires three proportional parameters, all based on the total token count:

    • h: Cache hit rate, where the hit portion is billed at the cache price
    • i: Ratio of uncached standard input
    • o: Output ratio, which typically does not use caching and is billed at the output price

    These three satisfy h + i + o = 1. The weighted cost per million tokens is:

    1. Unit Price (RMB/million tokens) = h × cache_price + i × input_price + o × output_price

    The prices below are used solely to illustrate the calculation method; actual prices depend on each vendor’s official documentation. For easier comparison, the example assumes two vendors have the same standard input and output prices, differing only in cache prices.

    Scenario Deductions

    Scenario A: High Hit Rate (h=98%, i=1.5%, o=0.5%)

    This is close to the workload of a coding assistant. Most inputs are repeated system prompts and code context.

    • Vendor 1 (cache price 0.115 RMB): 0.98×0.115 + 0.015×1 + 0.005×4 = 0.1477 RMB/million tokens, or about 14.77 RMB per 100 million tokens.
    • Vendor 2 (off-peak cache price 0.05 RMB): 0.98×0.05 + 0.015×1 + 0.005×4 = 0.084 RMB/million tokens, or about 8.40 RMB per 100 million tokens.

    Scenario B: Hit Rate Drops to 95% (h=95%, i=4.5%, o=0.5%)

    More new inputs are introduced into the business, and the hit rate drops.

    • Vendor 1: 0.95×0.115 + 0.045×1 + 0.005×4 = 0.17425 RMB/million tokens, or about 17.425 RMB per 100 million tokens.
    • Vendor 2: 0.95×0.05 + 0.045×1 + 0.005×4 = 0.1125 RMB/million tokens, or about 11.25 RMB per 100 million tokens.

    In both scenarios, Vendor 2 is cheaper. The reason is straightforward: the two vendors differ only in cache price, and Vendor 2’s cache price is lower while other prices remain the same, so Vendor 2 has the advantage at any hit rate. The difference between the two equals h × 0.065 RMB/million tokens. When the hit rate drops from 98% to 95%, the difference narrows from about 6.37 RMB to about 6.18 RMB per 100 million tokens, narrowing the advantage but without a reversal.

    To reverse the conclusion, the standard input or output prices between the two vendors must differ. For example, if Vendor 2 has a higher standard input price, as the hit rate drops, its high input price will gradually offset the advantage of its low cache price. This also illustrates that before making a selection, you must use real data for calculation and cannot assume an ideal hit rate.

    Converting Subscription Plans vs. Pay-As-You-Go API

    Most vendors offer monthly subscription plans for developers. Plans are divided into tiers and include rate limiting or throttling during peak hours. When evaluating plans, the core is to compare the plan fee with the original API price for the same usage.

    The following table uses illustrative data and converts the monthly fee into a weekly fee based on 4 weeks per month.

    Tier Monthly Fee Weekly Converted Fee Weekly Original API Price Plan Fee as % of Original API Price
    Starter 118 RMB 29.5 RMB 160 RMB ~18%
    Pro 538 RMB 134.5 RMB 955 RMB ~14%
    Flagship 1078 RMB 269.5 RMB 2229 RMB ~12%

    The calculation method is: Plan fee ratio = Monthly Fee ÷ 4 ÷ Weekly Original API Price.

    If the numbers in the table hold true, the weekly plan fee is only 12% to 18% of the original API price, a gap far exceeding common discounts. This result cannot be directly trusted. The quotas and original API prices in the table are illustrative data. When converting, two things must be verified: whether the original API price is calculated using the same hit rate, and whether the quota matches the vendor’s actual definition.

    When evaluating plans, also pay attention to the following three points:

    1. Peak hour rate limit scope. Some plans only limit rates for a few hours a day, while others cover the entire working day. If your team codes concentratedly during peak hours, the actual available quota will shrink, and the equivalent cost will rise accordingly.
    2. Weekly quota reset. Weekly quotas cannot accumulate leftover weekday balance for weekend use. During pulse-like workloads such as concentrated release schedules, teams may frequently hit the cap.
    3. Hit rate assumptions. The token quota advertised by the plan is usually calculated based on a high hit rate. If your business has a lower hit rate, the actual available quota will decrease accordingly.

    Before signing, please verify the following fields: peak window start and end times, concurrent rate limit cap, and weekly quota reset time. These three items can be found in the vendor’s official documentation and will directly affect the calculation results.

    Implementation Advice: Three Steps to Calculate Cost

    ```text
    Step 1: Measure Baseline
    Extract the h / i / o distribution from the call logs for the past 30 days, as well as the time distribution of requests.
    It is recommended that logs include request_id, timestamp, input_tokens,
    cached_tokens, output_tokens fields.
    h = cached_tokens / total_tokens
    i = (input_tokens - cached_tokens) / total_tokens
    o = output_tokens / total_tokens
    Note: First confirm whether input_tokens already includes the cached part, as different vendors may have different field definitions.

    Step 2: Parallel Calculation
    Use the weighted formula to calculate respectively