Understanding LLM API Pricing: A Practical Guide to Cost Optimization
This article breaks down how large language model API billing works beyond simple per-token rates, covering separate input and output pricing, context caching, and latency tiers. It explains the cost structure behind real invoices, then walks through practical optimization techniques such as limiting output length, designing stable cache-friendly prefixes, and routing requests between standard and high-speed tiers. Readers will find a reusable cost estimation formula, a weekly review workflow, and common pitfalls to avoid when managing LLM spending.
1. Why the Pricing Model Matters More Than the Unit Price
When integrating a large language model into a business system, teams often fixate on a single number: the price per million tokens. Real invoices, however, are determined by several dimensions at once. Input and output are priced separately, cache hits and cache writes are priced separately, and different latency tiers are priced separately. Ignoring any one of these leads to budget estimates that can be off by several multiples compared to actual spending.
From the source material, a typical pricing framework can be abstracted as follows: the standard tier charges 13 per million input tokens and 68 per million output tokens; the high-speed tier charges 26 and 136 respectively, exactly double the standard tier; cache hits are uniformly priced at 1.3 per million tokens; cache writes are split into two retention durations, five minutes and one hour, priced at 13 and 26 for the standard tier and 26 and 52 for the high-speed tier. The previous-generation model charged 20 for input, 100 for output, and 2 for cache hits, showing that the new generation has shifted unit prices down across the board.
The most noteworthy point here is not the price drop, but three structural characteristics:
- Output is priced at roughly five times the input price, which means the generation stage is the main cost battleground.
- The cache hit price is only one-tenth of the input price, so reusing context has very high economic value.
- Cache writes are tiered by time, meaning “how long to retain” is itself an optimizable cost variable.
2. The Cost Structure Created by Separate Input and Output Pricing
Because the output unit price is far higher than the input unit price, any application that defaults to “long answers” will have its bill amplified. In engineering practice, costs are usually brought under control from three directions:
First, constrain output length. Set differentiated maximum generation lengths for different tasks; extraction tasks can be limited to a few hundred tokens, while creative tasks can be relaxed further.
Second, turn “generation” into “selection.” Results that can be retrieved or matched from a candidate set should not be left to free-form model generation.
Third, use structured output. Require the model to return a strict structure, reducing pleasantries, restatement, and explanatory redundancy.
An illustrative call-parameter configuration is shown below (it only expresses a general approach):
resp = client.chat.completions.create(model="your-model-name",messages=messages,max_tokens=512, # Control output-side costtemperature=0.2, # Reduce randomness and rework)
It is worth emphasizing that every dollar saved on the output side usually has a higher marginal return than on the input side, because the two unit prices differ by several multiples.
3. Context Caching: Hits, Writes, and Lifecycle
Context caching is the core mechanism for reducing the cost of repeated calls. Its economic logic is this: on a hit, you are billed at an extremely low price; on a miss, you must “write,” and the write price is positively correlated with the retention duration.
This leads to three practical rules:
- Keep the prefix stable. Caching usually depends on prefix consistency, so put unchanging system instructions, tool descriptions, and domain knowledge at the front, and put user input that changes with each request at the end.
- Prioritize reuse. Multi-turn conversations and batched similar tasks should share the same prefix, avoiding rebuilding context every turn.
- Match the duration. Short-cycle batch jobs should choose a shorter retention tier, and long-cycle sessions should choose a longer tier; do not pay for durations you will not use.
A counter-example: putting timestamps or random request IDs at the beginning of the prompt causes the prefix to change every turn, so the cache can never hit, and the cost ends up higher than not using the cache at all.
4. Tiered Scheduling Between Standard and High-Speed Tiers
The high-speed tier costs twice as much as the standard tier, and what it buys is not stronger results but more stable latency. The sensible approach is therefore tiered routing: send interactive scenarios where users can perceive waiting to the high-speed channel, and send offline batch processing, data cleaning, log summarization, and similar scenarios to the standard channel.
Request entry|-- Real-time interaction (low-latency requirement) --> High-speed channel`-- Offline tasks (throughput priority) ----------> Standard channel
Combined with queuing and concurrency control, this can significantly improve user-experience metrics while keeping overall cost unchanged.
5. Cost Estimation and the Optimization Loop
Teams are advised to build a reusable estimation sheet that treats request volume, average input length, average output length, cache hit rate, and retention duration as variables:
Cost per call ≈ input tokens × input price × (1 - hit rate)+ input tokens × hit price × hit rate+ output tokens × output price+ write tokens × write price
Then review three metrics weekly: cache hit rate, average output length, and the share of the high-speed channel. The optimization loop should proceed in this order: first improve the hit rate (the largest payoff), then compress output length, and finally adjust channel tiering. If an organization uses the relevant model services from Baidu AI Cloud, it can combine monitoring, alerting, and logging services to continuously observe the metrics above and form a quantifiable cost-governance mechanism.
6. Common Pitfalls
- Looking only at unit price and not at hit rate, leading to budgets that are severely over- or under-estimated.
- Enabling long-duration caching for all requests, so write fees eat up the hit savings.
- Running offline tasks on the high-speed channel, doubling cost with no experience benefit.
- Frequently changing the system prompt, rendering the caching mechanism useless.
Overall, understanding the three main threads of “separate input and output, tiered caching, and tiered latency” turns large language model usage cost from something uncontrollable into an engineering problem that can be modeled, optimized, and reviewed.
