LLM API Cost Optimization: Why Identical Prices Still Produce Very Different Bills
When two LLM APIs advertise the same input, cache-hit, and output prices, their monthly bills can still diverge sharply. This article breaks LLM API billing into four factors you can calculate yourself — token pricing units, cache hits and writes, offline discounts and subscription plans, and call structure — and shows why the effective bill depends on all of them, not just the per-token rate. It includes a runnable Python cost-estimation script and a worked example that runs the same usage through four scenarios, cutting the bill by roughly 36% from baseline. You'll also get a platform-rule checklist, answers to common billing pitfalls (why caching sometimes doesn't help, why output tokens dominate cost, how retries double-bill), and a five-step cost-reduction playbook.
1. Why the Same Price Can Still Mean Bills That Differ by 40%+
Almost every team runs into the same question the first time it compares prices across multiple LLM API platforms: the model is the same, and the input, cache-hit, and output unit prices look nearly identical — so why do the month-end bills differ so much?
First, a caveat. The claim that bills differ by “more than 40%” usually only holds under a specific condition: same model, same usage volume, and the same availability of discounts. If the tasks differ in whether they can run offline, whether a subscription plan is purchased, or whether call timing is flexible, the bills will diverge even further.
“An identical unit price” only means the pay-as-you-go rate is the same for one price channel. It does not mean the entire billing pipeline is the same. A real monthly bill is built up from something much closer to:
Monthly bill = real-time pay-as-you-go calls + offline pay-as-you-go calls + subscription plan fees and overage + off-peak discounts + hidden costs from cache writes and retries
Platforms differ in their default values, available options, stacking rules, and settlement order. To avoid tying the discussion to any one vendor, this article does not name specific platforms. Instead, it abstracts cross-platform price differences into four billing factors you can plug into your own calculations: token pricing units, cache hits and writes, offline discounts and subscription plans, and call structure.
2. The Four Billing Factors That Decide How Different the Bills Are
2.1 Token Pricing Units
LLMs bill by token, but the way each vendor’s pricing page expresses it can vary: some use price per million tokens, some per thousand tokens, and some split the input side into two tiers — “cache miss” and “cache hit.” The first thing to do before comparing prices is to convert every price to the same denominator. This article uses price per million tokens throughout.
You also need to confirm whether the output side includes structured output, function-call arguments, special control tokens, and so on. Mixing different pricing conventions in one table will always distort the conclusion.
2.2 Cache Hits and Writes
Cache hits are the most easily underestimated item on a modern API bill. The common pattern is this: stable prefixes — system prompts, fixed examples, tool definitions — are marked as reusable context. After a hit, they are billed at the cache-hit price, which is usually far below the cache-miss input price.
But caching is not “turn it on and save money.” Two things deserve close attention:
- A cache hit requires the prefix to match exactly. If the system prompt mixes in timestamps, request IDs, randomly ordered examples, or dynamic user information, the whole prefix can be invalidated and fall back to full-price input.
- Cache writes are usually billed separately. The write price may equal the cache-miss input price, or it may be tiered by TTL. In the examples in this article,
write_ratiois defined as the proportion of cache-miss input tokens that need to be written to cache. For instance,write_ratio=0.05means that 5% of the tokens in the cache-miss input need to be written to cache, and this portion is billed separately atcache_write_price.
Low-frequency call scenarios deserve special care: if the cache rarely hits but a large number of requests still write to it, the write cost can eat up the savings.
2.3 Offline Discounts and Subscription Plans
This item is usually the main source of “same price, different bill.”
- Offline/batch channel. Tasks that don’t require second-level responses — log analysis, batch rewriting, offline extraction, data backfill — are often priced lower than real-time pay-as-you-go if the platform offers an asynchronous batch channel.
- Subscription/committed-volume plans. Prepay for a certain token allowance and the unit price is usually lower than pay-as-you-go; but the overage portion may fall back to the pay-as-you-go price or an average unit price.
- Off-peak discounts. Nighttime and low-traffic periods may carry additional discounts, provided calls can be moved to those windows.
Plan calculations require special attention to order. The example in this article uses: calculate the plan first, then the offline discount, then the off-peak discount. It also assumes the plan covers “input + output” tokens and does not include cache writes; the overage falls back to the average input/output unit price. If a real platform’s rules differ, adjust the script to match the actual rules.
2.4 Call Structure: The Real-World Multiplier
At the same unit price, different businesses produce different bills because their call structures differ:
- the ratio of real-time to offline calls;
- time-of-day distribution;
- context length and cache-hit rate;
- retries and failure rate;
- model-version changes and shifts in free-tier allowances.
When two platforms quote the same unit price, what you are really comparing is how well your usage profile matches their rules.
Platform Rule Checklist
Before doing a cross-platform comparison, confirm at least the following five items:
| Rule | What to confirm |
|---|---|
| Pay-as-you-go price | Whether input, cache-hit, cache-write, and output are all converted to the same unit |
| Cache TTL | Whether writes are tiered; whether a hit requires the prefix to match exactly |
| Offline discount | The proportion of tasks that can go offline, the discount size, whether it stacks with plans |
| Subscription plan | Coverage scope, overage price, whether it is calculated before the offline discount |
| Off-peak discount | The discount window; whether it applies only to the real-time portion |
3. Cost Model and a Runnable Script
Below is a corrected estimation script. It splits the bill into input, output, cache write, plan, offline, and off-peak parts. The key fix is: the off-peak discount applies only to the real-time portion and is no longer mistakenly applied to the part that already received the offline discount.
from dataclasses import dataclass@dataclassclass Price:"""Unit price: CNY per million tokens"""input_price: float # cache-miss inputcache_hit_price: float # cache-hit inputcache_write_price: float # cache writeoutput_price: float # outputdef estimate(requests: int,in_tokens: int, # average input tokens per requestout_tokens: int, # average output tokens per requestcache_ratio: float, # proportion of input that hits cache, 0~1price: Price,write_ratio: float = 0.0, # proportion of cache-miss input that needs to be written to cacheoffline_ratio: float = 0.0, # proportion of requests that can go through the offline channeloffline_discount: float = 0.0,# offline discount, 0.6 means paying 60% of the pricepackage_tokens: float = 0.0, # tokens covered by the plan, unit: million tokenspackage_price: float = 0.0, # effective unit price of the planoffpeak_ratio: float = 0.0, # proportion of the real-time portion that can be moved to off-peak hoursoffpeak_discount: float = 0.0 # off-peak discount, 0.8 means paying 80% of the price):m = 1_000_000total_in = requests * in_tokens / mtotal_out = requests * out_tokens / mhit_in = total_in * cache_ratiomiss_in = total_in - hit_inwrite_tokens = miss_in * write_ratio# Input side: cache-hit input + cache-miss inputcost_in_no_write = hit_in * price.cache_hit_price + miss_in * price.input_price# Cache writes are billed separatelywrite_cost = write_tokens * price.cache_write_price# Output sidecost_out = total_out * price.output_pricebase = cost_in_no_write + write_cost + cost_out# Plan deduction: assume it covers input + output, not cache writesbillable_tokens = hit_in + miss_in + total_outcovered = min(billable_tokens, package_tokens) if package_tokens > 0 else 0.0base_io = cost_in_no_write + cost_outavg_io = base_io / billable_tokens if billable_tokens > 0 else 0.0pkg_cost = covered * package_priceremaining = max(0.0, billable_tokens - covered)over_cost = remaining * avg_ioafter_pkg = pkg_cost + over_cost + write_cost# Offline discount: approximate by proportion of the total bill after the planoffline_part = after_pkg * offline_ratiorealtime = after_pkg - offline_partafter_offline = realtime + offline_part * offline_discount# Off-peak discount: applies only to the real-time portionoff_part = realtime * offpeak_ratiofinal = after_offline - off_part + off_part * offpeak_discountreturn {"input_cost": round(cost_in_no_write, 2),"cache_write": round(write_cost, 2),"output_cost": round(cost_out, 2),"base_total": round(base, 2),"after_package": round(after_pkg, 2),"after_offline": round(after_offline, 2),"final": round(final, 2),}if __name__ == "__main__":p = Price(input_price=20,cache_hit_price=2,cache_write_price=20,output_price=100)# A: pure pay-as-you-go, no cache, all real-timeprint(estimate(6_000_000, 1200, 300, cache_ratio=0.0, price=p, write_ratio=0.0))# B: cache enabled, 60% hit rate, 5% of cache-miss input needs to be written to cacheprint(estimate(6_000_000, 1200, 300, cache_ratio=0.6, price=p, write_ratio=0.05))# C: B + 30% of requests go offline, at 60% of the priceprint(estimate(6_000_000, 1200, 300,cache_ratio=0.6, price=p, write_ratio=0.05,offline_ratio=0.3, offline_discount=0.6))# D: C + 1/3 of the real-time portion moved to off-peak, at 80% of the priceprint(estimate(6_000_000, 1200, 300,cache_ratio=0.6, price=p, write_ratio=0.05,offline_ratio=0.3, offline_discount=0.6,offpeak_ratio=1/3, offpeak_discount=0.8))
4. Four Bills from the Same Usage: Where the Difference Comes From
Assume a typical usage profile:
- Monthly requests: 6 million;
- Average input: 1,200 tokens;
- Average output: 300 tokens;
- Total monthly input: 7.2 billion tokens;
- Total monthly output: 1.8 billion tokens;
- Example unit prices: cache-miss input 20, cache-hit 2, cache write 20, output 100 — all in CNY per million tokens.
The following figures are illustrative results that demonstrate the calculation method and do not correspond to any specific platform.
| Scenario | Key assumptions | Monthly cost (illustrative) | vs. baseline |
|---|---|---|---|
| A: Pure pay-as-you-go, no cache, all real-time | 0% cache hit, no writes, no discounts | 324,000 CNY | baseline |
| B: Cache enabled | 60% hit rate; 5% of cache-miss input written to cache | 249,120 CNY | -23.1% |
| C: Cache + 30% offline at 60% | Offline portion billed at 60% | 219,225.60 CNY | -32.3% |
| D: C + 1/3 of real-time calls moved to off-peak | Off-peak portion billed at 80%, applies only to real-time | 207,600.00 CNY | -35.9% |
This table yields three key conclusions.
First, the cache benefit is “hit rate × price gap,” but you still have to subtract the write cost. With an input price of 20 and a hit price of 2, the hit portion itself is 90% cheaper; the overall reduction is only 23.1% because only 60% of the input hits the cache, the miss portion is still charged at full price, and there is also the write cost.
Second, the benefit of the offline discount depends on “the offline-eligible proportion × the discount size.” If only 30% of the tasks don’t require real-time responses, you can’t enjoy the full offline discount.
Third, the off-peak discount looks less dramatic than the cache and offline savings, but it usually has the lowest implementation cost. Moving scheduled tasks, batch tasks, and backfill tasks to off-peak hours is an optimization that “changes the configuration, not the architecture.”
If you further stack a subscription plan — say, the plan covers 60% of input and output tokens, at a plan unit price below pay-as-you-go, with the overage falling back to the average unit price — the bill can drop further. The exact reduction depends on the plan’s coverage, its unit price, the offline ratio, and the off-peak ratio.
5. The Most Easily Overlooked Billing Details and FAQ
Why isn’t the cache taking effect?
A common cause is that the system prompt mixes in timestamps, request IDs, randomly ordered examples, or dynamic user information, which breaks prefix consistency. Put stable content first and dynamic content last.Why aren’t cache writes worth it?
Low-frequency, long-tail, short-reuse requests that keep writing to the cache may cost more in writes than they save in hits. Enable caching only for fixed prefixes that are reused at high frequency, and track “write cost / hit savings.”Output tokens are usually more expensive than input. In scenario A, output tokens make up only 20% of total tokens but contribute about 56% of the base cost. Limiting
max_tokens, using summaries instead of full text, and compressing structured fields usually pay off more than optimizing input alone.Retries are billed again. A retry after a timeout, rate limit, or parse failure is a real request every time. Set a retry cap, enforce idempotency, and use backoff to avoid a doubled bill during incidents.
Model versions change the unit cost. Older versions may be retired or stop opening to new users, and newer versions may come with time-limited free allowances. When building an annual budget, estimate with the post-migration new price rather than reusing the old model’s price.
How do you decide whether a task can go offline? If the business can accept minute-level, hour-level, or batch responses — for example log analysis, data backfill, batch rewriting, offline extraction — then it is a candidate for evaluating the offline channel.
How do you change the configuration for off-peak discounts? Schedule cron jobs, batch tasks, and non-urgent report tasks into the low-traffic windows the platform marks, and monitor failed retries so that nighttime task failures don’t end up increasing the bill.
6. The Five-Step Cost-Reduction Checklist
Step 1: Measure. Add instrumentation at the call layer and record at least model, channel, realtime, offpeak, cache_hit, and retry_id. Without data, optimization is guesswork.
Step 2: Shrink the denominator. Stabilize the system-prompt prefix, compress output length, and cap retries. These three usually don’t depend on any platform feature, yet can bring a clear reduction.
Step 3: Change the channel. Move asynchronous-eligible tasks to the offline/batch channel, and move stable real-time traffic to a subscription or committed-volume plan, with overage falling back per the rules.
Step 4: Shift the timing. Move scheduled tasks, data backfills, and batch rewrites to off-peak hours. The implementation cost is low and the benefit is stable.
Step 5: Add routing and governance. Tier tasks by difficulty: route simple classification, extraction, and formatting to a lighter model, and reserve complex reasoning for the flagship model. At the same time, set budget alerts, quota caps, monthly reconciliation, and quality evaluations to avoid cost cuts that cause quality regressions.
7. Conclusion
The right way to compare prices across platforms is not to compare unit-price tables alone, but to compare the match between “rules + usage structure.”
The recommended order is:
- Unify the pricing unit, converting all prices to price per million tokens;
- Run the same usage through the four factors: input conventions, cache rules, offline/subscription, and time-of-day distribution;
- Optimize in the order “measure → shrink the denominator → change the channel → shift the timing → route and govern.”
If your business already runs on a mature, stable cloud model service, prioritize doing the measurement and optimization on your own side: unify token instrumentation, stabilize the prompt prefix, separate real-time from offline tasks, and set budget alerts. These actions are independent of any specific platform, yet they determine whether the bill can actually go down.
