LLM Selection for Code Generation and Agent Tasks: Success Rate, Cost per Success, and Long-Run Stability
Selecting an LLM for code generation and agent workloads requires more than leaderboard scores. This article presents a reproducible evaluation approach based on three numbers: per-task success rate, actual cost per successful task, and stability under long-running workloads. It covers fixed-task long-horizon code generation tests, a local CLI self-feedback loop with safety boundaries, cost and quota estimation, and a multi-agent stability checklist with suggested metrics and logging fields.
1. Start With the Conclusion
When selecting a model, prioritize three numbers: the success rate for a single task, the actual cost per successful task, and stability under long-running workloads. Leaderboard scores can be used for initial screening, but they cannot replace testing on your own tasks. This article provides a reproducible evaluation method covering four parts: code generation, local self-feedback, cost estimation, and multi-agent testing. All numbers in this article are examples; readers should replace them with their own measurements.
2. Long-Task Iteration: Evaluate Code Generation with a Fixed Task
Generating a single function once does not show whether a model can complete a real project. Long tasks test whether the model can continuously iterate on existing code, maintain style consistency, and correctly handle dependencies between the frontend and backend.
It is recommended to use a fixed sample task for horizontal comparison, for example, “build a financial report monitoring web app.” The task description should include the following:
- Functional scope: display quarterly revenue and net profit curves for a specified company, support filtering by quarter, and highlight data with a year-over-year change exceeding 30%.
- Tech stack: use a mainstream component framework for the frontend and a lightweight framework such as FastAPI or Express for the backend. Write dependency versions into a lock file.
- Data source: use anonymized or public historical financial report samples; do not connect to real internal data.
- API contract: for example,
GET /api/reports?company=...&quarter=.... Field names, field types, and error codes must be defined in advance.
Each test must record the model name and version, temperature parameter, context length limit, number of interaction rounds, and number of human interventions. Without these records, two results cannot be compared.
Acceptance criteria should be divided into three layers:
- Can it start: after dependencies are installed in a clean environment, the frontend and backend can start normally.
- API correctness: requests are sent according to the contract, and the response structure and field types all match.
- Tests pass: the pass rate of the automated test suite. For low-risk projects, 90% can be set as the passing line; the specific value should be adjusted according to project risk.
After an iteration, if the model changes an API field that had already passed, record it as a regression failure. The number of regression failures reflects long-task capability better than single-generation quality.
3. Self-Feedback Loop for Local Command-Line Tools
The self-feedback process can be split into four steps: receive the task, execute, read logs, and correct. The following script shows the execution layer of the loop. It runs tests, saves the output to a log file, and captures the last 50 lines for the model to read:
#!/usr/bin/env bashset -euo pipefailmkdir -p logsLOG="logs/run_$(date +%Y%m%d_%H%M%S).log"pytest -q tests/ > "$LOG" 2>&1 || truetail -n 50 "$LOG"
The model uses these logs to locate errors, modify the code, and run again. The loop needs clear boundaries; otherwise, automatic modifications may cause damage:
- Runtime environment: execute in a container or virtual machine, not directly under the home directory of the development machine.
- Write scope: only allow modification of source code and test files inside the project directory; modifying system configuration and the user home directory is prohibited.
- Network access: disabled by default; only temporarily open specified package sources when installing dependencies.
- High-risk operations: actions such as deleting files, pushing code, and changing permissions must be confirmed by a human.
- Stop conditions: if the same error appears twice consecutively, or if iterations exceed 5 rounds, stop and hand over to a human.
Stop conditions are important. When a self-feedback loop encounters a problem it cannot solve, a common failure mode is repeatedly modifying the same piece of code, consuming many calls without making progress.
4. Cost and Quota: Calculate per Successful Task
A common mistake in model selection is comparing only the per-call price. A task usually contains multiple calls and may retry after failures. A more reasonable metric is the cost per successful task, calculated as follows:
Single-task cost = sum over all calls in the task of (input tokens × input unit price + output tokens × output unit price)
Cost per successful task = average single-task cost ÷ task success rate
Assume two models are tested on the same set of 20 tasks, with the following results (numbers are examples):
| Model | Average cost per task (CNY) | Task success rate | Cost per successful task (CNY) |
|---|---|---|---|
| Model A | 0.50 | 80% | 0.63 |
| Model B | 0.90 | 88% | 1.02 |
By per-call price, Model A is cheaper. By cost per successful task, Model A is still lower. However, failed tasks from Model A require more rework. If each manual fix takes 30 minutes, labor cost may offset the cost difference between the two models. Therefore, cost analysis should also record human intervention time.
Quota also needs to be estimated in advance. Suppose a team runs 5 agents simultaneously, each agent initiates an average of 60 requests per hour, and there are 3 one-hour peak periods per day. The peak number of requests per hour is 5 × 60 = 300. Compare this number with the provider’s limit to determine whether the quota is sufficient. When quota is insufficient, common consequences include throttled requests, queued tasks, and increased overall time.
5. Multi-Agent Stability Checklist
In a multi-agent system, the model acts as an orchestration center, managing tool calls and sub-agents. Before integration, check each item in the table below. The thresholds in the table are suggested values and can be adjusted according to business requirements.
| Check item | Metric | Suggested threshold | Suggested log fields | Failure handling |
|---|---|---|---|---|
| Long-context adherence | Proportion still following key constraints after turn 50 | No lower than 95% | turn_id, constraint_id, hit/miss | Reinject system instructions and validate again |
| Response timeout | P95 response time | Below the configured timeout threshold | latency_ms, model, retry_count | Exponential backoff retry, up to 3 times |
| Instruction following | Proportion of structured outputs passing validation | No lower than 98% | schema_valid, error_type | Retry once after validation failure; if it still fails, hand over to a human |
| Rate limiting | Proportion of 429 responses | Below 1% | status_code, wait_ms | Reduce concurrency, wait, then retry |
| Environment awareness | Proportion of startups that correctly identify the system, directories, and dependency versions | 100% |
