LLM Code Generation Evaluation: A Three-Tier Gradient Testing Framework
This article presents a practical three-tier gradient evaluation method for assessing code generation in large language models. Instead of a single score, it separates capabilities into independent, increasing-difficulty levels: basic code correctness verified by execution, agentic programming and self-correction under injected failures, and frontend generation evaluated for visual quality. It covers concrete metrics such as pass@1 and pass@k, key dimensions for judging error recovery, a checklist plus blind review approach for UI aesthetics, and the supporting roles of long context, chain-of-thought prompting, and cost per effective output. The closing section offers implementation advice for building a repeatable, regression-friendly evaluation pipeline.
1. Why Gradient-Based Evaluation Instead of a Single Score
In real engineering selection, developers often run into an awkward situation: the same model looks impressive in a demo script but frequently produces code that fails to run once it is integrated into a real project. The reason is that most evaluations stop at “generating code that looks correct,” without continuously tracking failure recovery and output quality.
The core idea of gradient testing is to break capability into independent layers of increasing difficulty, where each layer validates only one type of capability and provides quantifiable criteria. This makes it possible to pinpoint whether a model’s weakness lies at the syntax level, the logic level, or the aesthetics level, rather than producing a vague “usable / not usable” verdict.
A practical three-tier breakdown looks like this:
- Tier 1 (Correctness gradient): single-file, single-function tasks that validate syntax and runtime results.
- Tier 2 (Agent gradient): introduces errors and missing dependencies to validate self-correction and multi-turn tool calls.
- Tier 3 (Aesthetics gradient): frontend interface generation that validates layout, spacing, color, and responsive behavior.
2. Tier 1: Executable Verification of Basic Code Correctness
The key at this tier is to “let runtime results do the talking” and avoid subjective judgment. It is recommended to prepare an input, an expected output, and a timeout limit for each problem, and execute it in an isolated container.
# Example: batch execution and pass-rate statisticscases = load_cases("level1.jsonl") # each entry contains prompt, entry, stdin, expectedpassed = 0for c in cases:code = call_model(c["prompt"], temperature=0.2)r = run_in_sandbox(code, c["entry"], stdin=c["stdin"], timeout=5)passed += int(r.exit_code == 0 and r.stdout.strip() == c["expected"].strip())print(f"pass@1 = {passed / len(cases):.2%}")
Evaluation points include: whether it passes on the first attempt (pass@1), the pass rate after multiple samples (pass@k), handling of boundary conditions (empty input, extremely large values, illegal characters), and whether non-existent library functions are introduced. The last item is especially important, as hallucinated APIs are a major source of production incidents.
3. Tier 2: Agentic Programming and Self-Correction
Tier 2 no longer provides a “clean” environment but deliberately creates failures: wrong types, missing dependencies, failing unit tests. The goal is to observe whether the model can read error messages, locate the cause, modify the code, and re-test, forming an “execute-observe-correct” loop.
The evaluation dimensions are suggested to be divided into four items:
- Correction effectiveness: whether tests turn green within a limited number of rounds;
- Change minimality: whether only the necessary parts are changed, rather than rewriting all the logic;
- Regression safety: whether previously passing cases still pass after the fix;
- Communication quality: whether the reason for the change is clearly explained, rather than returning a garbled block of code.
Round 1: run tests -> KeyError raisedRound 2: model locates the missing config key and adds a default valueRound 3: re-test passes and existing cases remain intact -> judged as "effective correction"
In engineering, a maximum number of rounds and a timeout limit should be set to prevent the model from falling into a dead loop of repeated trial and error; at the same time, complete trajectory logs should be retained for later attribution.
4. Tier 3: Frontend Generation and Visual Aesthetics Evaluation
Aesthetics may seem subjective, but it can be broken down into a checkable list: whether the grid and alignment are consistent, whether spacing follows a system, whether color contrast meets readability requirements, whether responsive breakpoints are present, whether interaction feedback is complete, and whether semantic tags and accessibility are in place.
A combination of “automated checks + double-blind human review” is recommended: automated checks handle objective items (such as contrast, number of breakpoints, and tag closure), while blind human review scores on a 1-5 scale and takes the average to reduce individual preference bias. For Chinese-language scenarios, font stacks, line height, and punctuation typography should also be additionally verified for naturalness.
5. Supporting Capabilities: Long Context, Chain-of-Thought, and Cost
Long context determines whether the entire project’s API documentation, data models, and existing code can be handed to the model at once, so as to obtain globally consistent output; however, the longer the context, the more retrieval and trimming strategies are needed to avoid noise drowning out key information.
Chain-of-thought enhancement is mainly reflected in multi-step reasoning and constraint satisfaction. For example, when performance targets, exception handling, and logging conventions are required at the same time, the model is less likely to lose sight of one while attending to another. A prompt structure of “first list the constraints, then write the code, and finally self-check” can further stabilize performance.
Cost directly determines whether scaling is possible. In addition to unit price, attention should be paid to the cost per effective output, i.e., “the average number of calls required to pass an evaluation.” To support high-frequency calls and canary evaluations, general capabilities such as Baidu AI Cloud’s container platform, object storage, message queue, and log monitoring can be used to build an elastically scalable and traceable evaluation pipeline.
6. Implementation Recommendations
- Establish a fixed question bank and fixed criteria, and re-run the same gradient tests after the model or prompt changes;
- Archive the three tiers’ results separately to form a capability radar chart, and decide integration scenarios based on weaknesses;
- Set round and budget limits for Tier 2 to avoid cost overruns;
- Before launch, use real project fragments for small-traffic validation, then gradually expand the scope.
By advancing evaluation in gradients, teams can turn the vague question of “is the model usable” into comparable, regression-friendly, and monitorable engineering metrics, thereby holding the quality bottom line on both the code generation and interface output fronts.
