LLM API Pricing Explained: Token Billing and Cost Optimization
Learn how LLM APIs charge for input and output tokens, why generated tokens typically cost more, and how prompt caching affects billing. This article presents a practical cost-estimation model and engineering strategies for managing inference costs through prompt design, output limits, model routing, monitoring, and batch processing.
LLM API Pricing Explained: Token Billing and Cost Optimization
1. Understanding LLM API Billing
As large language models (LLMs) mature, more developers and businesses are accessing their capabilities through APIs rather than training and deploying models themselves. This approach lowers the barrier to adoption, but it introduces an important engineering challenge: understanding and managing API usage costs.
Unlike cloud services billed by instance runtime or storage capacity, LLM APIs generally use token-based pricing. Tokens are the basic units of text processed by a model. A tokenizer converts text into a sequence of tokens, which the model then processes to interpret the input and generate a response. Token counts therefore serve as a key measure of usage and the main basis for billing.
Providers commonly price input tokens and output tokens separately, with output tokens typically costing substantially more.
For illustration, the source pricing example for a recently released model lists approximately $3 per million input tokens on a cache miss and $15 per million output tokens. At those rates, output tokens cost five times as much as input tokens. This example illustrates a common pricing structure rather than a current pricing guarantee.
The difference reflects important characteristics of model inference.
2. Why Output Tokens Cost More Than Input Tokens
Input processing can run in parallel
When a model receives a prompt, it can process the input tokens in parallel during the prefill stage. This parallel processing generally makes the stage efficient and helps reduce the cost per input token.
Output generation is sequential
During the decode stage, the model generates text autoregressively: each new token depends on the tokens generated before it.
Each generation step requires a forward pass, and the next token must wait for the preceding step to finish. Longer responses therefore require more sequential computation, with workloads potentially growing beyond a simple linear relationship as context length increases.
Decoding is also highly sensitive to memory bandwidth, and hardware utilization is often lower than during prefill. These characteristics help explain why generated tokens are more expensive.
Output length is less predictable
The number of output tokens depends on what the model generates. Developers can set an upper limit, but they cannot precisely determine the actual length of every response. This adds uncertainty to output-side spending.
Together, these factors help explain pricing structures in which output tokens cost three to five times as much as input tokens. The exact ratio depends on the provider and model.
3. Prompt Caching: An Often-Overlooked Cost Lever
The phrase “cache miss” in an input-token price points to an important optimization mechanism: prompt caching, also known as context caching.
When multiple requests share the same prefix—such as a fixed system prompt, a background document, or few-shot examples—the inference engine can reuse previously computed intermediate results, including the key-value (KV) cache. This avoids repeating computation for the shared prefix.
Cached input tokens are often billed at a lower rate. In some pricing arrangements, the cached rate may be one-tenth of the uncached rate or less.
For developers, three practices are particularly useful:
- Put stable content first. Place system instructions, background documents, and other unchanged material at the beginning of the prompt. Put request-specific content afterward to maximize prefix reuse.
- Reuse context across related requests. When answering multiple questions about the same long document, make use of the available caching mechanism instead of repeatedly paying the full processing cost for the shared context.
- Account for cache expiration. Caches usually have a limited lifetime. Long gaps between requests can cause cached content to expire, so evaluate potential savings against the application’s actual request frequency.
The value of caching depends not only on the advertised discount but also on how often requests successfully reuse cached content.
4. Building a Cost-Estimation Model
During project planning, translate expected business activity into token usage. A simple starting model is:
Cost per request =Input tokens × Price per input token+ Output tokens × Price per output tokenMonthly cost =Average cost per request × Average daily requests × 30
If prices are quoted per million tokens, divide the token counts by 1,000,000 before multiplying by the quoted rates.
Consider a customer-support application with the following assumptions:
- Average input: 2,000 tokens per request, including knowledge-base context
- Average output: 500 tokens per request
- Daily volume: 100,000 requests
- Illustrative input price: CNY 20 per million tokens
- Illustrative output price: CNY 100 per million tokens
Using the reference rates from the original example:
Daily input cost =2,000 × 100,000 / 1,000,000 × CNY 20= CNY 4,000Daily output cost =500 × 100,000 / 1,000,000 × CNY 100= CNY 5,000Total daily cost = CNY 9,000Estimated monthly cost = CNY 270,000
Although the output token count is only one-quarter of the input token count, output accounts for more than half of the total cost.
The practical lesson is that reducing unnecessary output can save more than reducing the same number of input tokens, particularly when output pricing is substantially higher.
5. Engineering Practices for Reducing Costs
1. Streamline prompt design
Remove redundant descriptions and replace lengthy natural-language instructions with concise, structured wording.
Even a reduction of 100 tokens in a frequently used system prompt can produce meaningful cumulative savings at high request volumes.
2. Limit unnecessary output
Set an appropriate maximum output length, ask for bullet-point responses when suitable, and avoid long-form generation where it adds little value.
These measures directly target the more expensive portion of many API calls. Output limits should still leave enough room for a complete, useful response.
3. Route requests by task complexity
Not every request needs the most capable model.
A routing layer can send simple classification or intent-recognition tasks to a lightweight model while reserving a flagship model for complex reasoning.
Tiered routing can deliver substantial savings; the source article describes reductions of 30% or more in suitable deployments. Actual savings depend on the workload and the mix of models used. This is application-level routing, rather than necessarily a mixture-of-experts model architecture.
4. Make effective use of caching
Organize prompts around reusable prefixes and improve cache hit rates wherever the provider’s caching mechanism permits.
For applications with large, repeated contexts, caching can significantly reduce the average input cost per request.
5. Establish usage monitoring
Use logging, monitoring, and alerting to attribute token consumption by business unit, user, and API endpoint.
Budget-threshold alerts can help identify unexpected spending before it becomes difficult to control. General-purpose cloud observability services can support this monitoring setup without tying the approach to a particular platform.
Monitoring also makes it easier to see which workloads produce long responses, which repeatedly process large contexts, and where optimization efforts should be focused.
6. Use asynchronous and batch processing
For tasks that do not require immediate responses—such as offline document processing or bulk content generation—combine suitable work and execute it in batches.
Where requests can share context, this can reduce repeated context transmission and processing. Evaluate the benefit against the application’s latency requirements and the provider’s supported processing options.
6. Conclusion
Token-based LLM API pricing looks straightforward, but effective cost management requires an understanding of inference behavior, caching mechanisms, and application usage patterns.
When choosing an API, look beyond the headline token price. Evaluate the input-to-output price ratio, cached-input discounts, and the expected balance of input and output tokens in your workload.
With focused prompt design, appropriate output limits, tiered model routing, caching, and usage monitoring, developers can keep inference spending predictable and sustainable while maintaining useful application results.
