A Practical Guide to Calculating the Real Cost of LLM Inference
Comparing per-million-token prices side by side is a common but misleading shortcut. This guide shows how to model LLM inference cost as a function of cache hit rate, input/output ratio, and time-of-day pricing, how to improve cache hit rates through prompt architecture, how to convert subscription plans into equivalent per-token prices, and how to build a repeatable measurement workflow with sensitivity analysis. Readers will learn which variables actually drive the bill, how to spot ranking reversals between providers, and how to avoid common estimation mistakes.
1. Why “Price Comparison” Almost Always Leads to the Wrong Conclusion
During the evaluation phase of an LLM application, teams often place the “price per million tokens” of two services side by side and simply pick the cheaper one. This is not a sound engineering practice. An inference bill is essentially a multi-variable function, and any comparison based on a single variable will push the conclusion far off course.
Real spending is determined by at least five variables:
- The price of the cache-hit portion (usually significantly lower than the regular input price);
- The price of the cache-miss input portion;
- The price of the output portion (usually the most expensive of the three);
- The distribution of requests along the timeline, i.e., whether they fall within the provider’s discount window;
- The input-to-output ratio and the cache hit rate itself.
The phenomenon that sparked debate in the original discussion is a typical case: under an extremely high cache hit rate, the provider with the lower cache price can actually end up more expensive; when the hit rate drops to a medium level, the ranking completely reverses. This shows that the numbers on a price sheet are parameters, not conclusions.
2. Abstracting the Bill into a Computable Model
Let the total token volume be T, the cache hit rate be h, the input share be i, and the output share be o (i + o = 1), with cache price p_c, regular input price p_i, and output price p_o. Then:
Cost per million tokens = h * p_c + (i - h) * p_i + o * p_o
Note an easily overlooked constraint: h ≤ i. Cache can only hit content on the input side, so the hit rate can never exceed the input share. When the input share is only 1.5%, the upper bound of the hit rate is 1.5%, and a so-called “98% hit rate” is mathematically impossible at that point. This is a reminder that before citing any hit rate figure, you should first confirm whether it refers to the input portion or the total volume.
Verifying the ranking reversal with illustrative values (unit: CNY per million tokens; values are for demonstration only):
def cost_per_m(h, i, o, p_c, p_i, p_o):return h * p_c + (i - h) * p_i + o * p_o# Option A: cheaper cache, slightly more expensive outputA = dict(p_c=0.115, p_i=8.0, p_o=28.0)# Option B: more expensive cache, cheaper outputB = dict(p_c=0.50, p_i=6.0, p_o=16.0)for h in (0.95, 0.98):print(h, cost_per_m(h, 0.985, 0.015, **A),cost_per_m(h, 0.985, 0.015, **B))
The conclusion is intuitive: at a high hit rate, the cache price dominates total cost, and the provider with the cheaper cache wins; as the hit rate declines, the cache-miss input and output shares grow, the output price becomes the dominant term, and the ranking changes accordingly. The claim of “more than twice as expensive” simply places two parameters onto a workload that is most sensitive to caching.
3. Hit Rate Is Not a Configuration Setting, but an Architectural Outcome
You cannot raise the hit rate in a console; it is a product of application architecture. The actionable engineering levers are:
- Stable prefix. Place the system prompt, tool definitions, few-shot examples, and long documents at the very front of the request and keep them byte-for-byte stable. Inserting any timestamp, random ID, or username will invalidate the prefix.
- Reduce prefix jitter. Move dynamic content to the back and avoid the order of “assemble dynamic fields first, then the static template.”
- Control concurrency and lifecycle. Most platforms have a time-to-live window for cache entries; too long an interval between requests causes repeated cold starts.
- Segmented reuse. Split long context into independently reusable stable blocks instead of retransmitting the entire document every time.
- Observability. Use logging services and monitoring alerts to track prefix length, hit ratio, and cold-start counts, treating the hit rate as a first-class metric rather than something mysterious.
An illustrative request assembly order:
[stable system instructions][stable tool schema][stable knowledge block] | [conversation history] | [current dynamic input]
The more stable and longer the left side of the vertical bar, the higher the hit rate; stuffing dynamic variables onto the left side is the most common source of cost incidents.
4. Time-of-Day Pricing: Treat “Peak Hours” as a Capacity Constraint
The essence of discount-window pricing is peak shaving and valley filling. When evaluating it, look at three parameters: the duration covered by peak hours, the discount magnitude during off-peak windows, and the overlap between your own traffic and the peak.
If the peak is compressed to a few hours per weekday, most offline batch jobs can avoid it entirely; conversely, if the peak covers the whole day, time-based optimization yields almost no benefit. The assessment method is simple: build a time histogram of requests over the past two weeks, calculate the share of tokens falling within the peak interval r, then use effective average price = peak price * r + off-peak price * (1 - r) to get the weighted unit price.
Actionable strategies include: queueing latency-insensitive tasks such as batch summarization, offline evaluation, log analysis, and vectorization into the discount window; keeping interactive paths available at all times; using a message queue to smooth out load and a container platform to scale on demand, avoiding long-term payment for peak capacity.
5. Subscriptions vs. Pay-as-You-Go: How to Do the Equivalent Conversion
Evaluating a subscription coding plan cannot rely on the monthly fee alone; it must be converted into an “equivalent price per million tokens”:
Equivalent unit price = monthly fee / total available tokens per month
When converting, be sure to confirm three things: the quota accounting method (whether input and output are weighted, and how cache hits are counted), additional restrictions during peak hours, and whether the quota accumulates or resets across weeks. The observation in the original text that “the plan’s unit price is about 50-70% of the original API price” is not a universal rule; it depends on the provider’s quota generosity and your actual utilization. A plan you cannot fully use will see its equivalent unit price rise sharply, even to several times the pay-as-you-go cost.
A practical decision rule: first compute the monthly token volume and peak share from measured load, then plug them into the pay-as-you-go formula and the subscription formula respectively, and take the lower one; at the same time, keep the pay-as-you-go channel as overflow fallback to avoid business interruption when the quota runs out.
6. A Reusable Estimation Workflow
- Sampling. Instrument requests to record input, output, cache-hit token counts, and timestamps for each call.
- Categorization. Separate interactive and batch pipelines, and measure the ratio and time distribution for each.
- Modeling. Use the formulas above to compute each option’s unit price under the measured distribution, not an ideal one.
- Sensitivity analysis. Sweep the hit rate across 70%-98%, observe the ranking reversal point, and confirm whether the conclusion is robust.
- Optimize and re-evaluate. Re-measure after implementing prefix governance and quantify the gains from a higher hit rate.
- Governance. Set budget alerts and quotas, and use a monitoring dashboard to track cost trends per unit of business volume.
7. Common Pitfalls Checklist
- Comparing overall cost using the input price while ignoring that the output share is usually the main cost item;
- Citing hit rate figures that contradict the input share;
- Assuming cache is permanently valid and ignoring the time-to-live window;
- Counting only token fees while ignoring retries, failed requests, and peripheral costs such as vectorization;
- Evaluating a plan using the theoretical maximum quota while ignoring real utilization;
- Treating a single load-test result as a long-term average without covering business peaks and troughs.
8. Conclusion
The cost question in model selection is ultimately an engineering problem of “measure first, model next, optimize last.” By turning hit rate, time-of-day distribution, and input/output ratio into observable metrics, and comparing pay-as-you-go and subscriptions horizontally with a unified formula, you can keep your judgment stable even when price sheets change frequently. If you need to support batch inference and elastic scaling, you can build an observable, budget-controlled inference pipeline by combining general cloud capabilities such as object storage, message queues, container platforms, and monitoring alerts.
