LLM Selection and Evaluation: Balancing Accuracy, Latency, and Cost
This article explains how to treat large language model selection as a multi-objective optimization problem rather than a search for the single "strongest" model. It covers building a trustworthy evaluation set, defining a four-dimension metric system (accuracy, latency, token consumption, and cost), and combining these into a comparable cost model with sample Python code. It also shows how to read version-to-version results, avoid common evaluation pitfalls, and put a repeatable evaluation loop into practice under a limited budget.
1. The Real Problem: Choose the “Most Suitable,” Not the “Strongest”
Under a limited budget, the constraint of LLM selection shifts from “capability ceiling” to “usable capability per unit cost.” A model that leads by a few points on a public leaderboard does not necessarily lead on your own business corpus. Likewise, differences in response latency and token consumption often affect user experience and billing more directly than a slight gap in accuracy. The rational approach is to treat selection as a multi-objective optimization problem: above an acceptable quality threshold, minimize latency and cost.
2. The Evaluation Set: The Source of Trust in Every Conclusion
The credibility of any evaluation is capped by the quality of its dataset. Follow four principles:
- Scenario alignment. Evaluation samples must come from the real business distribution, not a general public question bank. Samples from a customer-service scenario and a code-completion scenario are almost never reusable across each other.
- Layered coverage. Split by business domain (for example, language and instruction following, reasoning and math, code and tool calling, domain knowledge Q&A), and ensure each layer has enough samples to avoid noisy conclusions from small samples.
- Controlled difficulty distribution. Record the ratio of easy/medium/hard samples so that, when comparing across versions, you can tell which layer an improvement came from.
- Version freezing. Assign a version number to the evaluation set and make it read-only, preventing accidental contamination during iteration that would create a false “improvement” caused by data leakage.
For scale, tens of thousands of samples (around 15,000) are enough to support domain-level breakdowns. If resources are limited, keep at least a few hundred samples per sub-domain and attach confidence intervals to your conclusions.
3. The Metric System: All Four Dimensions Matter
- Accuracy (quality): Measure per domain, not just the overall score. A flat total score with domains moving in opposite directions is very common across version iterations.
- Average latency: Record P50/P95 alongside the mean, since the mean hides the long tail. Agent-style tasks make multiple calls, and the long tail gets amplified.
- Token consumption: Separate input from output, and track the fixed overhead introduced by prompt templates on its own.
- Cost: Determined jointly by token consumption and unit price; convert to cost per 1,000 calls for easy horizontal comparison.
4. Cost Model and Sample Implementation
Combine the metrics above into a single comparable number:
# Sample code: demonstrates the calculation logic only; unit prices must follow actual commercial quotesdef cost_per_1k_calls(avg_in_tokens, avg_out_tokens,price_in_per_1k, price_out_per_1k):per_call = (avg_in_tokens / 1000 * price_in_per_1k+ avg_out_tokens / 1000 * price_out_per_1k)return per_call * 1000# Example: 2200 input tokens, 650 output tokensprint(cost_per_1k_calls(2200, 650, 0.002, 0.006))
The minimal skeleton of an evaluation framework is just as simple; the key is to persist the latency, tokens, and judgment result of every call together so results stay traceable:
def run_eval(model_client, dataset):records = []for case in dataset:t0 = time.perf_counter()resp = model_client.chat(case.prompt)latency = time.perf_counter() - t0records.append({"domain": case.domain,"correct": judge(resp, case.golden),"latency": latency,"tokens": resp.usage.total_tokens,})return aggregate(records) # output accuracy / P95 / cost per 1k calls by domain
5. Version Comparison: How to Read a “Report Card”
The following illustrative data shows the analysis method (numbers are for demonstration only and do not represent any real product): in one version, accuracy rose from 66.6% to 68.7% with a large jump in ranking, average latency dropped from 98s to 59s, tokens fell from 2944 to 2851, and cost per 1,000 calls decreased from 38.8 to 37.6 (in local currency).
Reading this correctly takes three steps:
- Look at the total score first, then break it down by domain. A +2.1 point total may be driven by a large jump in a single domain, for example +7.6 points in one vertical domain, while language and instruction following and tool calling actually fell by 1–3 points.
- Reconcile official claims with measured results. If the vendor emphasizes improved agent capability but your measured tool-calling dimension drops, the optimization goal is misaligned with your business focus; this kind of contrast is more valuable for decisions than the total score.
- Distinguish “real optimization” from a “denominator change.” A 40% latency drop appearing together with only a 3% token drop usually means the inference pipeline or scheduling strategy improved, rather than simply generating less content; spot-check output completeness to rule out the false optimization of “faster because shorter.”
6. Common Pitfalls
- Looking only at the mean and not the long tail, so an agent pipeline’s doubled P95 latency goes unnoticed.
- Prompt templates not standardized, so different models use different system-prompt lengths and neither tokens nor accuracy are comparable.
- Judgment criteria too loose, counting semantic similarity as correct and inflating accuracy.
- Ignoring retries and failure rate, so the hidden cost of failed requests never enters the bill.
- One-off evaluation with no regression mechanism, leaving later version iterations untraceable.
7. Implementation Recommendations
Start with a small sample (a few hundred cases) for quick screening and keep 2–3 candidates; then run the full evaluation set for domain-level validation; finally use a canary release with real traffic while continuously collecting latency and cost. On the engineering side, connect the evaluation pipeline to logging services and monitoring alerts, and build dashboards for accuracy, P95 latency, and cost per 1,000 calls; the model service can scale elastically through a container platform to handle the long tail. If you use related capabilities from Baidu AI Cloud, design the architecture around generic categories (model service, object storage, logging service, monitoring alerts) to preserve replaceability and avoid tight coupling to a single vendor.
Conclusion: The optimal solution under a limited budget is not to find an “all-around champion,” but to build an evaluation loop of your own—using a frozen evaluation set, a layered metric system, and a transparent cost model to turn selection from a subjective impression into a reproducible engineering decision.
