How to Calculate the Real Cost of LLM APIs: A Practical Guide
Comparing per-million-token prices is misleading because LLM API pricing is multi-dimensional, workload-dependent, and time-sensitive. This guide shows how to build your own cost model from cache hit rates, token composition, and peak/off-peak windows, calculate the break-even cache hit rate between candidate providers, improve hit rates through prefix design, shift batch workloads off peak, and decide when a subscription plan beats pay-as-you-go. It includes formulas, scheduling logic, quota monitoring, and a practical implementation checklist.
1. Why Comparing Unit Prices Almost Always Leads to the Wrong Conclusion
When evaluating the cost-effectiveness of LLM APIs, the most common approach is to place the “price per million tokens” of two options side by side and pick the lower one. In engineering practice, this is almost guaranteed to be misleading, for three reasons:
- Pricing is multi-dimensional. Mainstream offerings typically split a request into cached input, uncached input, and output, each priced separately. The cached portion is often only one-tenth to one-twentieth of the input price. Comparing just one of these segments means comparing only a small slice of the total cost.
- Workload structures differ enormously. The same “price per million tokens” can translate into very different actual spending for long-context workloads that reuse a system prompt across many turns versus short, standalone question-and-answer traffic with no reuse. The difference can be several times over.
- The time dimension is ignored. Many offerings introduce peak/off-peak pricing or rate constraints, so the same token volume produces different bills depending on when it is consumed.
The right approach, therefore, is not to compare unit prices but to build a cost model of your own: take the token structure of your workload, the cache hit rate, and the time of day as inputs, and produce the cost per unit of business output.
2. The Cost Model: Breaking the Bill into Computable Parts
Suppose an offering has three unit prices (in CNY per million tokens): cached input P_cache, uncached input P_in, and output P_out. Given a batch of requests whose token composition is cached C, uncached input I, and output O, the total cost is:
Cost = C * P_cache + I * P_in + O * P_out (tokens in millions)
The two sets of numbers in the source material can be abstracted into two typical options:
- Option A: relatively high cached-segment price (on the order of 0.115 CNY per million tokens)
- Option B: lower cached-segment price (on the order of 0.05 CNY per million tokens), but higher uncached input and output prices
Under an extremely high-reuse workload of “98% cache hit, 1.5% uncached input, 0.5% output,” the cost per 100 million tokens is about 12.57 CNY (A) versus 9.40 CNY (B), so the low cache price option wins. Under a “95% cache hit” workload, the two become about 14.92 CNY and 18.25 CNY, and the high cache price option is actually cheaper.
These results reveal the single most important conclusion of this guide: a low cache unit price does not mean a low total cost; the winner depends on which side of the break-even point your hit rate falls on. The break-even point is the hit rate that makes the two expressions equal. It is worth pre-computing this threshold for every candidate option and using it as the selection criterion.
3. Sensitivity Analysis: Hit Rate Is the First Lever on Cost
Sweeping the hit rate from 90% to 99.5% produces two crossing cost curves. In practice, hit rate can be improved from three directions:
- Stable prefix: keep the system prompt, tool definitions, and few-shot examples fixed at the very front, and avoid inserting volatile content such as timestamps or random IDs on every request.
- Controlled context growth: in multi-turn conversations, append to history rather than rewriting it, so the entire prefix does not become invalid.
- Batching and concurrency strategy: aggregate similar requests that can be merged, reducing uncached input caused by repeated prefixes.
An illustrative way to organize the prefix:
[Fixed segment] System role + tool schema + output format constraints + a few examples ← target for long-term cache hits[Semi-fixed segment] Business knowledge snippets (grouped by tenant/scenario, consistent within a group)[Volatile segment] Current user input, timestamp, session ID ← placed last to avoid polluting the prefix
It is worth stressing that the specific way caching is enabled, its invalidation rules, and its billing conventions vary considerably across platforms. Always defer to the official documentation of the service you choose; this guide assumes no particular API shape.
4. Peak and Off-Peak Windows: The Undervalued Cost Variable
Beyond caching, time of day is the second lever. The source material notes that one option has a peak window of 4 hours per weekday, which is relatively generous, while another has a longer peak window. The general rule abstracted from this is:
- The shorter the peak window and the closer it tracks real business peaks, the easier it is to schedule batch jobs out of peak hours.
- If offline tasks (batch summarization, log analysis, regression evaluation, data cleaning) make up a large share, schedule all of them to off-peak windows by default.
- When online traffic cannot be moved, use request queuing and degradation strategies to defer non-critical requests to off-peak execution.
A simple scheduling decision:
def route(request, now, peak_windows):if request.is_batch and in_peak(now, peak_windows):return QUEUE_FOR_OFFPEAK # deferrable jobs yield peak capacityif in_peak(now, peak_windows) and request.is_latency_sensitive:return USE_CHEAP_FALLBACK # online requests take a low-cost path at peakreturn CALL_PRIMARY
5. Subscription Plans: Turning Variable Cost into Fixed Cost
The essence of a subscription plan (the Coding Plan category) is trading committed usage for a discount. Converted against list pay-as-you-go rates, the three tiers in the source material work out to 50%–70% of the original price, i.e. a 30%–50% discount. Deciding whether a plan is worthwhile takes only three steps:
- Use the model from Section 2 to compute your monthly list-price API spend
S. - Find the plan price
Mthat covers your usage tier and time-of-day needs. - If
M < Sand the plan quota is still sufficient during peak hours, the plan is better; otherwise pay-as-you-go is more flexible.
The accompanying engineering constraint: plans usually come with quota cycles (for example, weekly resets) and peak-hour rate limits, so you must implement quota level monitoring and automatically fall back to the pay-as-you-go channel as you approach the cap, avoiding tasks being throttled at the end of a cycle.
if weekly_quota_used / weekly_quota > 0.8 and in_peak(now):switch_to_pay_as_you_go()
6. Implementation Checklist
- Build a unit price table: record the cache/input/output prices and peak/off-peak rules of candidate options in configuration rather than scattering them through the code.
- Profile your real workload: measure cache hit rate, input/output ratio, and peak/off-peak distribution over at least one full business cycle.
- Compute the break-even hit rate: use it to choose primary and backup options instead of a single-segment unit price.
- Optimize prefix structure: moving volatile content to the end is the cheapest way to raise hit rate.
- Schedule offline tasks: moving deferrable load out of peak hours is usually the fastest payoff.
- Combine plan and pay-as-you-go: carry the stable baseline with a plan, absorb peak overflow with pay-as-you-go, and set quota level alerts.
With this method, cost evaluation is upgraded from “comparing unit prices” to “comparing workload fit,” and the selection conclusion updates automatically as your workload profile changes, rather than remaining frozen at a one-off price screenshot.
