LLM API Billing Explained: Token Pricing, Cache Hits, and Practical Cost Control
This article breaks down how LLM API billing actually works and why comparing sticker prices per token leads to wrong conclusions. It explains the three billing tiers — input tokens, output tokens, and cache-hit tokens — the compute reasons behind the price gap between prefill and decode, and the real difference between pay-as-you-go and subscription quota plans. It also profiles the usage characteristics of coding workloads, where cost is dominated by long, repetitive input contexts, and lays out engineering practices for reducing spend: stabilizing prompt prefixes to preserve cache hits, trimming context, routing by model tier, reusing results, and setting budget alerts. A Python cost-estimation example and a routing sketch make the framework directly applicable. Readers come away with a vendor-neutral method for reading a bill, modeling true cost, and optimizing spend regardless of how prices change.
1. Why LLM API Pricing Is Harder Than It Looks
When developers estimate the cost of an LLM API, they often focus on a single number — the price per 1K tokens — and miss the layered structure underneath it. In practice, mainstream LLM services split their billing units into three categories: input tokens, output tokens, and cache-hit tokens. These have different unit prices, and the spread can be several multiples.
More importantly, the published unit price is only a “list price.” In real usage, actual cost is also shaped by context length, call frequency, cache hit rate, rate limits, and quota reset cycles. Comparing two models on sticker price alone therefore tends to produce the wrong conclusion.
The goal of this article is to give you a complete cost mental model: one that lets you both read the invoice and calculate the real bill.
2. The Three-Layer Token Billing Structure
LLMs bill by token. As a rough rule of thumb, Chinese text works out to about one token per character, while English is roughly four characters per token. Billing generally distinguishes three types:
- Input tokens: everything you send to the model, including system prompts, conversation history, user input, and any attached knowledge base or code context.
- Output tokens: the content the model generates and returns.
- Cache-hit tokens: the portion of your input that matched a server-side cache (a prefix cache) and therefore did not need to be recomputed, earning a lower unit price.
Following the industry-standard pricing pattern, the relationship among the three is typically: output > input > cache hit. Understanding this gradient is the first step in cost optimization.
3. Why Input and Output Tokens Cost Differently
3.1 Different Compute Characteristics
The input phase (prefill) can process an entire prompt in parallel, which keeps GPU utilization high. The output phase (decode) is autoregressive: tokens are produced one at a time, and each new token depends on everything generated before it. That serial nature keeps compute utilization lower. This fundamental difference in compute patterns is the direct reason output tokens usually cost more than input tokens.
3.2 Why Cache-Hit Tokens Are the Cheapest
When multiple requests share the same prefix — for example, a fixed system prompt, coding convention notes, or long document background — the server can cache the intermediate computation for that portion (the KV cache). Once a later request hits that cache, the prefill step does not need to be repeated, so cache-hit tokens can be priced well below ordinary input tokens.
This matters especially in coding scenarios: AI coding tools tend to carry a large body of unchanging system instructions and project context. If that content hits the cache consistently, long-term cost drops substantially.
A simple cost-estimation example:
def estimate_monthly_cost(requests_per_day,input_tokens,output_tokens,cache_hit_ratio,price_input, # unit price per 1M input tokensprice_output, # unit price per 1M output tokensprice_cache_hit, # unit price per 1M cache-hit tokens):days = 30total_input = input_tokens * requests_per_day * daystotal_output = output_tokens * requests_per_day * dayscached = total_input * cache_hit_ratiofresh_input = total_input - cachedcost = (fresh_input / 1e6 * price_input+ cached / 1e6 * price_cache_hit+ total_output / 1e6 * price_output)return {"fresh_input_tokens": int(fresh_input),"cached_tokens": int(cached),"output_tokens": int(total_output),"estimated_cost": round(cost, 2),}
The value of this code is that it makes the often-ignored variable — cache hit rate — explicit. Between two options with similar list prices, actual cost can differ by several multiples depending on how much of the input hits the cache.
4. Pay-as-You-Go vs Subscription Quotas
Commercially, LLM services come in two billing forms:
- Pay-as-you-go: billed in real time per token. You pay for what you use, the bill is transparent, and spending has a hard ceiling. It fits workloads with unpredictable volume or ones that need precise cost accounting.
- Subscription quota plans (including coding bundles): a fixed monthly fee in exchange for a call allowance or rate quota. Once converted to a unit price it looks cheaper, but it usually comes with rate limits, periodic quota resets, and similar constraints.
One common misconception is worth clearing up here: a plan’s quota is not equivalent to the cost of the actual experience. Coding tools often maintain multiple independent quota pools (for example, conversation and code execution counted separately), each with a different reset cycle. If certain capabilities require frequent resets or quota top-ups, the subjective cost to the user may not be lower than pay-as-you-go at all. “Low converted unit price” does not equal “low total spend,” so any evaluation has to factor in your real usage distribution and how efficiently you consume quota.
5. The Usage Profile of Coding Workloads
Coding applications are among the highest-frequency consumers of LLMs, and their usage has three distinct characteristics:
- Long context: they carry large amounts of code, files, and convention notes as background, so input tokens typically far outnumber output tokens.
- High repetition: system prompts and project context stay nearly identical across consecutive sessions, which makes them ideal candidates for cache reuse.
- Dense calls: a single development task can trigger dozens of model interactions, so rate limits and quota reset policies have a marked effect on the experience.
Because the input side dominates and repeats so heavily, cost optimization for coding should focus on reducing effective input cost — mainly by raising the cache hit rate and trimming context to compress real spend.
6. Engineering Practices for Cost Optimization
Building on the billing characteristics above, there are several directions for systematically lowering LLM usage costs:
- Stabilize the prompt prefix: put fixed system instructions and tool descriptions at the very front of the prompt, and keep their content and ordering stable, so frequent edits do not invalidate the cache. Caching generally depends on an exact prefix match, and any drift in the prefix causes a cache miss.
- Trim the context: carry only the code snippets genuinely relevant to the current task, and avoid redundant conversation history and unrelated files. The tighter the context, the lower the input token count and the accompanying cost.
- Route across model tiers: classify tasks by complexity, send simple tasks to a lightweight model, and reserve higher-capability models for complex ones instead of using a sledgehammer to crack a nut.
- Reuse results: cache results for identical or similar requests to reduce repeated calls to the model.
- Budget and monitoring: wire up logging and monitoring alerts, and set threshold alerts on daily token consumption and spend so cost stays visible and anomalies stay controllable.
A schematic routing and budget-control sketch:
def route_request(task):if task["complexity"] == "simple":return small_model # lightweight model, low costelif task["needs_cache"] and task["prefix_stable"]:return cached_model # cache hit, low unit priceelse:return large_model # high-capability model, used on demand
7. Model Selection and Budget Decisions
Back to the original question — how should you think about price changes across model generations? The answer is not to agonize over one generation’s unit price, but to build the right cost perspective:
- Look at structure, not unit price: evaluate the weighted cost across all three token types — input, output, and cache hit — rather than a single number.
- Total up the real bill, not the sticker price: fold rate limits, quota resets, and multiple quota pools into a model of your actual usage, so a “converted unit price” cannot mislead you.
- Prioritize optimization over switching: rather than hopping between options, get the engineering work done first — stabilize the prompt prefix, trim the context, route across model tiers. These typically cut cost more than switching models does.
Billing for LLM APIs is fundamentally “converting tokens into compute,” and the price of compute will keep shifting as the technology iterates. What developers really need is a cost analysis and optimization methodology that is independent of any specific vendor and reusable over time. Internalize this framework as an engineering habit, and you can make rational, predictable cost decisions no matter how prices move.
