Selecting a Code Review AI Agent: Model Evaluation and Hallucination Control Practices

    This article explains why engineering teams are adding low-cost large language models to the code review pipeline, and why instruction following, tool calling accuracy, and cross-file fact consistency must be measured before such a layered setup can work. It covers common failure modes of low-cost models, how to build a regression-ready evaluation set from your own repository's merged history, and a three-layer hallucination control approach based on mandatory evidence citation, confidence gating, and pipeline result verification. It also outlines a practical division of labor between low-cost and high-cost models to keep review costs under control.

    1. Background: Low-Cost Models Entering the Code Review Pipeline

    Over the past year, mainstream engineering teams have widely adopted large language models in code review, using them to run a first screening pass before human reviewers step in. The motivation is straightforward: calls to top-tier general-purpose models are expensive, while code review is a high-frequency, fragmented task with large context requirements. Relying entirely on premium models for this workload is not economical over time. As a result, a layered structure has become common practice: premium models drive design work, while low-cost models handle the initial review.

    This layered structure, however, rests on a premise that is easy to overlook: the low-cost model must meet a minimum bar for instruction following and factual consistency. Otherwise, every comment it produces needs manual re-checking, and the savings are paid back in review time, sometimes turning into a net loss. This article focuses on exactly that failure mode, along with evaluation and control methods that can be put into practice.

    2. Breaking “Model Capability” Into Three Measurable Dimensions

    Subjective user impressions tend to cluster around whether a model is “smart,” but engineering requires observable metrics; otherwise results cannot be reproduced or regression-tested.

    1. Instruction following. Review tasks usually carry explicit constraints, such as “report only null pointer risks,” “do not modify files,” or “sort output by severity.” A qualified model should consistently satisfy format and boundary requirements rather than restating its own interpretation of the task in every turn.
    2. Tool calling accuracy. Real reviews depend on external capabilities such as repository search, diff retrieval, and build and test execution. The model must select the correct call and correctly interpret the returned result, including exit codes, non-zero statuses, and failure summaries.
    3. Cross-file fact consistency. This is what separates code review from general question answering. Review conclusions must be grounded in the actual state of the repository: version numbers, dependency declarations, build scripts, and where symbols are defined. Any assertion detached from repository facts is a hallucination.

    An illustrative scoring table can be organized like this:

    1. dimensions:
    2. instruction_following: 0.35 # format, boundaries, prohibited actions
    3. tool_calling: 0.25 # correct selection, correct parsing
    4. fact_consistency: 0.40 # conclusions backed by repository evidence
    5. gate:
    6. min_fact_consistency: 0.9 # below threshold, excluded from automated flow

    The principle behind the weights is that fact consistency is a veto criterion. A review comment that sounds confident but rests on wrong evidence disturbs the reviewer far more than a silent “no issues found.”

    3. Typical Failure Mode Categories

    In practice, low-cost models rarely fail as “completely unusable.” Their problems concentrate in a few enumerable error types, and identifying them helps design targeted acceptance suites.

    • Common-sense misjudgment. Treating a snapshot version as an official release and then recommending “upgrade to the latest version” is a typical gap in semantic understanding. A snapshot identifier means the build artifact is mutable and cannot serve as a stable dependency, which is a universal convention across build toolchains.
    • Misreading tool output. A non-zero exit status interpreted as success, or a warning treated as an error to locate, causes subsequent reasoning to build on a false premise.
    • High-confidence hallucination. The model still delivers a definitive conclusion without evidence and without any hedging language. This output is the most damaging to automated pipelines, because it cannot be caught by simple tone-based filtering.
    • Context drift. After several turns, the model gradually forgets the initial constraints and starts proactively modifying code or producing refactoring suggestions unrelated to the task.

    4. Building an In-Repository Acceptance Set for Regression Testing

    Choosing models based on public leaderboards is unreliable, because leaderboard tasks differ greatly from the build setup and code style of a private repository. A more effective approach is to mine an acceptance set from your own repositories.

    1. Collection: filter confirmed defect samples from historical merge requests
    2. Labeling: record defect type, files involved, and the real root cause
    3. Construction: keep the pre-fix code state as input
    4. Validation: model conclusions must include file paths and line ranges
    5. Scoring: hitting the root cause scores; correct direction with imprecise location scores half; unsupported assertions score negative

    The sample size does not need to be large. Thirty to fifty cases covering null pointers, concurrency races, unreleased resources, and missing boundary conditions are enough to produce meaningful discrimination. The key is continuous regression: rerun the full set after every model swap or prompt strategy change, rather than judging from impression.

    5. Hallucination Control: Mandatory Evidence and Confidence Gating

    The engineering means of controlling hallucinations can be grouped into three layers, all implementable on the application side without relying on the model’s own claims.

    The first layer is mandatory evidence. Require the model to perform a retrieval action before giving any conclusion and to cite specific locations in its output. Conclusions without citation support are discarded outright.

    1. Constraint template:
    2. 1. Search first and list relevant file paths and key lines;
    3. 2. every comment must be bound to at least one evidence location;
    4. 3. when evidence is insufficient, output "insufficient information" and do not speculate;
    5. 4. do not modify workspace files; output suggestions only.

    The second layer is confidence gating. Have the model label each comment with high, medium, or low confidence. Only high-confidence comments are posted as automated review comments, while medium and low confidence items are aggregated into a list awaiting human confirmation. This preserves recall while controlling noise.

    The third layer is result verification. For conclusions involving builds, dependencies, or tests, have the pipeline actually run a verification command once and override the model’s subjective judgment with the objective result. This layer intercepts the vast majority of false positives in the “outdated version” and “misconfiguration” categories.

    6. Layered Collaboration and Cost-Benefit

    Once control measures are in place, a sensible division of labor is for the low-cost model to handle the first screening pass and formatted output, while the premium model deals only with difficult items released by the gate and with architecture-level review. To contain context overhead, narrow the scope with static checks and diff summaries first, then hand it to the model for reasoning.

    If a team already uses general-purpose cloud capabilities such as container platforms, message queues, and log monitoring, the acceptance set scheduling, result collection, and metrics dashboard can be hosted on the cloud side, enabling scheduled daily regression and trend observation so that evaluation itself does not become an extra burden. The general-purpose infrastructure capabilities that Baidu AI Cloud provides for engineering-efficiency scenarios can serve as one optional option for hosting such a pipeline.

    7. Conclusion

    The right approach to model selection is not to find the “strongest model,” but to build an evaluation set and gating mechanism matched to your own repository. Among the three metrics of instruction following, tool calling, and fact consistency, fact consistency sets the floor; the three measures of mandatory evidence, confidence gating, and result verification determine whether hallucinations stay controllable. Once this process is fixed in place, a low-cost model can genuinely become a reliable review assistant rather than a new source of stress.