Understanding LLM Token Pricing and Cost Optimization Practices
This article explains how token-based billing works for large language model inference and why it differs from traditional usage-based software costs. It breaks down the four core billing factors—asymmetric input/output pricing, context caching, tiered pricing, and tokenizer differences—then provides a reusable cost estimation model with sample code. It also covers four dimensions for evaluating providers and four practical cost governance recommendations, helping readers build accurate budgets and avoid common billing mistakes.
1. Why Token Billing Needs Its Own Framework
In traditional software engineering, cost usually scales linearly with call volume, CPU time, or storage capacity, which makes capacity planning relatively straightforward. LLM inference has a completely different cost structure: the basic billing unit is not the number of requests, but the number of tokens the model processes and generates. A single request may consume only a few hundred tokens, or it may consume hundreds of thousands of tokens at once because of an extra-long context. The cost difference between the two can span three orders of magnitude. This means that if you budget using the intuition of “estimating by call count,” the result will almost certainly be inaccurate.
The complexity goes further: mainstream billing systems generally split cost into multiple dimensions—input tokens, output tokens, cache hits and cache writes, and, for some providers, context-length tiered pricing. A misunderstanding of any single dimension can cause the final bill to deviate significantly from expectations. So the first step in cost governance is not comparing prices, but clearly defining the billing model.
2. A General Explanation of the Four Core Billing Factors
2.1 Asymmetric Pricing for Input and Output
Most inference services charge different unit prices for input and output, and the output price is usually significantly higher than the input price. The reason is that the generation phase requires token-by-token autoregressive decoding, which cannot be parallelized as fully as the prefill phase, resulting in lower compute utilization. The engineering implication is direct: controlling output length often saves more money than compressing input, especially in scenarios with long outputs such as summarization, rewriting, and structured extraction.
2.2 Context Caching: Hits and Writes
To reduce the compute overhead of repeated prefixes, the industry commonly offers context caching, which is typically billed in two parts: cache writes (writing a long prefix into the cache for the first time, usually priced at or slightly above the normal input rate) and cache hits (subsequent requests reuse that prefix at a steep discount). For businesses with fixed system prompts, fixed knowledge base documents, or multi-turn conversations that share a long history, the cache hit rate is one of the highest-weighted variables in the cost model.
2.3 Tiered Pricing: Unit Price Rises with Input Length
Some services use tiered pricing, where the more input tokens a single request contains, the higher the unit price of the tier it falls into. This contrasts sharply with fixed-unit-price models. Under tiered pricing, splitting one extra-long request into several shorter requests may actually reduce the total cost; under a fixed-unit-price model, splitting only adds fixed overhead and latency, which is not worthwhile.
2.4 Billing Basis Differences: Characters, Bytes, and Tokens
Different models use different tokenizers, so the same Chinese text may map to noticeably different token counts. Therefore, cross-model price comparisons must use tokens as a unified basis; you cannot directly compare “cost per thousand characters,” or the conclusion may be reversed.
3. A Reusable Cost Estimation Model
Let input tokens be I, output tokens be O, cache-write tokens be W, and cache-hit tokens be H, with corresponding unit prices p_in, p_out, p_write, and p_hit (in CNY per million tokens). The cost of a single request is:
Cost = I * p_in / 1e6+ O * p_out / 1e6+ W * p_write / 1e6+ H * p_hit / 1e6
Example implementation (demonstrating calculation logic only, with no vendor APIs):
def estimate_cost(i, o, w=0, h=0,p_in=0.0, p_out=0.0, p_write=0.0, p_hit=0.0):"""Estimate single-request cost (CNY) using unit prices per million tokens"""return (i * p_in + o * p_out + w * p_write + h * p_hit) / 1_000_000def tiered_unit_price(tokens, tiers):"""Tiered pricing: tiers is [(upper, price), ...]; returns the input unit price hit"""for upper, price in tiers:if tokens <= upper:return pricereturn tiers[-1][1]
Take a typical long-context batch task as an example: roughly 510,000 input tokens, about 8,000 output tokens, and about 460,000 cache-write tokens. With fixed unit prices (input at 7 CNY and output at 28 CNY per million tokens), the cost is about 3.57 + 0.23 ≈ 3.80 CNY. With tiered pricing, if 510,000 tokens fall into the highest tier, the unit price rises and the cost increases accordingly. If 460,000 of those tokens are cache hits and the hit price is only a small fraction of the input price, the total cost can drop by an order of magnitude. The same usage produces vastly different results under three billing rules—which is exactly why you should “model first, then choose.”
4. Four Dimensions for Evaluation
- Billing transparency: Does it provide itemized breakdowns for input, output, and cache, and can these be exported for reconciliation? Missing details make optimization impossible.
- Cache economics: Does the hit discount and cache lifetime match your business reuse pattern?
- Pricing model fit: Long-context batch processing and short-request high concurrency call for completely different pricing models.
- Observability support: Does it offer call-volume statistics, token usage monitoring, and budget alerts? This usually needs to be implemented alongside logging and monitoring/alerting systems.
It is worth stressing that the ease of use of a console UI only affects troubleshooting efficiency and should not be the basis for a cost conclusion; the real judgment must return to the itemized usage data itself.
5. Four Practical Cost Governance Recommendations
- Establish a usage baseline: separately track average input, output, and cache hit rates by business scenario to form a regression-able cost baseline.
- Optimize prompt structure: move the stable, unchanging parts to the front as a cacheable prefix and place variables at the end to maximize cache hits.
- Constrain output length: use explicit format constraints and length limits to suppress redundant generation, since the output unit price is usually the main amplifier on the bill.
- Set budget guardrails: enforce quotas and rate limiting at the gateway layer, and combine monitoring alerts to trigger daily/monthly budget threshold reminders, preventing runaway bills from abnormal calls.
6. Common Pitfalls
- Comparing unit prices without comparing the basis: ignoring tokenizer differences leads to distorted cross-model conclusions.
- Ignoring cache-write costs: for long prefixes reused infrequently, the write cost may exceed the savings from hits.
- Budgeting by call count: under token-based billing, this almost inevitably underestimates cost.
- Treating UI experience as a capability rating: it has no causal relationship with cost or quality.
7. Conclusion
The complexity of token billing comes from the stacking of multiple dimensions, not from a single high or low price. A workable path is to build an estimation formula with tokens as the unified basis, plug real usage data into different pricing rules for comparison, and then keep iterating with caching strategies, output constraints, and budget guardrails. If you need a deployable solution for usage collection and a cost dashboard, you can build one on general capabilities such as logging, monitoring/alerting, and object storage offered by Baidu AI Cloud, forming a cost governance loop that is decoupled from the business and reusable.
