Engineering Practices for Reducing LLM Inference Costs

    A practical guide to lowering LLM inference costs by focusing on cost per task rather than price per token. It breaks down the cost model, explains where token usage differences come from, shows how to evaluate models on both quality and cost, and provides an actionable optimization checklist covering routing, context management, caching, output constraints, observability, and elastic scaling.

    1. The Symptom: Same Unit Price, Much Smaller Bill

    With two generations of flagship models releasing close together, the hottest industry discussion is not about leaderboard rankings but a simpler question: why does the same task cost several times more with one model than another? Once you abstract away the specific products, you get a purely engineering conclusion: the cost of a task equals the unit price multiplied by the number of tokens actually consumed. The unit price is set by the price list, while usage is determined by how the model “gets things done.” When unit prices across two generations are close, nearly the entire gap in the bill comes from usage.

    This explains a common misjudgment: selecting a model based only on the listed price per million tokens often leads to the wrong conclusion. What really drives annual spending is cost per task, not price per token.

    2. The Cost Model: Breaking the Bill into Optimizable Variables

    A token is the smallest unit of text a model processes. Input is what you send in, and output is what the model generates. The two are usually priced separately, with the output price significantly higher than the input price. So:

    1. Cost per task = input tokens × input price + output tokens × output price
    2. Cost per unit of work = cost per task × number of retries
    3. Monthly budget ≈ cost per unit of work × task volume × (1 + overhead factor)

    The “number of retries” and the “overhead factor” are the most easily overlooked. Multi-turn tool calls, repeatedly refilling context, and rerunning failed jobs all push the actual bill well above the theoretical value. The optimization order should be: first cut usage, then discuss unit price, and only then negotiate.

    3. Where Usage Differences Come From: Planning Quality and Tool Selection

    For the same problem, a several-fold difference in token consumption usually does not mean “the wording got shorter” but that the path got shorter. There are four common sources:

    • More accurate task planning: decompose first, then execute, reducing ineffective exploration and repeated trial and error;
    • More accurate tool selection: choosing the right retrieval, computation, or code execution tool on the first try, avoiding multiple rounds of probing;
    • Cleaner context: carrying only the necessary fragments, reducing repeated billing on long context;
    • More restrained output: no redundant explanations or repeated code, so output tokens drop noticeably.

    One signal worth noting: on comprehensive tasks close to real projects, as the new generation’s score improves, output tokens actually drop by about 20%. This shows that capability gains and cost reductions can happen in the same direction, rather than being an inevitable trade-off.

    4. Evaluation Criteria: Look at the Quality Board and the Cost Board Together

    Looking only at the capability leaderboard leads to misjudging cost-effectiveness. A more reliable approach is to use the same set of problems, the same reasoning tier, and the same prompts, and record three metrics at the same time:

    Metric Meaning Purpose
    Task score Upper bound on quality Decide whether it is good enough
    Cost per unit of work Cost-effectiveness Decide whether you can afford it
    Completion latency Experience and throughput Decide whether it can go live

    A rule of thumb from experience: the top model on the capability board and the top model on the cost board are often not the same tier; the mid-tier model already approaches the previous generation’s flagship level on most business tasks, at a cost that may be only a fraction of it. For the vast majority of enterprise scenarios, the mid-tier is the default starting point, with the flagship reserved for difficult long-tail cases.

    5. An Actionable Optimization Checklist

    1. Tiered routing: send simple requests to the small model and escalate complex requests to the large model, using rules or a lightweight classifier for the upfront decision;
    2. Context governance: deduplicate, truncate, and structure retrieved fragments, avoiding feeding an entire document in repeatedly;
    3. Cache reuse: cache results for stable prefixes and high-frequency questions to cut repeated input;
    4. Output constraints: use structured output to limit length and reduce ineffective generation;
    5. Observability: bring token usage, retry rate, and cost per task into logging services and monitoring alerts, with bill attribution by business line;
    6. Elastic capacity: run inference services on a container platform with on-demand scaling, combined with a message queue to smooth peaks, avoiding paying for peak capacity all the time.

    An illustrative routing logic:

    1. def route(prompt, tools):
    2. # Low complexity goes straight to the small model, avoiding unnecessary high-cost inference
    3. if len(prompt) < 512 and not tools:
    4. return small_model(prompt)
    5. return large_model(prompt, tools)

    6. Common Pitfalls

    • Treating unit price as cost: the same unit price does not mean the same bill;
    • Testing only capability, not cost: you cannot answer “can it scale?”;
    • Ignoring retries: rerunning failed jobs is often a hidden major cost;
    • Blindly going for the flagship: most tasks meet the bar with the mid-tier; the premium buys the long tail.

    7. Conclusion

    In engineering practice on platforms such as Baidu AI Cloud, cost governance is already a core part of putting models into production: with cost per unit of work as the primary metric, combined with tiered routing, context governance, and full-chain observability, you can bring spending down without compromising quality. The key to model selection is not finding the “strongest model” but finding the right position on the cost curve for each type of task.