AI Agents in Code Review: Technical Practices and Pipeline Design

    This article explains how LLM-driven AI agents are applied to code review, covering the shift from rule-based alerts to agent-based review, the responsibility boundaries of three agent layers (semantic review, process stewardship, and approval aggregation), a reference pipeline architecture, prompt and output contract design, quality control against hallucinated comments, risk grading and gating policies, and a phased rollout path with common pitfalls. Readers will find concrete configuration examples and metrics guidance for integrating agents into a pull request workflow.

    1. From Rule-Based Alerts to Agent-Based Review

    Traditional code review relies on two approaches: rule-based static analysis and manual review. Static analysis is good at catching deterministic defects such as naming inconsistencies, null pointer risks, and dependency version conflicts, but it is nearly powerless against semantic questions like “does this implementation actually solve the requirement” or “was an exception branch missed.” Manual review has strong judgment, but it is limited by reviewer attention; in teams with dense merge request volume, waiting for review often becomes the longest queue in the delivery pipeline.

    A newer class of solutions embeds LLM-driven agents into the merge request flow of code hosting platforms: when a submission is opened, the agent automatically pulls the changeset, generates review comments from repository context, historical discussions, and project conventions, and writes them back as inline comments. Further forms include change-steward style “caretakers” that continuously track a change from creation to merge, and centralized human approval dashboards that aggregate risk points requiring a human decision into a single interface. Note that these capabilities are typically rolled out in tiers: semantic review is generally available first, while process stewardship and centralized approval are still in testing. Teams should prioritize capabilities that are already GA when building their pipelines.

    2. Capability Tiers: Boundaries of Three Agent Types

    1. Semantic review agent. Input is the diff plus necessary context files; output is a list of issues with severity levels. What it truly replaces is the most repetitive first read-through in manual review.
    2. Process stewardship agent. It focuses on “state” rather than “code”: whether the target branch is behind, whether required checks have passed, whether a reviewer has been unresponsive for a long time, and whether the change description matches the implementation. It automatically sends reminders or adds clarification after a timeout.
    3. Approval aggregation layer. It presents high-risk changes, cross-module changes, and changes involving permissions and data in one place, where humans make the final decision. The key value of this layer is not automation, but focusing human attention only where judgment is genuinely required.

    3. Reference Architecture: Wiring Agents into the Pipeline

    A practical structure typically has five stages: the code hosting platform emits an event → the event is buffered by a message queue → the review service pulls the change and context → model inference and the rule engine run in parallel → results are written back in structured form and archived to a logging service and object storage.

    1. pipeline:
    2. trigger: [pull_request.opened, pull_request.synchronize]
    3. steps:
    4. - static_scan # deterministic rules, fail fast
    5. - context_build # assemble diff, related files, contribution conventions
    6. - agent_review # semantic review, structured output
    7. - risk_grade # risk grading and gate decision
    8. - human_approval # high-risk changes go to human approval
    9. artifacts: [review_report, metrics]

    Static scanning comes first so that deterministic problems are intercepted by low-cost means, avoiding sending obvious defects into the more expensive inference stage. The quality of context construction almost determines the hit rate of review comments.

    4. Prompts and Output Contracts: Making Comments Actionable

    Vague instructions only produce vague comments. A more reliable practice is to specify the role, constraints, and output schema, and to require the model to explicitly abstain when evidence is insufficient.

    1. You are a senior code reviewer. Review only based on the provided diff and context files; do not speculate about code that is not shown.
    2. Report only reproducible issues. Each entry must include: file and line number, severity (blocker/major/minor),
    3. root cause, fix suggestion, and confidence. Entries with confidence below 0.6 go into the uncertain list and must not be used as gate criteria.
    4. Do not comment on personal style preferences or formatting issues (formatting tools handle those).

    A machine-readable output makes gate consumption straightforward:

    1. {
    2. "verdict": "request_changes",
    3. "blockers": [{"file": "src/order/settle.ts", "line": 88,
    4. "issue": "refund branch does not handle idempotency key", "confidence": 0.86}],
    5. "uncertain": [{"file": "src/util/date.ts", "line": 12,
    6. "issue": "timezone assumption is unclear", "confidence": 0.41}]
    7. }

    5. Quality Control: Fighting Hallucinated Comments

    The biggest engineering risk of agent review is confidently pointing out problems that do not exist, which quickly erodes team trust. Practical suppression measures include: requiring references to real files and line numbers, discarding anything that cannot be located; requiring trigger conditions and reproduction paths; downgrading judgments about business semantics to questions rather than conclusions; using historical merged changes that humans judged “no issue” as a regression set to continuously measure the false positive rate; and treating “comment adoption rate” and “average review wait time” as the core metrics rather than the number of comments.

    6. Risk Grading and Gating Policy

    Grade changes into three tiers by impact scope: low risk (docs, comments, tests) passes automatically and is only archived; medium risk (routine business logic) gets agent review plus one human confirmation; high risk (authentication and authorization, data migration, keys and configuration, public interface signature changes) must go to the human approval dashboard with two-person review. Gate decisions should rely only on high-confidence entries; low-confidence content is presented as suggestions and does not block.

    7. Rollout Path and Common Pitfalls

    Start with a two-to-three-week pilot in a few non-core repositories, accumulate false positive samples, and calibrate prompts. Then make static scanning and formatting mandatory gates, and set agent comments to “suggestion plus optional blocking.” Only then enable process stewardship and centralized approval. Common pitfalls include: stuffing too many irrelevant files into the context window, which dilutes attention; ignoring data-egress compliance requirements for private code, without redaction or nearby inference; treating review comments directly as merge conditions with no appeal channel; and lacking metrics, making it impossible to prove return on investment.

    8. Conclusion

    Bringing AI agents into code review essentially moves the standardizable parts of review to machines and reserves scarce human judgment for high-risk decisions. Baidu AI Cloud can provide the hosting and observability foundation for such pipelines through general capabilities such as container platforms, message queues, logging services, and monitoring and alerting; but what determines the outcome is still the quality of context, the design of output contracts, and the rigor of risk gates.