How to Accurately Calculate the True Cost of LLM API Usage and Choose the Right Provider
A practical framework for estimating the real cost of LLM API usage. Learn how cache hit rates, peak/off-peak pricing, and subscription plans affect your bill, with formulas and code examples to guide model selection.
As domestic large language models rapidly improve, the performance gap between models is narrowing, and cost is becoming a core factor for developers and enterprises when choosing technology. However, LLM API pricing is far more complex than the headline number of “price per million tokens.” Mechanisms such as cache discounts, peak/off-peak time-based pricing, and subscription plan conversions stack on top of each other, meaning the real cost under different usage patterns can differ by several times. This article sets aside specific vendors and presents a general, reusable cost estimation methodology to help you make rational cost decisions across different business scenarios.
1. Why the Sticker Price Misleads You
When comparing model services, many developers’ first instinct is to check the “standard input price” or “cached input price” and conclude: pick whichever is cheaper. This approach has three common pitfalls:
Ignoring the impact of cache hit rate. Modern LLM services generally support context caching (repeated prompt prefixes are charged at a very low rate). In scenarios such as AI coding assistants, multi-turn conversations, and agent workflows, cached tokens often account for over 90% of input, making the cached price—not the standard price—the dominant cost factor.
Ignoring the weight of output tokens. Output tokens are usually priced several times higher than input tokens. Although they make up a smaller share, they cannot be ignored in long-form generation or code completion scenarios.
Ignoring the time dimension. Some services use peak/off-peak pricing, where the same call can cost more than twice as much depending on the time of day. Your traffic distribution directly determines how much “off-peak dividend” you can capture.
2. A Universal Cost Calculation Formula
A rigorous per-call cost model can be expressed as:
Total cost = input tokens × (cache hit rate × cached price + (1 - cache hit rate) × standard input price) + output tokens × output price
Take a typical high-cache scenario: 100 million tokens with 98% cache hits, 1.5% standard input, and 0.5% output. Suppose two services have different pricing structures: Service A has a slightly higher cached price but a lower standard price, while Service B offers an extremely low cached price during off-peak hours. Plugging into the formula, Service A costs about 12.6 units per 100 million tokens, while Service B costs about 9.4 during off-peak hours—B is indeed cheaper.
But when we lower the cache hit rate from 98% to 95%, the conclusion flips: Service A costs about 14.9, while Service B costs about 18.3. The reason: every percentage point the hit rate drops, tokens that previously enjoyed the cache discount are billed at the standard price—and because B’s standard price is higher, its cost elasticity is greater.
This comparison reveals a key rule: services with low cached prices win in high-hit-rate scenarios, while services with low standard input prices are more stable in medium-to-low hit-rate scenarios. Your choice should depend on your business’s actual cache hit rate, not an idealized assumption.
3. How to Estimate Your Business’s Cache Hit Rate
Cache hit rate is not a number you can guess—it is determined by your application architecture:
- AI coding assistants: system prompts and project context are extremely long and highly reused; hit rates typically reach 95%–99%.
- Multi-turn conversations and customer service: conversation history accumulates turn by turn with high prefix reuse; hit rates are generally above 90%.
- Single-turn Q&A and content generation: each request’s prompt differs significantly; hit rates may fall below 80%.
The recommended approach: after integrating a service, compute the ratio of cached tokens to total input tokens from your call logs over a period of time to get a measured hit rate, then plug it into the cost formula. You can also proactively raise the hit rate through prompt engineering—for example, placing invariant parts (role definitions, knowledge base snippets, tool definitions) at the front of the prompt and variable user input at the end, maximizing prefix reuse.
4. Time Arbitrage Under Peak/Off-Peak Pricing
Time-based pricing is another easily overlooked variable. Vendors define “peak hours” very differently: some mark most of the weekday daytime as peak, while others limit peak windows to just a few afternoon hours on workdays. The shorter the peak window, the more time you have to run calls at off-peak prices.
For tasks that are not time-sensitive—such as batch data processing, offline evaluations, code scans in nightly builds, and bulk document summarization—proactively schedule them during off-peak hours. A simple scheduling example:
import datetimedef is_off_peak(peak_hours, workdays_only=True):now = datetime.datetime.now()if workdays_only and now.weekday() >= 5:return Truereturn now.hour not in peak_hours# Assume peak hours are 14:00-18:00 on workdaysif is_off_peak(peak_hours={14, 15, 16, 17}):run_batch_job()
By using a task queue (e.g., a message queue plus scheduled jobs) to concentrate elastic tasks into off-peak periods, you can often cut another 30%–50% off your bill.
5. Break-Even Analysis: Pay-As-You-Go vs. Subscription Plans
Beyond per-token billing, subscription plans (such as monthly plans for coding scenarios) are another mainstream model. These plans typically provide a token quota per week or month, and the quota may differ between peak and off-peak periods. Evaluating whether a plan is worthwhile takes two steps:
Step 1: Convert the plan quota into an equivalent pay-as-you-go amount. For example, suppose a plan tier officially allows about 300 million tokens per week during off-peak hours. If those tokens would be worth roughly 160 units at standard API prices, and the plan costs 118 per month, the plan discount is about 70% of the standard price; higher tiers may reach a 50% discount.
Step 2: Assess quota utilization. A subscription is essentially “prepaid wholesale”—the wholesale price only pays off when your actual usage consistently reaches 60%–80% of the quota or more. If your usage fluctuates heavily or falls far below the quota, pay-as-you-go is more economical.
A simple decision rule: track your real token consumption for two to four consecutive weeks. If weekly average consumption exceeds 70% of a plan tier’s quota, upgrade to that tier; if it exceeds the highest tier, consider a hybrid “subscription + pay-as-you-go” model.
6. Comprehensive Selection Recommendations
Combining the analysis above, here is an actionable decision workflow:
- Measure your business’s actual cache hit rate and input/output ratio, plug them into the universal cost formula, and compute the unit cost of each candidate service.
- Analyze your traffic’s time distribution, estimate the share of calls that can be shifted to off-peak hours, and introduce task scheduling for time arbitrage if needed.
- Track real usage to find the break-even point between pay-as-you-go and subscription plans, and avoid paying for idle quota.
- For latency-sensitive, high-concurrency production traffic, also weigh non-price factors such as reliability, rate-limiting policies, and context length. A multi-provider architecture with failover can reduce single-point risk.
- Establish a monthly cost review mechanism. Pricing rules and model versions will change, and your cost model should be updated accordingly.
Conclusion
Optimizing LLM API costs is fundamentally an engineering problem shaped by your business characteristics, not a simple price comparison. Cache hit rate determines which pricing structure wins, traffic distribution determines how much off-peak dividend you capture, and usage volume determines whether a subscription is worthwhile. Mastering the methodology of “measure first, calculate, then adjust dynamically” is more valuable than memorizing any price table. We hope this framework helps your team spend every dollar wisely amid the rapid iteration of models.
