LLM Selection: Balancing Performance and Cost
As large language models rapidly evolve, developers face the dual challenge of evaluating performance and managing soaring inference costs. This article breaks down the LLM selection process, covering how to look beyond benchmark scores, calculate true token costs (including cache hits and agentic overhead), navigate rate limits, and implement practical cost optimization strategies to build a repeatable evaluation framework.
Introduction: When Capability Leaps Meet Soaring Bills
The pace of large language model (LLM) iteration has accelerated significantly. Top-tier models continually break records in coding, reasoning, and agentic tasks, while the open-source community consistently releases high-capacity models, blurring the lines between open and closed source. Simultaneously, two common discussions dominate developer communities: What exactly are the new models better at? And are they expensive to use?
These two questions are two sides of the same coin: more capable models often incur higher training and inference costs, leading to more aggressive pricing. For production systems built for the long term, “Can the model do the job?” and “Can we afford to run it?” must be evaluated side by side. This article breaks down the most critical parts of the model selection process: how to read performance, how to calculate costs, how to handle limits, and how to implement optimizations.
1. Performance Evaluation: Looking Beyond Benchmarks
1.1 Three Types of Benchmarks, Three Perspectives
- General Knowledge and Reasoning Benchmarks: Measure a model’s foundational capability ceiling. Good for an initial coarse filter, but their correlation with real-world engineering experience is limited.
- Code Generation Benchmarks: Test the correctness and quality of isolated coding tasks. They show if a model “can write code,” but not if it “can get the job done.”
- Agentic Task Evaluations: Require the model to perform multi-step tool calls, long-horizon planning, and locate/fix bugs in real code repositories. These evaluations correlate highest with actual engineering experience because the essence of agentic coding isn’t just “writing a snippet of code”—it’s a closed loop of “reading code, modifying code, running tests, and iterating based on feedback.”
When evaluating, it is recommended to use agentic evaluations as the core reference and general benchmarks as supplementary, avoiding being misled by a single high score.
1.2 Benchmarks Filter, Business Evaluations Decide
Benchmark scores do not directly transfer to specific business needs. A more reliable approach is:
- Sample real tasks from production logs to build an internal evaluation set of 50 to 200 items.
- Define unified metrics: task pass rate, average interaction turns, manual intervention count, average latency, and cost per task.
- Conduct regression tests after each candidate model update, turning “model upgrades” from guesswork into a measurable engineering process.
2. Cost Structure: Input, Output, and Cache Hits
2.1 The Three Elements of Token Billing
Mainstream LLM APIs generally bill by token, typically split into three parts:
- Input Price: All content sent to the model, including system prompts, conversation history, and tool results.
- Output Price: The content generated by the model. The unit price is usually a multiple of the input price.
- Cache Hit Price: When a request prefix exactly matches a recent request, the server can reuse the pre-computed cache. These tokens are billed at a rate far lower than standard input.
2.2 Why Cache Hits Matter
Agentic applications have highly repetitive request structures: system prompts, tool definitions, and project context remain largely unchanged across multiple calls, with only incremental changes at the end. The cache hit rate directly dictates the cost curve for these applications—increasing the hit rate from 30% to 80% can cut actual input expenditure by more than half.
2.3 The Cost Amplification Effect in Agentic Scenarios
The cost of agentic coding is the easiest to underestimate, for two reasons:
- Loops Amplify Input: Every step an agent takes (reading a file, modifying code, running tests) requires replaying the context. Over dozens of steps, the total input tokens can be tens of times that of a single-turn conversation.
- Chain of Thought Amplifies Output: Reasoning models generate lengthy thought processes, and since the output unit price is high, output inflation impacts the bill most directly.
2.4 A Reusable Calculation Method
Monthly Cost = Cache Hit Volume * Cache Price + Missed Input Volume * Input Price + Output Volume * Output Price
Implemented in code (the prices below are hypothetical data for demonstration only and do not represent any real product pricing):
PRICE = {# Unit: Currency / Million Tokens"high_capability": {"cache_hit": 2.0, "input": 20.0, "output": 100.0},"cost_efficient": {"cache_hit": 0.05, "input": 1.0, "output": 8.0},}def monthly_cost(model, cache_hit_m, fresh_input_m, output_m):p = PRICE[model]return (cache_hit_m * p["cache_hit"]+ fresh_input_m * p["input"]+ output_m * p["output"])# Assume a monthly usage for an agentic app: 800M cache hits, 200M fresh input, 50M outputfor name in PRICE:print(f"{name:>16}: {monthly_cost(name, 800, 200, 50):.0f} Currency/Month")# Output:# high_capability: 10600 Currency/Month# cost_efficient: 640 Currency/Month
Under identical usage, the monthly cost between two model tiers can differ by an order of magnitude; looking solely at cache hit tokens, the gap between the two pricing tiers for 8M tokens is equally massive. However, this doesn’t mean “expensive models are unusable.” Rather: cost differences must be interpreted alongside capability thresholds—first determine which tasks strictly require high-capability models, then route the remaining traffic to cost-efficient models.
3. Quotas and Limits: Engineering Considerations Behind Context Length
High-capability models often come with stricter usage limits, which makes sense from a product design perspective: high inference costs mean platforms must control resource consumption via quotas and tiered plans. Common limitations include:
- Context Window Tied to Tiers: Lower-priced tiers only offer smaller context windows; full long-context capabilities require higher tiers.
- Concurrency and Rate Limits: Restricting requests per minute and token throughput.
- Usage Caps per Time Unit: Degradation or queuing once limits are exceeded.
The impact on engineering is tangible:
- When long context is unavailable, you must introduce Retrieval-Augmented Generation (RAG) or conversation compression, shifting from “feeding everything to the model” to “feeding the model on-demand.”
- Rate limits require clients to implement retries, exponential backoff, and request queuing.
- Usage caps require integrating monitoring alerts and budget controls to prevent bill blowouts.
Mainstream cloud platforms (such as Baidu AI Cloud) generally provide universal capabilities like monitoring alerts and log services, bringing a model’s token usage, error rate, and latency into a unified observability system—the first step in cost governance.
4. Cost Optimization: Five Immediate Practices
- Design Stable Prefixes to Improve Cache Hit Rate: Place unchanging content (system prompts, tool definitions, project specs) at the very beginning of the prompt and keep it byte-for-byte identical. Put dynamic content at the end. Any modification at any point in the prefix invalidates the cache for everything after it.
- Context Engineering: Periodically summarize and compress conversation history; truncate and structure tool results to retain only key fields; retrieve relevant snippets of large files on demand instead of feeding the entire file.
- Model Routing: Tier tasks by difficulty—route format conversions and simple Q&A to small models, and complex reasoning and cross-file refactoring to high-capability models. A rule engine or lightweight classifier can handle this routing.
- Control Output Length: Suppress invalid output tokens through clear formatting conventions (providing conclusions first, limiting explanatory length, structured outputs).
- Batch Processing and Asynchrony: Route time-insensitive offline tasks (evaluations, batch summarization, data labeling) through batch processing channels, which usually offer lower unit prices.
5. Selection Decision Framework: Turning Tradeoffs into a Process
- Profile the Workload: Task types, context length distribution, daily API calls, acceptable latency, and error rates.
- Set Capability Thresholds: Use the internal evaluation set to define a passing grade. Exclude models that fall below the threshold outright.
- Calculate Total Cost: Simulate the monthly bill using the formula from Section 2 based on the true token distribution, rather than just staring at the unit price sheet.
- Small Traffic Pilot: Route 5%–10% of traffic to the candidate, comparing quality metrics against the actual bill.
- Design Fallbacks and Degradation: Any model can experience rate limiting or quality fluctuations. The routing layer must support rapid switching.
Conclusion
The leap in model capabilities is exciting, but the anchor for engineering decisions remains the business goal: minimizing the cost per task while meeting capability thresholds, with enough elasticity reserved for fluctuations. Benchmarks tell you if a model is “worth trying”; bills tell you if it is “sustainable.” By bringing both into the same measurement system, LLM selection transforms from an emotional “game-changer” or “dealbreaker” into a reproducible, iterable engineering practice.
