A Practical Guide to LLM Selection and Inference Cost Optimization

    This guide explains how to choose large language models based on your own task distribution rather than public leaderboards, and how to control inference costs. It covers a five-dimension scoring framework, a formula for cost per successful task, four cost-reduction patterns (semantic caching, model routing, batching, and context governance), the observability metrics needed to support optimization, common pitfalls, and a five-week rollout plan.

    1. Why Call Volume and Spend Look So Different

    Global LLM usage statistics often show a striking pattern: domestic Chinese models occupy nearly half of the slots on call-volume leaderboards and account for close to 60% of total calls, yet their estimated share of spend is under 10%. Meanwhile, a small number of high-end overseas models take more than a third of total spend on a tiny fraction of the calls.

    These numbers describe two separate facts that should not be conflated:

    • Unit price gap: per-token prices differ by orders of magnitude. Domestic models are significantly cheaper per token than high-end overseas models.
    • Workload segmentation: high-volume workloads tend to be high-frequency, short-context, and tolerant of errors; high-spend workloads tend to involve long contexts, complex reasoning, and a strong dependency on output quality.

    So the question “which model is the most convenient to use?” has no single answer. The right question is: given my task distribution, which combination of models delivers the highest marginal return? The rest of this guide lays out a practical way to decide.

    2. Build a Selection Framework: Five Dimensions and Weights

    Rather than relying on impressions, teams should use a shared scorecard with the following dimensions:

    Dimension Key metrics Suggested weight
    Quality Pass rate on your business eval set, structured-output compliance rate 35%
    Cost Blended cost per 1,000 successful requests 25%
    Latency P50/P95 time to first token, end-to-end duration 15%
    Reliability Availability, rate-limit trigger rate, retry success rate 15%
    Engineering fit Context length, function calling, streaming output, ecosystem compatibility 10%

    Scoring sketch (pseudocode):

    1. def score(model, metrics, weights):
    2. return sum(
    3. normalize(metrics[d]) * w
    4. for d, w in weights.items()
    5. )
    6. # Note: normalize must be based on your own eval set,
    7. # not public leaderboard scores, to avoid distribution shift

    The key principle: your eval set must be sampled from real production traffic. Public leaderboards reflect general-purpose capability distributions, which often do not match your input lengths, domain terminology, or output format constraints.

    3. Cost Modeling: Turning “Cheap” into a Computable Number

    Do not compare unit prices directly. Compare cost per successful task:

    1. cost per successful task = (input tokens × input price + output tokens × output price)
    2. / first-attempt success rate
    3. × (1 + retry amplification factor)

    Three amplification factors that are easy to overlook:

    1. Retry amplification: at an 80% first-attempt success rate, actual token consumption is roughly 1.25× the theoretical value.
    2. Context inflation: in multi-turn conversations, history is billed repeatedly, and long sessions can multiply cost several times over.
    3. Wasted output: parse failures caused by non-compliant formats are a major source of hidden cost.

    On the engineering side, all three can be suppressed through prompt trimming, summarizing conversation history, and constraining structured output.

    4. Four Cost-Reduction Patterns and Implementation Notes

    4.1 Semantic caching

    Reuse results for high-frequency repeated requests. Set a similarity threshold and an expiration policy so stale results do not pollute the business.

    1. def get_answer(query):
    2. hit = cache.lookup(query, threshold=0.95)
    3. if hit and not hit.expired():
    4. return hit.payload
    5. ans = llm.invoke(query)
    6. cache.store(query, ans, ttl=3600)
    7. return ans

    4.2 Model routing

    Split traffic by task difficulty: simple classification, extraction, and rewriting go to lightweight models; complex reasoning and long-form generation go to high-capability models. Routing can be implemented with rules, a small classifier, or a confidence threshold.

    4.3 Batching and asynchronous processing

    Merge requests for offline workloads and use a queue to smooth out peaks. This improves resource utilization and lowers unit cost noticeably.

    4.4 Context governance

    In retrieval-augmented scenarios, cap the number of retrieved chunks and the length of each chunk, keeping only passages highly relevant to the question. This is the first line of defense for long-context cost control.

    5. Observability: No Measurement, No Optimization

    A minimum viable monitoring checklist:

    • Business: call volume, success rate, first-attempt success rate, retry rate
    • Cost: input/output token distribution, cost per task, cache hit rate
    • Quality: format compliance rate, human spot-check scores, regression eval pass rate
    • Performance: P95 latency, rate-limit and error-code distribution

    Put the cost dashboard next to the quality dashboard. Looking at cost alone pushes teams to sacrifice quality; looking at quality alone lets the budget run out of control. The two must be on the same screen.

    6. Common Pitfalls

    • Substituting leaderboard rankings for your own eval: mismatched distributions lead to misleading conclusions.
    • Watching unit price but not success rate: a low unit price with a low success rate often means a higher total cost.
    • Switching everything at once: roll out gradually and keep a rollback switch.
    • Ignoring output parsing cost: unstructured output shifts the cost of manual rework onto the business side.

    7. Suggested Rollout Plan

    1. Week 1: sample real traffic and build a business eval set of 200–500 items.
    2. Week 2: run baseline evals and cost modeling for 2–3 candidate models.
    3. Weeks 3–4: add routing and caching, and roll out to 10% of traffic.
    4. Week 5 onward: ramp up gradually based on monitoring data and set up a monthly re-evaluation cadence.

    If your organization already uses Baidu AI Cloud capabilities, you can consolidate model calls behind a gateway layer and pair it with general-purpose services (log service, monitoring and alerting, message queue, object storage) to handle metering, rate limiting, and auditing, avoiding the governance overhead of integrating multiple vendors separately.

    Conclusion: model selection is not about picking the “strongest” model, but the combination that best matches your task distribution at a controllable cost. Quantify quality with your own eval set, quantify spend with cost per successful task, and optimize both at once with routing and caching. That is the sustainable engineering path.