LLM Token Cost Estimation: How Cache Hit Rate Determines Your Bill
This article explains how cache billing splits LLM API costs into cache-hit input, cache-miss input, and output, and why cache hit rate strongly affects the final bill. It covers a weighted cost formula, a method for estimating hit rates from request logs, a comparison of pay-as-you-go pricing with subscription plans, and a reusable checklist for cost estimation.
LLM Token Cost Estimation: How Cache Hit Rate Determines Your Bill
As large language models are deployed at scale in coding assistants, customer service bots, and retrieval-augmented generation (RAG) systems, token costs have become a significant factor in technology selection for many teams. Once cache billing was introduced, the bill changed from a simple input-plus-output structure to a three-part model: cache-hit input, cache-miss input, and output. Many teams compare costs by looking only at the list prices vendors publish, and their actual bills can end up several times higher than expected. This article uses the layered billing structure common across the industry as a basis for breaking down how real costs are calculated, and it offers a reusable estimation framework.
1. Why a Single Unit Price Is Not Comparable
Modern LLM API billing typically includes several separate price items:
- Cache-hit input: Request content that repeats earlier context. It is billed at the cache price, which is usually much lower than the regular input price.
- Cache-miss input: Input content that appears for the first time. It is billed at the regular input price.
- Output: Content generated by the model. Its unit price is usually the highest.
The core problem is that vendors anchor their prices differently. Some highlight cache prices, some highlight off-peak discounts, and some highlight bundled plan prices. Comparing a standard cache price directly with an off-peak cache price mixes different bases and leads to wrong conclusions. The correct approach is to fix one set of hit-rate and time-period assumptions, then calculate a weighted blended cost per unit of tokens.
2. The Three-Part Cost Formula
Define the following:
his the cache hit rate (0 to 1).iis the share of cache-miss input, roughly the input portion of 1 − h.ois the share of output.P_cache,P_input, andP_outputare the unit prices for the three categories.
The weighted cost per million tokens is:
Blended cost = h × P_cache + i × P_input + o × P_output
Take a typical high-cache scenario. In a coding assistant, users often resend the full code context within the same session, so the cache hit rate can reach about 98%. Assume input makes up 99.5% of tokens and output makes up 0.5%. The hit-rate split is then 98% / 1.5% / 0.5%. Substituting these values into the formula gives the weighted blended cost.
Note that the output share is very small, but because the output price is usually several times the input price, its effect on total cost is not always negligible. In code generation, if the model’s replies are long, the output share may rise to 2% to 3%, which noticeably changes the structure of the bill.
3. Hit Rate as the Main Lever on the Bill
Cache hit rate is the most sensitive variable in cost estimation. The following two sets of typical assumptions compare its effect. The unit is per 100 million tokens, and the prices are illustrative relative levels.
Scenario A: High hit rate (98% hit / 1.5% input / 0.5% output)
In this case, the bill is almost entirely determined by the cache price. If a vendor’s off-peak cache price is very low, the cost advantage of off-peak batch jobs is significant under time-of-use pricing. If you only look at non-off-peak prices, the gap between the two vendors shrinks to a very small range.
Scenario B: Medium hit rate (95% hit / 4% input / 1% output)
Each 3-percentage-point drop in the hit rate more than doubles the share of cache-miss input. Because the cache-miss input price is usually several to dozens of times the cache price, the bill can rise instead of fall. Some time-of-use plans with deep discounts can end up costing more than standard pricing at a lower hit rate.
This leads to an important conclusion: the higher the hit rate, the more sense it makes to choose low cache prices with time-of-use pricing. When the hit rate is unstable or low, pricing with a higher cache price and a cheaper input price is more stable. This also explains why different teams can reach opposite conclusions from the same set of quotes.
4. Estimating Hit Rate in Practice
The hit rate should come from measurement, not guesswork. You can estimate it from request logs. For each request, compute the overlap between the current input prefix and the cached prefix from the previous round. The following example shows the idea:
def estimate_hit_ratio(requests, tokenizer):hit_tokens, total_input = 0, 0prev_prefix = ""for req in requests:full_prompt = build_prompt(req) # Includes system prompt, history, and current input# Compute the longest common character prefix with the previous round to approximate hit tokenscommon = common_prefix_len(prev_prefix, full_prompt)hit_tokens += tokenizer.count(common)total_input += tokenizer.count(full_prompt)prev_prefix = full_promptreturn hit_tokens / total_input
Keep three points in mind during measurement:
- Cache lifetime: Most platforms expire cache entries after a limited time, from a few minutes to a few hours. Infrequent requests often miss the cache, so estimates should only count continuous sessions within the time window.
- Prefix stability: Keep the system prompt and few-shot examples at the beginning and leave them unchanged. Any change to content that comes earlier invalidates the entire cached prefix.
- Concurrent sessions: When many users run at once, the cache is isolated per session. Hit rate should be averaged across sessions weighted by their traffic, not computed as a simple global average.
5. Trade-offs Between Time-of-Use Pricing and Subscription Plans
Besides pay-as-you-go billing, subscription plans are also common in the industry. A subscription is a monthly fee for a large token quota. The effective per-token price is usually 50% to 70% of the original API price. Some plans also define peak hours, during which rate limits apply or quotas are reduced. For example, a plan might have a peak window of several hours per day, with more generous quotas during the rest of the day.
Use the following framework to choose between them:
- Stable and predictable usage: If your team can estimate weekly token consumption reliably and peak demand is not extreme, the lower effective price of a subscription is a clear advantage. You trade predictable spending for a discount.
- Volatile usage: Bursty workloads, such as large-scale code review or bulk data extraction, usually fit pay-as-you-go billing better. Pair this with off-peak batch scheduling to run non-real-time jobs during discounted hours.
- Hit-rate-sensitive applications: First calculate the blended cost with the formula above, then compare it with the subscription’s effective price. Do not compare a subscription’s unit price directly with an API cache price.
6. A Reusable Cost Estimation Checklist
Engineering teams can apply the following checklist to evaluate costs:
- Fix a common hit-rate assumption. Test at least two levels, 95% and 98%.
- Use a consistent time-period assumption that separates off-peak and non-off-peak hours.
- Use a consistent output share assumption, based on real business logs.
- Calculate the weighted blended cost to produce a comparable metric, cost per 100 million tokens.
- Compare the result with the effective price of any subscription plan. Check whether the plan’s peak-hour limits affect your SLOs.
- Regularly compare actual bills with estimates and check for drift in the hit rate.
Cost optimization is not about finding the cheapest unit price. It is about finding the billing mix that fits your workload, given how hit rate, time-of-use distribution, and usage volatility interact. Turning this framework into an internal tool helps keep costs under control as models evolve quickly and prices change often.
