A Practical Guide to Calculating the Real Cost of LLM Inference
Comparing per-million-token list prices is a common but unreliable way to estimate LLM inference costs, because the actual bill is a weighted sum across cached input, uncached input, and output tokens, and may also vary by time of day. This guide walks through a token-based cost formula, a reusable Python estimation function, and the impact of cache hit rate on total spend. It also explains peak and off-peak pricing, how to evaluate subscription-style plans, and how to turn cost governance into a repeatable process using real traffic data.
1. Why Comparing Unit Prices Almost Always Gives the Wrong Answer
When LLM applications move into production, cost estimation is often the most oversimplified step. A common approach is to put the “per-million-token price” of two services side by side and pick the cheaper one. The problem is that a real bill is not determined by a single unit price. It is the weighted sum of several pricing dimensions multiplied by call volume.
The tokens in a typical request fall into three categories: input tokens that hit the cache, input tokens that miss the cache, and output tokens generated by the model. These three are usually priced very differently. Cached input is often only a small fraction of the base input price, while output is typically several times the input price. As a result, the actual cost of the same model can vary by several times across different workloads, and comparing unit prices across services rarely leads to a reliable conclusion.
A less obvious variable is time. Time-of-day pricing, with peak and off-peak tiers, is now common in the industry. Some providers charge a premium during peak hours, while others only throttle or surcharge specific time windows. If load testing and budgeting both rely on an all-day average price, the actual bill can easily drift from expectations.
2. The Cost Model: Write Down the Formula First
Let T be the total tokens in a given time window, h the cache hit rate, r_in the input ratio, r_out the output ratio, p_cache the cached input unit price, p_in the regular input unit price, and p_out the output unit price. The cost per token is:
cost_per_token = h * r_in * p_cache+ (1 - h) * r_in * p_in+ r_out * p_out
Take an example with 1.5% input, 0.5% output, and 100 million total tokens, evaluated at two hit rates of 98% and 95%. Two typical conclusions emerge. In a high-hit-rate scenario (around 98%), the provider with the more expensive cache price can actually have the lower total cost. When the hit rate drops to around 95%, the ranking may reverse. This shows that the cache unit price is meaningless on its own; it only becomes meaningful when bound to the hit rate.
In engineering practice, it is best to turn this logic into a script instead of estimating by hand every time:
def estimate(total_tokens, hit_rate, in_ratio, out_ratio,p_cache, p_in, p_out):cached = total_tokens * hit_rate * in_ratiomiss = total_tokens * (1 - hit_rate) * in_ratioout = total_tokens * out_ratioreturn (cached * p_cache + miss * p_in + out * p_out) / 1e6
3. Cache Hit Rate: The Only Metric Worth Optimizing
The formula shows that each percentage point of improvement in the hit rate often saves more than “one percentage point.” It shifts tokens that would otherwise be billed as regular input onto cached pricing, creating a clear leverage effect. Common ways to improve the hit rate include:
- Stable prefix: keep the system prompt, tool definitions, and few-shot examples fixed at the very front of the request, and avoid dynamic timestamps or random IDs polluting the prefix.
- Reduce prefix jitter: do not reorder historical messages during a session, and do not insert changing fields into the system section.
- Increase reuse granularity: turn long documents and code-base context into reusable stable blocks that are shared across multiple turns through the same prefix.
- Monitor the hit rate: add instrumentation in logging and monitoring/alerting to track the hit rate and the share of cached tokens, with dashboards split by business line.
It is worth emphasizing that the specific cache matching rules, minimum cacheable length, and expiration policies are defined by each service provider. Before integration, refer to the official documentation rather than making budget assumptions.
4. Time-of-Day Pricing and Plans: Choosing Between Two Cost Structures
The essence of time-of-day pricing is to convert capacity pressure into a price signal. The peak-hour premium corresponds to the cost of scarce compute. When evaluating it, first look at the time distribution of your own traffic. If calls concentrate in daytime working hours, the impact of the peak premium is amplified. If the workload is mainly nighttime batch processing, the actual average price will be clearly lower than the list average price.
Subscription-style plans (the Coding Plan type) are a different structure: a fixed monthly fee buys a token quota or call allowance, and its value depends on whether you actually use it up. The test is straightforward: convert the plan quota over the billing cycle into an “equivalent original-price API amount,” then divide by the actual monthly fee to get a discount range. In practice, the equivalent discount of such plans usually falls between half and seventy percent of the original price. When you do not use it fully, the discount degrades quickly, and the overage may be billed at the original price.
A practical decision order is: first measure the hit rate and time distribution with real traffic, then plug them into both the time-of-day pricing model and the plan-conversion model, and take the lower of the two. At the same time, keep a switchable abstraction layer to avoid being locked into a single billing structure.
5. Make Cost Governance a Process
Cost optimization is not a one-time activity but a continuous process: estimate the token volume during the requirements phase; calibrate the hit rate with real traffic during the canary phase; after launch, review three metrics weekly: cost per token, cache share, and peak-hour share; and re-run the budget regression after architecture changes such as prompt restructuring or context strategy adjustments. If you use general-purpose cloud capabilities, you can combine a log service for call auditing, monitoring and alerting for threshold reminders, and object storage to archive historical requests for offline analysis, upgrading “picking the cheapest by feel” to “choosing the structure by data.”
