LLM Inference Cost Optimization: An Engineering Guide to Lower Cost Per Task
This article explains why LLM inference cost should be measured per completed task rather than by per-million-token price. It breaks down the pricing levers—input/output price gaps, context caching, and reasoning effort tiers—then covers output token efficiency, context-length discount conditions, and defensive execution risks in low-cost models. It also includes an evaluation script skeleton and a practical model-selection checklist for reducing inference costs without sacrificing acceptable quality.
1. The Real Problem: Cost Per Task, Not Price Per Token
When evaluating whether a large language model service is cost-effective, the most common mistake is focusing only on the listed price per million tokens. The actual bill is determined by four variables:
Cost per task ≈ (input tokens × input price + cached tokens × cache price+ output tokens × output price) / task success rate
The “task success rate” is the most easily overlooked factor. If a model needs multiple rounds of rework to deliver, actual consumption multiplies, and even a low unit price no longer holds. Cost optimization therefore requires looking at three things at once: pricing structure, token usage efficiency, and first-pass delivery rate.
2. Three Levers in the Pricing Structure
The first is the price gap between input and output. For most mainstream services, the output unit price is several times the input price, which means “making the model write less fluff” saves more money than “making the model read less context.”
The second is cached input. For repeated prefixes such as system prompts, long documents, and code repository indexes, enabling context caching can reduce the unit price to a fraction of the standard input price, often with a reduction of more than 70%. The engineering task is to place stable content first and volatile content last, ensuring byte-level consistency of the prefix; otherwise, the cache will not hit.
The third is the reasoning effort tier. Most services offer multiple configurations ranging from fast to high-effort tiers. High-effort tiers consume more thinking tokens; the unit price stays the same, but total volume rises. On some tasks, however, the cumulative cost of repeated trial and error at a lower tier is higher than getting it right in one pass at a higher tier.
3. Token Efficiency: The Underrated Cost Factor
Output token efficiency refers to the output length required to complete the same task. High-efficiency models often express the same information with fewer tokens, directly offsetting a unit-price disadvantage. For evaluation, it is recommended to fix a set of 20–50 real business samples and record three metrics: average output tokens, tool-call rounds, and manual correction rate. Only by putting these three alongside price in the same table does a meaningful horizontal comparison become possible.
A skeleton of an illustrative evaluation script:
def evaluate(case, model, reasoning="medium"):r = call_model(model, case.prompt, reasoning=reasoning)return {"in": r.prompt_tokens,"cached": r.cached_tokens,"out": r.completion_tokens,"rounds": r.tool_rounds,"passed": check(case.golden, r.text),}# When aggregating, compute cost per task = total spend / number of passed samples
4. Context Length and Discount Conditions
Low-price tiers often come with context window limits—for example, a discounted price applies only when the input context is below a certain threshold. For long-document scenarios, design segmentation, summarization, and retrieval-augmented strategies in advance, splitting oversized content into “a cacheable stable prefix + concise relevant snippets.” This avoids falling into the higher-price range and also reduces attention dilution from irrelevant context.
5. Reliability Risk: Defensive Execution
Low-cost small models tend to exhibit “defensive execution” on long-chain tasks: repeatedly confirming scope, declaring work incomplete, and delaying the production of deliverable files. The cost of such behavior is hidden—more rounds, accumulating tokens, and human intervention. The mitigation is to engineer the workflow:
- Define deliverables and acceptance criteria clearly, and write them into the system prompt;
- Enforce structured output, writing to disk before reporting;
- Use workflow orchestration to split large tasks into verifiable small steps, each with idempotent writes;
- Add validation and rollback for critical write operations, with logging services and monitoring alerts to track failure rates.
{"task": "annotation", "deliverable": "file_path", "steps": 3, "must_write": true}
6. Model Selection Checklist
- Run benchmarks on real samples rather than relying only on leaderboard intelligence scores;
- Separate the “cacheable prefix” from “dynamic content” to maximize cache hits;
- Use lower tiers for simple tasks and higher tiers for complex tasks, routing by difficulty;
- Factor the rework rate into total cost, and set a red line for first-pass delivery rate;
- Build a cost dashboard that aggregates tokens and spend by business line.
7. Conclusion
Cost-effectiveness is not a unit-price competition but the product of “pricing structure × token efficiency × delivery success rate.” Making caching, tier routing, and workflow constraints solid, together with unified observability using general container, logging, and monitoring capabilities from platforms such as Baidu AI Cloud, is what allows inference costs to be continuously reduced within an acceptable quality boundary.
