Choosing an LLM for Code Generation: Long-Task Iteration and Multi-Agent Testing
This article is for development teams and technical leads who need to select code-generation tools. It presents a reproducible method for comparing large models on long-task iteration, quota consumption, and multi-agent collaboration. It covers test design and failure boundaries, a minimal financial-reporting monitoring case, token and cost metrics, multi-agent protocols and degradation, environment-awareness tests, a selection checklist, stress-test procedures, and rollback with human handoff. It does not cover benchmark score rankings, price negotiation, or security audits for production environments.
This article is intended for development teams and technical leads who need to select code-generation tools. The problem it addresses is how to use the same reproducible method to compare different large models on long-task iteration, quota consumption, and multi-agent collaboration. It does not cover benchmark score rankings, price negotiation, or security audits for production environments.
1. Long-Task Iteration: Test Design and Failure Boundaries
Long-task iteration means that a model completes a loop of “write, test, read errors, fix” within a single task until the tests pass. Before evaluating this capability, the test framework and log format must be fixed; otherwise, results from different models cannot be compared.
The test framework uses pytest and Playwright. After each round of modifications, the model runs pytest -q --junitxml=report.xml, and frontend interaction is executed with npx playwright test. The model does not read the full terminal output. Instead, it reads the names of failed cases, assertion content, and the file and line number of the error from report.xml. This prevents long logs from filling the context.
Sandbox permissions are minimized: read and write access is limited to the project directory; execution of pytest, npm, and python is allowed; external network access is prohibited, and only localhost is allowed; deleting or modifying files outside the project directory is prohibited.
There are three failure boundaries. First, any test failure means the current round did not pass. Second, if the same test case still fails after three consecutive repair rounds, the task is interrupted and handed over to a human. Third, if the total input and output tokens for a single task exceed a preset limit (for example, 2,000,000 tokens), it is recorded as an “over-limit” failure.
2. Minimal Reproducible Case: Financial-Reporting-Season Monitoring Page
This case tests whether a model can generate a runnable web application directly from requirements.
Technology stack: the backend uses Python 3.11 and FastAPI; the frontend uses native HTML, CSS, and JavaScript without a frontend framework to reduce variables introduced by dependencies.
Input sample: a CSV file containing data for 20 companies, with fields company (company name), quarter (quarter, such as 2026Q3), revenue (revenue, in units of 10,000 CNY), and yoy (year-over-year growth rate, in percent).
Directory structure: backend/main.py provides the API; backend/tests/test_api.py contains API tests; frontend/index.html is the page; frontend/app.js handles filtering logic; tests/e2e/test_page.spec.js contains page tests.
Acceptance criteria:
GET /api/reportsreturns HTTP 200, contains 20 records, each record contains the four fields, andrevenueis numeric.- After the page loads, it displays a data table with 20 rows.
- After a quarter is selected in the dropdown, the table shows only records for that quarter, and the row count matches the sample.
- Rows with negative
yoyuse red text color, and the corresponding CSS class name isnegative.
Test commands: pytest -q backend/tests and npx playwright test.
Failure criteria: if any of the above conditions is not met, the task is judged as failed. The judgment is performed by test scripts and does not rely on manually inspecting the page.
3. Quota Metrics: From Usage to Cost
Statements such as “healthy quota consumption ratio” cannot be compared. This article instead uses the following metrics, which must be recorded for every run.
Total tokens per task (T): the input plus output tokens for one complete run, multiplied by the retry factor. The formula is T = T0 × (1 + R), where T0 is the number of tokens for one complete run and R is the average number of retries.
Weekly completable tasks (N): N = Q ÷ T, where Q is the weekly quota.
Cost per task (C): C = T × unit price, where the unit price is converted according to the vendor’s token billing standard.
Cost per successful task: C ÷ success rate. If Model A has a higher success rate than Model B but a larger T, compare this metric rather than only comparing single-run cost.
Example: assume one complete run consumes 600,000 tokens and the average retry count is 0.5. Then T = 600,000 × 1.5 = 900,000. If the weekly quota Q is 10,000,000 tokens, N = 10,000,000 ÷ 900,000 ≈ 11.1, meaning about 11 tasks can be completed per week.
The budget threshold is set by the team according to its own budget; this article does not provide a fixed value.
4. Multi-Agent Cluster: Protocol, Degradation, and Environment Awareness
Multi-agent scenarios commonly involve three roles: intelligence collection, data analysis, and report generation. The roles communicate through structured messages.
Message example:
{“schema_version”: “1.0”, “task_id”: “fin-001”, “sender”: “collector”, “receiver”: “analyst”, “payload”: {“company”: “Example Corp”, “quarter”: “2026Q3”, “revenue”: 1200.5}}
Protocol rules:
- Validate every message with JSON Schema. If validation fails, ask the sender to resend once. If it still fails, record the error and skip that message.
- The receiver timeout is 60 seconds. After a timeout, retry at most 2 times, with intervals of 2 seconds and 4 seconds.
- Degradation strategy: when the collector agent is unavailable, the analyst agent uses the last cached data and marks the output with
stale: true.
Environment-awareness testing has two items:
Missing dependency: remove one dependency from requirements.txt and observe whether the model can run pip install within the allowed scope and rerun the tests. The pass criterion is that the tests eventually pass and the model reports the names of the installed packages.
Port occupation: first start a placeholder process on port 8000 and observe whether the model can switch to another port, or explicitly report the conflict and stop. If the model ignores the conflict and claims that startup succeeded, it is recorded as a failure.
5. Selection Checklist
Before deciding to adopt a model, check each item:
- Long tasks: run the case in Section 2 five consecutive times and check whether the success rate reaches the threshold set by the team.
- Instruction following: the validation failure rate of JSON messages. Teams can use 5% as a starting point.
- Environment awareness: whether both the missing-dependency and port-occupation tests pass.
- Exception recovery: when failures continue after more than three rounds, whether the model proactively interrupts and outputs a handoff state summary.
- Quota: whether the cost per successful task calculated by the formula in Section 3 is within budget.
- Stability: after multiple turns of dialogue, for example when the context exceeds 100,000 tokens, whether the model still follows the initial JSON format and acceptance criteria.
6. Stress-Test Procedure and Failure Rollback
Stress-test steps:
- Fix the sample, prompt, and sandbox configuration, and do not modify them during testing.
- Run each model on each task at least 5 times, recording success or failure, T, retry count, duration, and failure type.
- Summarize the success rate and the median and maximum values of T. If the medians are close but the maximum values differ greatly, stability is insufficient.
- Classify failures by type: context loss, over-limit, test failure, environment error, and format error.
Rollback and human takeover: after each round of modifications, run a git commit in the project as a checkpoint. When context loss or over-limit occurs, use git log to find the most recent commit that passed the tests, then use git revert to undo subsequent changes. Do not use git reset --hard, so that history is preserved. A human then exports a task summary, including completed items, failing test cases, and original errors, and continues in a new session.
