LLM Cost Optimization: A Practical Guide to Model Selection and Tiered Routing
This article explains how engineering teams can treat large language model selection as a quantifiable cost optimization problem. It covers building a reusable cost formula, classifying tasks into tiers, evaluating models on real traffic rather than public benchmarks, and applying architectural cost-saving techniques such as prefix caching, batching, cascade routing, output length control, and observability. Readers will also find common pitfalls to avoid and a conclusion on decoupling capability from cost into a schedulable resource pool.
1. Background: Why Cost-Effectiveness Is the First Constraint in LLM Adoption
As enterprise applications move into large-scale production, the central tension in model selection has shifted from “can it do the job?” to “can we afford to run it?” When call volume grows from a few thousand requests per day to hundreds of millions, small differences in unit price are amplified into substantial annual spending differences. Engineering teams therefore need to reframe selection as a quantifiable optimization problem: minimize the cost per task while satisfying quality constraints.
The two low-cost model tiers discussed in the source material reflect a product segmentation strategy common across the industry: the flagship tier pursues the upper bound of capability, the mid-tier balances capability and price, and the low tier covers high-frequency, short-chain scenarios with very high throughput and very low unit price. Understanding the engineering implications of this segmentation is more valuable than memorizing any specific model name.
2. Build a Cost Model: Do the Math First
Any selection discussion should start from a reusable cost formula:
Cost per task =input tokens × input unit price+ output tokens × output unit price+ retry count × cost per task- discount benefit from cache hits
Cache hits are the most easily overlooked lever. Most platforms offer significant discounts for repeated prefixes (system prompts, long documents, tool definitions), so placing stable, unchanging content first and variable content last is a zero-cost optimization.
Illustrative code (pseudocode, expressing only the tiered routing idea):
def route(task):if task.risk == "high" or task.chain_length > 50:return flagship_model # prioritize capability upper boundif task.latency_sla < 0.5 and task.output_tokens < 200:return low_cost_model # prioritize throughput and unit pricereturn mid_model # default balanced tier
3. Task Classification: Define “Good Enough” Precisely
The prerequisite for cost-effectiveness optimization is strict task classification. In practice, tasks fall into four categories:
- High-frequency short-response tasks: intent recognition, tag extraction, format conversion, simple Q&A. Low demand on the capability ceiling, extremely sensitive to latency and unit price, suitable for the lowest tier.
- Medium-complexity business automation: structured extraction, multi-turn form filling, routine code completion. Requires stable instruction following; the mid-tier is usually sufficient.
- Long-chain knowledge work: large codebase refactoring, cross-document analysis, complex reasoning. Must rely on the flagship tier; downgrading directly amplifies rework costs.
- High-risk scenarios: decisions involving compliance, finance, or healthcare. Capability ceiling and auditability should be the first priority, with cost taking a back seat.
Key takeaway: the unit price saved by downgrading can be offset by rework, manual review, and loss of trust caused by a single error. “The cheapest option that is good enough” must therefore include a quality floor constraint rather than a pure price comparison.
4. Evaluation: Don’t Rely on Benchmarks Alone
Public leaderboards are only useful for initial screening. A practical approach is to build a tiered evaluation set on real business traffic:
- Sample the real request distribution rather than hand-crafted “nice-looking examples”;
- Record both quality metrics (accuracy, structured output validity rate) and economic metrics (cost per thousand tasks, P95 latency);
- Introduce a human-correction feedback loop and track the “proportion corrected by users,” a core metric closer to perceived quality than accuracy;
- Measure “completion rate” separately for long-chain tasks rather than single-step correctness, to avoid local correctness with overall failure.
5. Cost Reduction at the Architecture Level
- Prefix caching and prompt normalization: fix system instructions, tool descriptions, and examples to maximize cache hits.
- Batching and asynchronous processing: use low-priority queues for offline tasks, trading latency for unit price.
- Cascade routing: first use a low-cost model to produce a draft and confidence score, then escalate low-confidence samples to the higher-priced model; this typically reduces cost significantly with almost no quality loss.
- Output length governance: cap maximum output tokens and require concise formats; since output unit price is usually higher than input, compressing output yields clear benefits.
- Observability: bring token consumption, cache hit rate, and escalation rate into monitoring, alerting, and logging services to form a cost dashboard.
6. Common Pitfalls
- Running all traffic on the flagship model, leading to runaway costs;
- Looking only at unit price while ignoring rework rate, causing total cost to rise instead;
- Overlooking context inflation: long contexts accumulate high input costs even at a low unit price;
- Lacking a downgrade switch and canary mechanism, making it impossible to roll back quickly when quality fluctuates.
7. Conclusion
The engineering essence of model segmentation is decoupling “capability” and “cost” into a schedulable resource pool. Teams should establish a cost formula, task classification standards, and a closed-loop evaluation process on real traffic, then use caching, batching, and cascade routing to concentrate the budget where high capability is genuinely needed. To carry these strategies in an integrated way, teams can build an elastic inference gateway on top of general-purpose capabilities such as the container platform, message queue, object storage, and monitoring and alerting offered by Baidu AI Cloud, achieving a continuous balance between cost and quality.
