LLM Inference Cost Optimization: From Billing Structure to Cache Hits
A practical guide to understanding how large language model inference is billed and how to reduce costs. This article breaks down token-based pricing, cached-prefix discounts, and off-peak rates, shows how to estimate per-task cost with a simple formula, and outlines five engineering techniques—cache-friendly prompt design, model tiering, context compression, batch processing, and cost observability—that can cut inference spending by an order of magnitude.
LLM Inference Cost Optimization: From Billing Structure to Cache Hits
A lightweight LLM recently topped a model distribution platform’s popularity charts on launch day, promoted with the claim that “a single task costs roughly 0.26 RMB.” Beyond the specific product, the phenomenon reflects a broader technical reality: as LLM applications scale into production, inference cost is replacing model capability as the first constraint in architecture decisions. This article doesn’t benchmark any particular model. Instead, it uses this moment as a starting point to systematically cover inference billing structures, cost estimation methods, and optimization techniques—giving developers a reusable methodology for cost control.
1. Understanding LLM Billing Structures
Mainstream inference services all bill by token, but several key differences are essential to understand.
1.1 Asymmetric Input and Output Pricing
Most services charge different rates for input and output tokens, with output typically 3–5x more expensive. The reason lies in the two phases of inference: the prefill phase processes input tokens in parallel with high throughput, while the decode phase generates tokens serially, limited by memory bandwidth, making the per-unit compute cost higher. As a result, a task with long input and short output costs far less than one with short input and long output.
1.2 Cache Hit Discounts
This is the pricing mechanism most worth watching in recent years. When multiple requests share an identical prefix (such as a system prompt or long document context), the server can reuse already-computed KV cache and skip prefill computation. Cached input tokens can cost as little as one-tenth of the regular price or less—some services charge only around 4% of the standard input rate for cache hits. For workloads with fixed templates and high call volumes, this fundamentally changes the cost structure.
1.3 Peak vs. Off-Peak Pricing
Some services use higher pricing tiers during peak hours, with peak rates several times higher than off-peak. For latency-insensitive offline tasks (batch processing, data labeling, report generation), scheduling during off-peak windows is a zero-cost optimization.
2. Estimating Per-Task Cost
Before choosing a model, build the ability to quantify costs. The formula is simple:
Cost per task = input tokens × input price+ output tokens × output price- cached tokens × (input price - cached price)
Take a typical document Q&A task: system prompt plus document totals about 8,000 input tokens (of which 6,000 are a cacheable fixed prefix), with an average output of 2,000 tokens. Assume an input price of 0.5 RMB per million tokens, an output price of 2 RMB per million tokens, and a cached price of 0.02 RMB per million tokens:
- Non-cached input: 2,000 × 0.5 / 1e6 = 0.001 RMB
- Cached input: 6,000 × 0.02 / 1e6 = 0.00012 RMB
- Output: 2,000 × 2 / 1e6 = 0.004 RMB
Total: roughly 0.005 RMB per task. Even for heavier workloads—multi-turn agent tasks with hundreds of thousands of input tokens and tens of thousands of output tokens—keeping per-task cost under a few cents (RMB) is entirely feasible. This also explains the logic behind claims like “running a task for 0.2 RMB”: a lightweight model combined with cache reuse really can push costs down an order of magnitude.
3. Five Engineering Techniques for Cost Optimization
3.1 Design Prompts for Cache Hits
Cache hits require an exactly identical prefix. In practice, place static content (system instructions, tool definitions, reference documents) at the very front of the prompt and dynamic content (user questions, conversation state) at the end. Avoid inserting volatile fields like timestamps or random IDs into the prefix. This restructuring doesn’t change model behavior, but it directly improves cache hit rates—often reducing input costs by 50% or more.
3.2 Model Tiering and Task Routing
Not every task needs a flagship model. A sound architecture tiers by task complexity: route simple tasks like classification, extraction, and rewriting to lightweight models, and reserve large models for complex reasoning. In practice, the price gap between lightweight and flagship models can reach 10–20x, and for many structured tasks the quality gap can be closed with good prompt design and a few examples. A simple routing layer (rule-based, or powered by a small model) can reduce overall cost by an order of magnitude.
3.3 Compress Context and Output Length
Output tokens are expensive, so controlling output length offers the best cost-to-effort ratio: explicitly instruct the model to “output JSON only,” “keep it under 200 words,” or “skip explanations.” This saves money and simplifies parsing. On the input side, use retrieval-augmented generation (RAG) to inject only relevant passages rather than entire documents, and periodically summarize conversation history in long dialogues.
3.4 Batch and Asynchronous Processing
For offline tasks, prefer batch inference endpoints (usually discounted significantly) and schedule them during off-peak windows. Use asynchronous queues to smooth traffic spikes and avoid triggering peak-hour pricing.
3.5 Build Cost Observability
Log input/output token counts, cache hit rates, and unit prices for every call, and feed them into your logging and alerting systems with aggregation by business dimension. You can’t optimize what you don’t measure—many teams only discover in production that a prompt template has driven cache hit rates to nearly zero, or that a faulty retry loop has doubled their costs.
4. Adoption Recommendations
For enterprise users, follow a “measure first, then tier, then solidify” path: first collect one to two weeks of real token consumption data from production; then tier models by task type, shifting high-frequency, low-complexity tasks to cheaper models; finally, productize prompt templates, caching strategies, and routing rules as platform capabilities. On cloud platforms such as Baidu AI Cloud, you can combine commodity services like object storage, message queues, logging, and monitoring to build a complete asynchronous batch inference and cost analysis pipeline—turning cost governance from “post-hoc reconciliation” into “real-time control.”
5. Conclusion
“0.2 RMB per task” isn’t just a marketing number—it’s a snapshot of how rapidly LLM inference costs are falling. What developers should really focus on is not the price of any particular model, but the underlying cost structure: understand asymmetric input/output pricing, master the cache hit mechanism, match model capability to task tiers, and drive continuous optimization through observability. With this methodology in hand, you can keep inference costs within business-acceptable limits regardless of how model prices evolve.
