How To · DomainIntermediate

How to Build a Code Review AI Agent

Build an AI code review agent for LLM pull request review: diff context, repo RAG, severity, CI comments, and evals scored against senior-reviewer judgments.

Git forge APIDiff packerRepo RAGCI reporterReview evals15 min · 6 steps · Updated 2026-08-25
The short answer

To build a code review AI agent, pack each pull request with the diff, neighboring definitions, and repo RAG, run a model that emits severity-ranked findings, post them as CI comments, and evaluate the agent against senior-reviewer judgments — never against whether it sounded confident.

What you’ll build

  • A PR agent that packs the diff, related symbols, and repo conventions — not only the patch hunk
  • Comments posted in CI with severity, file, line, and a suggested patch when confident
  • A suppress path for generated files, vendored code, and lockfiles
  • Evals that compare agent findings to senior-reviewer labels, including false-positive rate
  • A trace per PR: prompt, retrieved files, comments, and human reactions

Before you start

  • 01Bot access to the git forge (GitHub, GitLab, or Azure DevOps) with permission to comment, not to merge
  • 02A clone path that can pack the diff plus neighboring files without uploading the whole monorepo to a vendor trainer
  • 03A small corpus of past PRs with senior-reviewer comments, labeled by severity
  • 04A severity taxonomy the team already uses (blocker, major, nit) so the agent does not invent one
  • 05Agreement that the agent never presses merge

Key takeaways

  • 01

    An AI code review agent is only as good as the context pack: diff plus symbols plus the conventions file, not the hunk alone.

  • 02

    Severity and a comment cap are product features; unranked nits get the bot banned.

  • 03

    Repo RAG exists to fetch the invariant, the auth helper, and the last similar migration — not to dump the monorepo into the prompt.

  • 04

    The agent comments; humans or deterministic CI merge.

  • 05

    Evals that match senior reviewers on blockers and punish false positives are the shipping gate.

01

How to build a coding agent that reviews pull requests

A code review agent is not a chat window on the repo. It is a batch job that fires on each pull request, reads a packed context, and writes structured comments. Authors tolerate it when it catches blockers they would have shipped and stays quiet otherwise. They mute it when it bikesheds naming.

Write the contract first. Which rules are in scope? Who may see the diff (often the same as CI secrets policy)? Does the agent suggest patches or only flag? Is it allowed to request changes, or only to comment? If that document does not exist, you will tune prompts against Slack complaints.

02

Diff context is more than the patch

Send the full file for small edits and a window around each hunk for large ones, plus one hop of symbols. A changed helper needs its callers. A changed SQL string needs the wrapper that interpolates it. Language servers or ctags in CI can provide those hops without a 20-minute clone of the world.

Drop generated bundles, lockfiles, vendored third_party, and binary diffs. They waste tokens and produce false secrets findings. Respect .gitattributes and CODEOWNERS: an agent that comments on a generated protobuf file has already lost the room.

  • Always: patch, file path, language, PR description, linked issue
  • Usually: full file if under a size cap, plus one-hop symbols
  • Retrieved: CONTRIBUTING, ADRs, auth helpers, similar past findings
  • Never: the entire monorepo, secrets from CI, unrelated apps in the same git repo
03

Repo RAG without drowning the prompt

Repo RAG is targeted retrieval: conventions, architecture notes, the canonical authz helper, a similar migration. Index those, not every test fixture. Query with the changed paths and a short summary of the PR. Five to ten passages is plenty. If the model still misses a local invariant, your packer missed a neighbor, not a wiki page.

Permission still applies. A public fork PR must not retrieve internal ADRs. Run retrieval as the bot identity that the forge already uses for CI, and keep customer-private repos off any vendor trainer. Traces stay in your cloud.

04

Severity, CI comments, and the merge button

Each finding needs a rule id and a severity the team already uses. Blockers fail the check. Majors comment. Nits are off by default or behind a label. Cap the review at a small number of comments, highest severity first. Duplicate the same note on three hunks and you have a spam bug.

Post through the forge review API so threads update on push. Do not @-spam authors. Never grant the bot merge or admin. Deterministic tests, SAST, and CODEOWNERS remain the merge gates. The LLM pull request review is an extra reviewer with a measured false-positive rate.

05

Evals versus senior review

Label a set of historical PRs: blockers the senior reviewer caught, nits they ignored, and comments they made that were later reverted. Score the agent on blocker recall, extra comments per PR, and whether a suggested patch applies. A model that finds 90% of blockers and files 40 nits is worse than one that finds 70% and files two.

Re-run on every prompt, model, and packer change. Sample live PRs weekly: did humans react with thumbs-up, resolve, or 'not useful'? Feed the misses back as eval cases. This is how the agent tracks the team's taste instead of a generic 'be a senior engineer' prompt.

06

Implementation

ReinforcedX implements the packer, repo RAG, CI reporter, and the senior-review eval set in your forge and your cloud. Model-agnostic. You own the prompts, traces, labeled PRs, and runbooks. No token markup. The bot does not get merge rights.

Four weeks: contract and labeled set, packer plus comments in a shadow repo, shadow-mode on real PRs with comments hidden from authors, then handover. Thirty days on-call is for the first monorepo path exception. 98.7% QA pass rate is our delivery bar on the agreed eval set, not a claim that the agent replaces your staff engineer.

Step-by-step build

  1. 1

    Define the review contract

    List the defect classes you want (authz, injections, data loss, secrets, breaking APIs), the severity labels, and a max comment count per PR.

  2. 2

    Pack the diff with neighbors

    From the forge webhook, collect the patch, the full files, and one-hop definitions (callers, callees, types) so the model can see usage, not only the edit.

  3. 3

    Add repo RAG for conventions and similar code

    Index CONTRIBUTING, ADRs, auth helpers, and prior review comments; retrieve a handful of passages keyed by the languages and paths in the PR.

  4. 4

    Run a structured reviewer

    Force JSON findings with file, line, severity, rule id, rationale, and an optional patch; drop anything that cannot point at a line in the diff.

  5. 5

    Post through CI, not as a noisy human

    Use the checks or review API, group by severity, skip generated paths, and update the same review on new pushes instead of stacking duplicates.

  6. 6

    Evaluate against senior review

    On a labeled historical set, score recall on blockers, false-positive rate on nits, and time-to-first-comment; gate releases on those numbers.

Common pitfalls

The mistakes that show up in real deployments — each one costs a week if you learn it the hard way.

Reviewing the hunk in isolation

Most real defects are missing callers, broken invariants, or a style the repo already forbids two directories over. If you only send the patch, you built a linter with extra latency.

Commenting on every nit

Twenty 'consider renaming' notes train authors to mute the bot. Cap comments, rank by severity, and stay silent on nits unless the team asked for style.

Auto-merge on a green agent

An LLM pull request review is an advisor. Merge stays with humans or with deterministic gates (tests, CODEOWNERS). The agent does not own the button.

No allowlist of rules

A free-form 'find bugs' prompt hallucinates style wars. Encode the rules you care about: authz, injections, resource leaks, migrations, secrets — then let the model hunt those.

Evaluating on synthetic bugs only

Planted TODOs are easy. Grade against historical PRs where a senior reviewer left a blocker, and count the nits the agent filed that humans resolved as 'won't fix.'

Frequently asked questions

What is an AI code review agent?

A service that reads a pull request, packs the diff with neighboring code and repo conventions, and posts severity-ranked comments in CI. It is an advisor. It is not a merge gate unless you also have deterministic tests and CODEOWNERS. If it only sees the hunk, it is a noisy linter.

How do I build a coding agent that does LLM pull request review?

Hook the forge webhook, pack diff plus symbols plus a small repo RAG, force structured findings with file and line, post them through the review API with a comment cap, and evaluate recall on historical blockers against senior-reviewer labels. Keep merge out of the bot's scopes.

Will the agent replace our senior reviewers?

No. It catches a subset of mechanical and security misses before a human looks, and it does it on every PR including the ones seniors skip. Architecture and product judgment stay human. Measure it that way: blocker recall and false-positive rate, not 'does it sound senior.'

How do we stop the bot from nitpicking?

Encode an allowlist of rules, default nits off, cap comments, and grade false positives against historical 'won't fix' threads. Teams that skip the cap ship a bot that gets muted in a week. Put nits behind a label so authors who want style notes can opt in without forcing them on everyone.

Does the review agent run on your SaaS or in our git?

In your environment. ReinforcedX implements the agent in your cloud and forge. You own weights, prompts, labeled PRs, and runbooks. Four weeks, 30 days on-call, no token markup, model-agnostic. We will not require uploading the monorepo to a third-party trainer.

Want this built for your team?

From architecture review to a deployed, evaluated system your engineers own — we ship it with you.

FAQ

Working with us

How soon can this build work start?

Typically within a week or two of a scope being agreed. The first delivery is deliberately a small batch so you can check the output against your expectations before volume ramps.

What do you need from our team?

One process owner who knows the workflow, one engineer with access to the systems involved, and a weekly 45-minute review. No standing committee, and no requirement for an ML specialist on your side.

Who owns the output and the data?

You do. Datasets, labels, weights, evaluation suites and runbooks are yours and are handed over at the end. Your data trains your models only, with zero-retention provider settings by default.

Can you scale volume up quickly if we need it?

Yes, and the quality bar holds because the rubric and gold set are already agreed by that point. Ramping is a staffing question, not a re-scoping one, so it usually takes days rather than a new engagement.

We already have a vendor for this. Why switch?

Often you should not. The cases where teams move to us are when they cannot get a quality number out of their current vendor, or when the work is delivered as an opaque batch with no trace of how disagreements were resolved.

What happens after the engagement ends?

We stay on-call for 30 days at no extra cost, then move to an optional support retainer. Most teams also keep a quarterly evaluation review with us to catch drift early.

Copyright © 2026
ReinforcedX, Inc.
All rights reserved