Calculating LLM API Costs: Cache Hit Rates and Pricing Plans

    Understanding LLM API costs requires more than just comparing base price lists. This article breaks down how to calculate actual expenses by analyzing cache hit rates, input/output ratios, and peak/off-peak pricing. It provides a practical cost model, a break-even analysis for pay-as-you-go versus subscription plans, and engineering best practices to effectively reduce LLM expenses.

    1. Why You Shouldn’t Just Compare Base Price Lists

    Large Language Model (LLM) services are typically priced per million tokens. However, actual expenditures depend on three components: cached input, uncached input, and output. Because the unit prices for these three categories differ significantly, looking at just one can lead to wrong conclusions. For instance, a service with a low cache price but a high output price might not be cheaper for output-heavy workloads.

    Billing is also affected by time periods, pricing plans, and billing metrics. The same usage volume can result in bills that differ by several times under different rules. Therefore, the correct way to compare costs is to first unify the calculation metrics and then calculate based on your actual usage structure.

    2. The Cost Model: Breaking Down the Bill

    Assume the total token volume over a period is 100%. The cached input share is h, the uncached input share is u, and the output share is o, where h + u + o = 100%. The unit prices for the three types of tokens are c, i, and x respectively, measured in currency units per million tokens.

    Since 100 million tokens equals 100 million tokens, the percentage value conveniently equals the corresponding number of million tokens. Thus, the cost per 100 million tokens can be expressed directly as:

    Cost per 100M tokens = h × c + u × i + o × x

    The corresponding calculation function is as follows:

    1. def cost_per_100m(hit_share, miss_input_share, output_share,
    2. price_hit, price_miss_input, price_output):
    3. # Shares are passed as percentage values, e.g., 98 for 98%
    4. # Unit prices are in currency units per million tokens
    5. return (hit_share * price_hit
    6. + miss_input_share * price_miss_input
    7. + output_share * price_output)

    The value of this model lies in turning the bill into a few independently observable variables. As long as you know your cache hit rate, input/output ratio, and the three unit prices published by the service provider, you can calculate the cost per 100 million tokens and compare different options.

    3. The Impact of Cache Hit Rates on Costs

    The unit prices below are hypothetical values used to demonstrate the calculation method and do not represent any real service quotes: cached input is 0.2 per million tokens, uncached input is 2.0, and output is 8.0.

    Cache Hit Rate Uncached Input Share Output Share Cost per 100M Tokens
    98% 1.5% 0.5% 26.60
    95% 4.5% 0.5% 32.00
    90% 9.5% 0.5% 41.00

    As shown in the table, for every 5 percentage point drop in the hit rate, the cost increases by approximately 20%. The cache hit rate is one of the most sensitive metrics affecting cost and should be included in routine monitoring.

    Additionally, sensitivity depends on the size of the cache discount. The smaller the ratio between the cache unit price and the uncached unit price, the greater the impact of hit rate fluctuations on costs. If the cache discount is very small, the cost difference caused by hit rate changes will also shrink accordingly.

    4. Impact of Peak and Off-Peak Hours on Average Price

    Some services adjust their pricing during specific time periods. Instead of using a single unit price for calculations, you should split usage by time period and calculate a weighted average:

    Weighted Price = Σ (Segment Usage Share × Segment Price)

    1. def weighted_price(segments):
    2. # segments is a list of tuples: [(usage_share, price), ...], sum of shares is 1
    3. return sum(share * price for share, price in segments)

    Assuming peak hours on weekdays account for 30% of usage, peak pricing is 20% higher than off-peak, and the base price calculated in the previous section is 26.60. Then:

    • Peak Price: 26.60 × 1.2 = 31.92
    • Weighted Price: 0.3 × 31.92 + 0.7 × 26.60 = 28.20

    The start and end times, time zones, and applicable dates for time periods must be based on the provider’s latest rules and should not be estimated based on past experience.

    5. Break-Even Analysis: Pay-As-You-Go vs. Subscription Plans

    Subscription plans have fixed costs but usually impose weekly or monthly quotas. To determine if a subscription is worth it, you first need to calculate the break-even usage:

    Break-Even Usage = Monthly Subscription Fee ÷ Average Price

    Continuing with the assumptions above, the average price is 26.60 per 100M tokens, or 0.266 per million tokens. If the monthly subscription fee is 100, then:

    100 ÷ 0.266 ≈ 376 million tokens, which is about 376 million tokens/month.

    1. def breakeven_million_tokens(monthly_fee, avg_price_per_million):
    2. # Returns the break-even usage in million tokens
    3. return monthly_fee / avg_price_per_million

    If your monthly usage is below this value, pay-as-you-go is more cost-effective. If it is above this value, a subscription plan is better—provided the plan’s quotas and rate limits meet your business needs. Three things to note during calculation:

    1. If the plan enforces a weekly quota, first convert it to a daily average and compare it with your business peak usage.
    2. Rate limits during peak hours may affect the availability of online services, so you cannot look at cost alone.
    3. Billing rules for off-peak and peak hours may differ; calculate them separately before summing them up.

    6. Engineering Practices to Reduce Costs

    1. Stabilize Prompt Prefixes. Many caching mechanisms rely on prefix matching. System prompts, examples, and fixed rules should be placed at the beginning of the request and kept byte-for-byte identical, while dynamic content should be placed at the end.
    2. Control Output Length. Output unit prices are typically higher than input unit prices. Setting a reasonable maximum number of output tokens and requiring structured output can directly reduce expenses.
    3. Streamline Context. Summarize historical conversations or use a sliding window to trim context, preventing irrelevant content from being repeatedly billed.