How to Calculate the Real Cost of Large Language Model APIs: Caching, Peak Pricing, and Plans
API list prices reveal only the cost of a billing unit. Actual spending also depends on the input-to-output ratio, cache hit rate, peak and off-peak usage, and whether a prepaid plan is fully utilized. This article provides a practical cost formula, compares pricing strategies at different cache hit rates, explains how to schedule workloads during cheaper periods, and shows when usage-based billing or subscription plans are more cost-effective.
1. Standardize the Billing Model First
The unit price shown in official documentation represents the price of a single billing unit. The actual cost of an API call depends on the ratio of input to output tokens, how many input tokens are served from the cache, which billing period the request falls into, and whether the business uses a prepaid or subscription plan.
This article uses yuan per million tokens as the pricing unit. One hundred million tokens equals 100 million-token units.
The cache hit rate is defined as:
Cache hit rate = cached input tokens ÷ total input tokens
The input-token ratio and output-token ratio must add up to 1. Under this definition, the cost can be calculated as follows:
def calculate_real_cost(total_tokens: float, # Total tokens, in millionscache_hit_rate: float, # Cached input tokens / input tokens, from 0 to 1input_ratio: float, # Input tokens / total tokensoutput_ratio: float, # Output tokens / total tokensprice_cache: float, # Price for cached input tokens, yuan per million tokensprice_input: float, # Price for uncached input tokens, yuan per million tokensprice_output: float, # Price for output tokens, yuan per million tokens) -> float:assert 0.0 <= cache_hit_rate <= 1.0assert abs(input_ratio + output_ratio - 1.0) < 1e-9input_tokens = total_tokens * input_ratiocache_tokens = input_tokens * cache_hit_rateuncached_input_tokens = input_tokens * (1 - cache_hit_rate)output_tokens = total_tokens * output_ratioreturn (cache_tokens * price_cache+ uncached_input_tokens * price_input+ output_tokens * price_output)
2. How Cache Hit Rate Changes the Result
The following example assumes that input tokens account for 90% of every 100 million tokens, while output tokens account for the remaining 10%. The prices below are illustrative assumptions and do not represent the pricing of any specific platform.
- Plan A: cached input at ¥0.115, uncached input at ¥1.2, output at ¥4.0
- Plan B (off-peak): cached input at ¥0.05, uncached input at ¥3.0, output at ¥4.0
Plan B’s cached-input price is only about 43% of Plan A’s, but its uncached-input price is significantly higher. The resulting costs are:
| Cache hit rate | Plan A (yuan per 100 million tokens) | Plan B (yuan per 100 million tokens) |
|---|---|---|
| 98% | 52.30 | 49.81 |
| 95% | 55.23 | 57.78 |
For Plan A at a 98% cache hit rate, the 90 input tokens consist of 88.2 cached tokens and 1.8 uncached tokens. Output accounts for 10 tokens. The total cost is:
88.2 × 0.115 + 1.8 × 1.2 + 10 × 4.0 ≈ ¥52.30
For Plan B at a 95% cache hit rate:
85.5 × 0.05 + 4.5 × 3.0 + 40 ≈ ¥57.78
At a 98% hit rate, Plan B is approximately ¥2.5 cheaper. When the hit rate falls to 95%, Plan B becomes approximately ¥2.5 more expensive. The break-even point is around 96.5%.
This means model selection should not focus solely on the cached-input price. First, measure the application’s actual cache hit rate. If the context changes frequently and a high hit rate cannot be maintained consistently, a plan with more balanced pricing may be more cost-effective.
3. Peak and Off-Peak Pricing
Some providers use different prices during peak and off-peak periods, but the definition of “peak” can vary considerably.
For example, one provider may classify all weekdays from 9:00 to 18:00 as peak hours. Another may define peak time as only 14:00 to 18:00 from Monday through Friday. Under the second definition, 20 hours per weekday, as well as the entire weekend, would qualify for off-peak pricing.
Offline data processing, overnight batch generation, and non-real-time RAG index construction can be deferred to off-peak periods through scheduled jobs or message queues. To support this approach, the architecture must decouple real-time requirements from scheduling windows. The business team should first define the latest acceptable completion time for each task type. The scheduling layer can then assign an appropriate execution window.
Before using peak and off-peak pricing, confirm:
- The exact definition of peak and off-peak periods.
- The time zone used for billing.
- Whether public holidays are treated as weekdays or weekends.
4. Comparing Subscription Plans with Usage-Based Billing
The key metric for evaluating a prepaid or subscription plan is its effective unit price:
Monthly plan fee ÷ actual tokens used, in millions
The lower the effective unit price, the more attractive the plan is.
For example, suppose a plan costs ¥500 per month and includes 1 billion tokens, or 1,000 million tokens. At a 98% cache hit rate using Plan A’s prices, consuming the full quota through usage-based billing would cost approximately ¥523, or about ¥0.523 per million tokens.
The plan’s effective price is:
¥500 ÷ 1,000 = ¥0.50 per million tokens
This is only about 4% cheaper than usage-based billing.
If the business uses only 70% of the quota by the end of the month, actual usage is 700 million tokens. The effective plan price rises to approximately:
¥500 ÷ 700 ≈ ¥0.714 per million tokens
The same 700 million tokens would cost approximately ¥366 under usage-based billing. In this case, the plan is about 37% more expensive.
Subscription plans are therefore better suited to workloads with stable traffic that consistently consume most of the quota. For unpredictable or highly variable traffic, usage-based billing is usually more flexible and less risky.
5. Enterprise Cost Optimization Practices
Relying on the billing rules of a single provider rarely produces the lowest possible cost. Enterprises should establish cost governance at the architecture level.
Stabilize prefixes to improve cache hit rates
Place system instructions, few-shot examples, and other stable content at the beginning of the prompt. Put dynamic content afterward. Measure the actual cache hit rate before adjusting the prompt structure.Route requests by time period and price
Use an API gateway to coordinate request routing. During peak periods, traffic can be sent to a usage-based model with predictable response performance. During off-peak periods, requests can be directed to plan-based capacity with sufficient remaining quota. Before switching, validate output quality and rate-limiting behavior across the relevant models.Compress input
Before calling a large language model, use a lightweight model or traditional algorithms to summarize, denoise, and truncate long text. This reduces unnecessary input tokens.Cache results and deduplicate semantically
Store results for identical queries in a key-value store such as Redis. Use a vector database to identify semantically similar questions. Result caches should have expiration policies and must account for consistency when the underlying data changes.
6. Cost Review Checklist
During periodic cost reviews, check the following items:
- Is the cache hit rate measured daily, with cached tokens separated from ordinary input tokens?
- Has the input-to-output token ratio changed?
- How is traffic distributed between peak and off-peak periods?
- What proportion of traffic comes from offline tasks?
- What percentage of the subscription or prepaid quota is actually being used?
- What are the peak levels of burst traffic, and have any rate limits been triggered?
- After a provider changes its prices, have effective unit prices been recalculated?
7. Disclaimer
All prices, plans, and quotas in this article are illustrative assumptions and do not represent the pricing of any specific platform. Actual prices, peak and off-peak periods, plan rules, and quotas should be verified in the provider’s official billing documentation and applicable contract. These terms may change over time.
