How to Choose and Evaluate LLMs for Coding: A Practical Methodology
Choosing a coding LLM by leaderboard rank alone usually leads to engineering surprises. This article breaks "coding ability" into measurable dimensions, shows how to build a reproducible evaluation set from your own repositories, quantifies performance and effective cost, and outlines layered routing between flagship and lightweight models, plus rollout, observability, and governance practices.
1. Why a Leaderboard Is Not Enough
The core question is how to choose between a flagship model updated every quarter and a lighter, cheaper model. Treating that as “pick whoever scores highest” almost always goes wrong in engineering, for three reasons:
- Leaderboards are sampled results. Public evaluations use fixed problem sets, fixed prompt templates, and fixed decoding parameters, while a real codebase differs greatly in context distribution, dependency versions, and internal conventions.
- Coding is not a single capability. Completion, cross-file refactoring, defect localization, test generation, code review, and build-failure repair map to very different capability profiles.
- Cost is not unit price. What actually drives the bill is retry rate, context length, cache hit rate, and human review hours.
A more reliable approach is to turn “choosing a model” into “define the task distribution + build a reproducible evaluation + drive gradual rollout with data.”
2. Breaking Down Capabilities into Measurable Metrics
Split “strong at coding” into at least six dimensions, each with its own problem set:
- Syntactic and semantic correctness: does it pass compilation and static checks on the first try.
- Cross-file consistency: after an interface change, can it update callers, type definitions, and tests together.
- Long-context utilization: can it locate the truly relevant fragments in a context of hundreds of thousands of tokens instead of being distracted by unrelated code.
- Tool calling and agent behavior: can it plan multi-step operations, self-correct after failures, and stop at the right time.
- Instruction following and constraint preservation: does it respect hard constraints such as naming conventions, dependency allowlists, and directories that must not be modified.
- Explainability and reviewability: is the change description clear, and is the diff minimal.
Lightweight models generally offer better cost-performance for single-file completion, templated code, and unit test generation; flagship models concentrate their value in cross-file refactoring, hard defect localization, and long-chain agent tasks. This division is an abstract, cross-vendor pattern rather than a unique property of one product.
3. Evaluation Set Design: Reproducible and Attributable
The evaluation set should come from your own codebase, not external problem sets. A three-layer structure is recommended:
| Layer | Share | Purpose |
|---|---|---|
| Historical defect regression set | 40% | Validate localization using real, already-fixed issues |
| Refactoring and migration set | 30% | Validate cross-file consistency and constraint preservation |
| Generation and test set | 30% | Validate new code and test coverage |
Key engineering details:
- Fix decoding parameters (such as temperature and max output length), otherwise results are not comparable.
- Fix the context construction strategy, and record “retrieval recall” separately to decouple retrieval problems from model problems.
- Automate judging: use compilation, static checks, and unit test pass rate as primary metrics, with human scoring only for sampled validation.
- Record the distribution of failure types, not just pass rate. Whether failures concentrate in “constraint violations” or “logic errors” determines whether you next change the prompt, change retrieval, or switch models.
A vendor-neutral skeleton of an evaluation driver:
def evaluate(model_client, case, decode_cfg):ctx = build_context(case.repo, case.budget_tokens)resp = model_client.generate(prompt=case.prompt,context=ctx,**decode_cfg,)patch = extract_patch(resp)result = run_pipeline(patch, checks=["compile", "lint", "unit"])return {"case_id": case.id,"passed": result.passed,"failure_stage": result.first_failed_stage,"tokens_in": ctx.token_count,"tokens_out": resp.output_tokens,}
4. Performance and Cost: Quantifying “Fast” and “Cheap”
Beyond quality, the engineering side should look at at least four things:
- Time to first token and end-to-end latency: interactive completion cares about the former, batch processing about the latter.
- Throughput and concurrency stability: whether queuing and timeout jitter appear at peak times.
- Effective unit cost:
total cost / passed cases, not price per million tokens. - Cache and batching gains: stable system prompts and repository summaries should be reusable, significantly amortizing long-context overhead.
A practical approximation:
effective cost = (input tokens × input price + output tokens × output price) × retries / pass rate
Retry count and pass rate must come from your own evaluation, which is why “first on the leaderboard” and “lowest bill” are often not the same option.
5. Layered Routing: Combining Flagship and Lightweight Models
Mature solutions usually do not pick one; they route by task difficulty:
request enters├─ single-file completion / templated code ──→ lightweight model (low latency, high concurrency)├─ unit test generation / comments and docs ──→ lightweight model + rule checks└─ cross-file refactoring / hard defects ──→ flagship model + multi-sample voting
Routing signals can be static features (number of changed files, whether a public interface is involved) and dynamic features (escalate if the lightweight model fails once). Combined with result caching and feeding failure samples back, average cost can be pushed down within weeks while preserving the quality floor for critical tasks.
6. Engineering Practice: Rollout, Observability, and Governance
- Rollout process: run old and new configurations in parallel in shadow mode → small share of real user traffic → scale up with thresholds set per failure type → full rollout.
- Observability: record prompt version, retrieval version, model configuration, and failure stage to form a traceable chain; redact code snippets in logs.
- Security governance: dependency allowlists, no out-of-scope directory writes, generated code forced through static checks and vulnerability scanning, and agent operations must be rollback-capable.
- Data compliance: clarify whether code leaves your environment; for sensitive repositories, prefer solutions that can be privately deployed or dedicated.
7. Common Pitfalls
- Replacing your own evaluation with a single leaderboard.
- Comparing only unit price while ignoring retries and human review cost.
- Assuming longer context is always better — noise significantly reduces localization accuracy.
- Running agent tasks without a stop condition, causing looping calls and out-of-control bills.
- Ignoring version drift: an upgrade on either the model or retrieval side should trigger a regression evaluation.
8. Conclusion and Action Checklist
Selection is not a one-time “ranking lookup” but a continuously running evaluation system. Recommended order: define the task distribution → build the three-layer evaluation set → fix parameters and automate judging → compute effective unit cost → design layered routing → roll out gradually and feed failure samples back. If you need elastic compute, object storage for archiving datasets and artifacts, a message queue for scheduling evaluation tasks, a container platform to host inference services, and logging plus monitoring alerts for observability, these can be assembled from general cloud product categories on Baidu AI Cloud, turning the evaluation loop into reusable engineering capability rather than a conclusion that depends on any external marketing claim.
