LLM Capability Evaluation: A Methodology for Hallucination, Code Generation, and Character Handling
As new LLM releases ship faster than ever, choosing a model on benchmark impressions is no longer enough. This article lays out a reproducible methodology for evaluating LLM capabilities across three dimensions—context hallucination, code generation, and character-level handling—along with concrete metric definitions and verification approaches. It shows how to score hallucination tasks programmatically with strict field-level checks, how to separate one-pass coding ability from multi-round stability using sandboxed execution, and why reasoning and non-reasoning modes must be measured separately. It also covers the engineering constraints that turn a capable model into a stable service (concurrency limits, output length and cost, reliable retries, and observability), how to feed online bad cases back into the test set, and five common pitfalls to avoid.
1. Start With the Problem: Why Capability Gains Must Rest on Measurable Evaluation
Over the past year, LLM releases have iterated at a noticeably faster pace—it has become the norm for the same vendor to ship multiple new versions within six months. For developers, this creates a practical problem: model selection cannot rely on the impression that something “feels stronger on the leaderboard.” It has to rest on evaluation sets aligned with real business tasks.
A reusable evaluation plan should answer four questions at once:
- Task fit: On the real business distribution, what is the success rate?
- Stability: If the same input is sampled repeatedly, how bad is the worst case?
- Cost and latency: Are the average output length, token consumption, and P95 latency acceptable?
- Failure modes: Where do errors concentrate, and can engineering measures catch them?
The three dimensions below each come with metric definitions and verification methods.
2. Dimension 1: Context Hallucination and Multi-Source Extraction
2.1 Breaking “hallucination” into measurable sub-problems
“Hallucination control” is a vague term. When evaluating, you should split it into at least three task types:
- Multi-source extraction: aggregate fields across paragraphs from long material, where any missing or mismatched field counts as a failure;
- Long-text summarization: measure coverage and faithfulness—does the output introduce statements not present in the material?
- Multi-constraint judgment: given rules and material, produce a single conclusion (for example, locating one specific record in a list according to a rule).
The key insight is this: problems where each single step is easy but the number of intermediate steps is very large are the real test of hallucination. Even if each step has a high accuracy rate, chaining them together makes the overall accuracy decay quickly. Such problems are well suited to strict all-or-nothing judgment rather than partial credit per field.
2.2 Defining the metrics
For each question, record two values:
- 1-Pass rate: the proportion of strict matches against the gold answer under a single sample;
- Worst case: the poorest result across multiple samples (for example, 3–5 runs).
Reporting only the mean hides the long tail. What actually affects the user experience is “will this call suddenly go wrong,” so the mean, 1-Pass, and worst-case value must be presented side by side.
2.3 Validate structurally, don’t read with human eyes
Scoring for hallucination tasks should be programmatic. Have the model output JSON, then run field-level validation in code:
import jsondef strict_check(pred: str, gold: dict) -> bool:"""Strict check: the set of fields and their values must match exactly; no fuzzy matching."""try:obj = json.loads(pred)except json.JSONDecodeError:return Falseif set(obj.keys()) != set(gold.keys()):return Falsereturn all(obj[k] == gold[k] for k in gold)
The accompanying prompt constraint matters too: require “answer only from the given material, and output null when uncertain.” This separates fabrication-type hallucination from refusal-type conservatism—the former is more dangerous, while the latter can be gracefully degraded.
3. Dimension 2: Coding Fundamentals: One-Pass Ability vs. Multi-Round Stability
3.1 Two different questions
The most commonly misread part of coding evaluation is this: “Can it get it right” and “Is it close to right every time” are two different things.
- One-pass ability: after a single prompt, can the generated code compile and pass the test cases directly?
- Multi-round stability: across repeated samples, does it at least produce a version that compiles and passes basic cases?
One model may have a mediocre 1-Pass rate but stay consistently close to correct across multiple samples (needing only small fixes); another may score high occasionally but go completely off track the rest of the time. For a “generate, then manually fine-tune” workflow, the former is friendlier; for a fully automated pipeline, both demand a higher floor.
3.2 Designing categorized questions
| Question type | Focus | Judgment |
|---|---|---|
| Business code (interfaces/templates) | Structural completeness, reasonable dependencies | Compile + unit tests |
| Complex algorithms | Boundary conditions, complexity constraints, correctness | Hidden cases + performance cases |
| Refactor / fix | Understanding existing code and making minimal changes | All original tests pass + new cases |
| Coding without reasoning mode | The capability floor in low-latency scenarios | Run the same set separately |
On the engineering side, always execute generated code inside an isolated container, limiting CPU, memory, execution time, and network access, so the evaluation environment can’t be damaged by untrusted code.
3.3 Evaluation executor skeleton
import concurrent.futures as cfdef run_case(case, model, mode="reasoning", passes=3):results = []for _ in range(passes):try:out = call_model( # parameters follow the platform's documentationmodel=model,messages=build_prompt(case),reasoning=mode == "reasoning",)results.append(sandbox_judge(out, case)) # returns (compiled?, cases passed?)except Exception:results.append((False, False)) # failures must be counted, never silently droppedreturn {"id": case.id,"pass1": results[0][1],"min_pass": min(r[1] for r in results),"pass_rate": sum(r[1] for r in results) / len(results),}def evaluate(cases, model, workers=4):with cf.ThreadPoolExecutor(max_workers=workers) as pool:return list(pool.map(lambda c: run_case(c, model), cases))
Don’t blindly crank up workers: most hosted model services cap per-account concurrency, and issuing beyond that limit triggers throttling and pollutes the latency metrics.
4. Dimension 3: Character-Level Handling: Small Tasks Expose Detail Robustness
Character counting, locate-and-replace, delimiter splitting, and case or full-width/half-width normalization may look trivial, but they are a mirror for detail robustness. The typical failure is not that the model “can’t do it”—it’s that a slight hallucination causes it to misread a character, confuse similar-looking glyphs, or overlook a formatting convention.
There are two improvement strategies:
- Reshape the task: have the model output executable processing logic (such as a script) rather than the result directly, then run it in a sandbox and compare against the expected output. This makes errors locatable and reproducible.
- Quantify the metrics: use exact match rate as the primary metric and edit distance (character-level) as an auxiliary metric, to observe the difference between “slightly wrong” and “completely wrong.”
def edit_distance(a: str, b: str) -> int:prev = list(range(len(b) + 1))for i, ca in enumerate(a, 1):cur = [i]for j, cb in enumerate(b, 1):cur.append(min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + (ca != cb)))prev = curreturn prev[-1]
A common pattern in practice is this: simple questions score steadily at full marks, while complex questions show a wide gap between upper and lower bounds. This gap is often not a capability deficiency but a single misread character in a long chain getting amplified step by step—so during evaluation, track separately at which step the error occurs.
5. Reasoning and Non-Reasoning Modes Belong in Separate Tables
The two call modes of the same model differ completely in capability, latency, and token-consumption structure; mixing them in one table is meaningless.
- Reasoning mode: generates intermediate thinking before giving a conclusion; clearly beneficial for complex logic and long chains; the cost is high first-token latency and large token consumption.
- Non-reasoning mode: low latency and better suited to high-concurrency online scenarios; its performance on complex tasks must be measured separately.
A recommended report structure: use question type × mode as a two-dimensional grid, recording success rate, 1-Pass, average/max tokens, and P50/P95 latency. From this you can derive a routing strategy: a front-end classifier (or a small model) judges task complexity, sending simple requests to non-reasoning mode and complex requests to reasoning mode, balancing cost and effectiveness.
6. From “Works” to “Stable Service”: Four Engineering Constraints
Meeting the capability bar is only the first step. In real integration, the following constraints often affect the experience more than the model score itself.
- Concurrency and quotas: some platforms cap per-account concurrency for their newest models, or only make them available in higher-tier plans or subscriptions. The client should implement token-bucket rate limiting and queuing, and keep a backup model for degradation.
- Output length and cost: newer models produce longer outputs on average, which directly raises token consumption and the probability of timeouts. Countermeasures: explicitly cap the maximum output length, require “conclusion first, then reasoning,” reuse cacheable results, and batch offline tasks.
- Reliable call wrapping: combine timeout with exponential backoff plus jitter for retries, along with idempotency keys and a circuit breaker, so throttling doesn’t get amplified into an avalanche.
import random, timedef call_with_backoff(fn, max_retry=3, base=0.5):for attempt in range(max_retry + 1):try:return fn()except Exception:if attempt == max_retry:raisetime.sleep(base * (2 ** attempt) * (0.5 + random.random()))
- Observability: keep redacted logs of calls and track success rate, latency percentiles, token usage, and the distribution of failure reasons; wire in a logging service and monitoring alerts, and set threshold alerts on error rate and P95 latency. If you use model services from platforms such as Baidu AI Cloud, you can build the same collection-and-alerting loop on top of their monitoring and logging capabilities.
7. Continuous Evaluation: Turning Online Bad Cases Back into Test Sets
Evaluation is not a one-off activity. The recommended loop:
- Label online failure samples by error type (hallucination / compile failure / test-case failure / format mismatch);
- Periodically flow them back into the offline evaluation set, deduplicate, and expand the question bank;
- Every time you switch model versions or adjust prompts, run a full regression and compare against the historical baseline;
- Roll out a new version first to a small traffic slice, and go full-scale only if the metrics don’t degrade.
8. Five Common Pitfalls
- Sample size too small: conclusions drawn from a dozen questions have very low confidence—aim for at least 30–50 questions per category.
- Looking only at the mean: ignoring the worst case leaves long-tail risk unassessed.
- Prompt overfitting: few-shot examples leak the answers, and the evaluation set becomes disconnected from the online distribution.
- Testing only reasoning mode: neglecting how the default mode performs in real high-concurrency scenarios.
- Substituting a general leaderboard for the business set: a model that is strong in general ability won’t necessarily win on vertical tasks.
9. Conclusion
The latest generation of models shows observable progress in hallucination control, one-pass coding ability, and character-level handling. But the value of evaluation isn’t to prove who is stronger—it’s to quantify “on what task, in what mode, and at what cost something is reliably good enough.” By turning evaluation into a reproducible pipeline—unified questions, programmatic judgment, mode separation, and recording of latency and cost with continuous regression—you can maintain engineering certainty even amid rapid model iteration.
