Introduction
A code review catches problems before they reach production. Adding an AI assistant to that process can speed up common checks, but it should not replace human judgment. This guide develops a practical pipeline where an AI assistant prepares an initial review draft and a human reviewer makes the final decision. It is a workflow design exercise, not a recommendation to automate code review entirely.
Start with the review contract
Before calling any model, define what the pipeline must produce. A useful contract lists the deliverable, the permitted inputs, and the decisions that remain with a person. For this example, the deliverable is a draft review that includes:
- A summary of the change
- A list of potential issues grouped by severity
- References to the relevant code locations
- A recommended action: approve, request changes, or hold for discussion
The contract must also state what the assistant may not do. It should not merge pull requests, close comments, push commits, or modify source code directly. Those actions belong to a person or a controlled automation step with explicit authorization.
Define severity levels before the first review run. For this example, use four levels:
- Critical: a change that may expose data, break authentication, or introduce a security vulnerability. These always require human review and must never be auto-approved.
- Major: a likely bug, a missing error path, or a performance regression that affects the main flow. These require a human decision before merging.
- Minor: a style inconsistency, a missing docstring, or a naming issue. These can be suggested but are not blocking.
- Information: an observation or a question that does not clearly indicate a problem. These are informational only.
Severity levels give the reviewer a consistent way to triage suggestions and prevent low-priority items from blocking a release.
Select the input scope
A pull request can touch many files. Sending the entire diff to a model risks context limits, cost, and noise from unrelated changes. Define the input scope before each review run.
A reasonable default includes:
- The pull request title and description
- The changed files and their diffs
- Relevant test files, if changed
- The repository's contribution guidelines, if they exist
Exclude generated files, lockfiles, and vendored dependencies unless the change specifically concerns them. Record which files were included so that a reviewer can reproduce the context later.
For a large pull request, consider splitting the review into logical units instead of sending everything at once. Reviewing by module or by feature keeps each model call focused and makes it easier to verify that every cited line number is accurate.
Prepare the model input
Construct a prompt that gives the assistant a clear role and output format. The prompt should instruct it to:
- Read the changed code and the contribution guidelines
- List potential issues with severity levels
- Cite specific line numbers where possible
- Avoid inventing policies not present in the guidelines
- Mark uncertain observations as "needs human review"
Use placeholders for variable parts of the input so the same prompt template can be reused across repositories. For example:
Review the following pull request for the repository {{repository_name}}.
Contribution guidelines:
{{contribution_guidelines}}
Changed files:
{{changed_files}}
Severity levels:
- Critical: security vulnerability or data exposure
- Major: likely bug or performance regression
- Minor: style inconsistency or missing documentation
- Information: observation or question
Return a draft review with issue severity, location, and recommended action. Mark any uncertain observation as needs human review.
Keep secrets out of the prompt. Do not include credentials, API keys, or internal documentation that the model should not retain. If the contribution guidelines contain sensitive internal policy, summarize the public-facing rules instead.
Run the review as a bounded step
Invoke the model as one step in a larger pipeline. Set a timeout and a maximum token budget so that a slow or runaway call cannot block the entire workflow. Record the model identifier, prompt version, input hash, and output for each run.
Choose a timeout appropriate to your environment. A 30-second timeout may be enough for small diffs, while large pull requests may need 90 seconds. If the model call times out, record the timeout as an error and do not retry automatically. An automatic retry could duplicate work or exceed rate limits.
If the model returns an invalid format, treat it as a pipeline failure rather than attempting to repair the output silently. Log the raw response alongside the structured result so that a person can diagnose the mismatch later. Do not guess at what the model meant.
Validate and separate results
The model's output is a proposal, not a verified review. A separate validation stage should:
- Confirm every cited file exists in the pull request
- Confirm every cited line number falls within the changed range
- Flag any recommendation that conflicts with an explicit contribution guideline
This validation can be deterministic code. It does not need another model call. Keep model-generated suggestions separate from validated facts so that a reviewer can see what was checked and what remains an open question.
Here is a concrete example. Suppose the model suggests:
Critical: The function
process_paymenton line 42 does not validate the payment amount.
The validation stage checks:
- Does
process_paymentexist in a changed file? Yes. - Is line 42 within the changed range? Yes.
- Does the contribution guideline require amount validation? If no guideline exists, flag this as "needs human review" rather than treating it as a confirmed finding.
If the model cited line 42 but the changed file only has 30 lines, the validation stage marks the citation as invalid and removes it from the draft. The reviewer never sees a broken reference.
Worked example: reviewing a pull request
Imagine a pull request that adds a user export feature to a web application. The changed files are:
app/export.py— new file, 120 linesapp/routes.py— modified, adds one routetests/test_export.py— new test file
The pipeline input includes the diff for all three files plus the contribution guidelines, which state that all new functions must have docstrings and that sensitive fields must not appear in logs.
The model drafts the following suggestions:
- Minor:
app/export.py, line 15, missing docstring onexport_users. - Critical:
app/export.py, line 68, the user email appears in a log statement. - Major:
app/routes.py, line 12, no error handling for the case when the export directory does not exist. - Information:
tests/test_export.py, line 5, consider adding a test for empty result sets.
The validation stage confirms all file paths and line numbers. It checks the contribution guidelines and confirms that docstrings are required for new functions, so suggestion 1 is validated. It confirms that sensitive fields must not appear in logs, so suggestion 2 is validated as Critical. It confirms that suggestion 3 is plausible but requires a human decision about whether error handling belongs in this route. Suggestion 4 remains informational.
The reviewer accepts suggestion 1, rejects suggestion 2 after confirming the log line was already removed in a later commit, accepts suggestion 3 with a modified error message, and files suggestion 4 as a follow-up task.
Human review and final decision
A person must make the final review decision. The AI draft should appear beside the code diff, with each suggestion shown as a separate item that the reviewer can accept, reject, or edit. Record which suggestions were accepted and which were overridden. This log becomes useful data for improving the prompt and the pipeline over time.
The reviewer should also check that the assistant did not miss obvious issues. If a pattern of missed problems appears, adjust the prompt or the input scope rather than trusting the model more. For example, if the model consistently misses missing error handling, add an explicit instruction to check every function call for error returns.
Do not treat a successful model response as proof that the code is correct. A fluent review draft can still miss real problems or flag nonexistent ones. The human reviewer remains accountable for the final decision.
Track metrics without overclaiming
Measure time to first review, number of suggestions per pull request, acceptance rate, and override rate. These metrics describe pipeline behavior, not code quality improvement. Do not claim that AI review reduces defects unless you have a controlled comparison that isolates review quality from other changes.
The acceptance rate is the fraction of AI suggestions that a reviewer accepts without modification. The override rate is the fraction that a reviewer edits before accepting. A high override rate may mean the prompt needs refinement, while a low acceptance rate may mean the model is producing too many false positives.
Record false positives and false negatives separately. A false positive is a suggestion the reviewer rejects because no problem exists. A false negative is a real issue that the model missed and the reviewer found independently. Both are useful for tuning the pipeline, but neither alone establishes that the pipeline improves review quality.
Limitations
An AI assistant can miss security vulnerabilities, misunderstand business logic, and suggest changes that compile but behave incorrectly. It may also reproduce common coding mistakes from its training data. Treat every suggestion as something to verify, not as authoritative guidance.
The model does not understand your repository's conventions unless they are present in the contribution guidelines or in the changed code. It may suggest a pattern that works in one codebase but violates another codebase's rules. It also cannot run tests, check types, or execute the code it reviews.
This pipeline works best as a first pass that surfaces routine issues, leaving the human reviewer free to focus on architecture, security, and domain-specific concerns. It is not a substitute for a knowledgeable reviewer who understands the application.
Implementation considerations
Prompt engineering best practices
The prompt should be concise but comprehensive. Test variations to find the optimal balance between specificity and flexibility. Include:
- Clear role definition (e.g., "You are a senior code reviewer")
- Specific output format requirements
- Examples of desired output structure
- Warnings against hallucination and invented policies
Validation implementation
The validation layer should be implemented as deterministic code that:
- Parses the AI output into structured items
- Checks file paths against the actual changed files
- Verifies line numbers fall within the expected range
- Cross-references with contribution guidelines
- Flags any inconsistencies for reviewer attention
This layer can be written in the same language as the rest of the pipeline and should be thoroughly tested with known good and bad inputs.
Metrics and continuous improvement
Track these key metrics:
- Time to first review: How long from PR creation to initial AI review
- Suggestions per PR: Average number of AI-generated items
- Acceptance rate: Percentage of suggestions accepted without modification
- Override rate: Percentage of suggestions that are edited before acceptance
- False positive rate: Suggestions that reference non-existent issues
- False negative rate: Real issues that the AI missed
Use these metrics to iterate on prompt design, input scope, and validation rules. The pipeline should evolve based on reviewer feedback and observed performance.
Scaling considerations
For teams with many reviewers, consider:
- Standardizing contribution guidelines to reduce ambiguity
- Creating reusable prompt templates for common repository types
- Building a library of common issue patterns with severity classifications
- Implementing role-based access to review logs and metrics
Conclusion
An AI-augmented code review pipeline separates preparation from decision. The model drafts the review; a person decides what to act on. Define the contract, bound the input, validate the output, and keep final approval with a reviewer. That structure makes the pipeline inspectable, correctable, and safe to operate.
Sources
- Anthropic: Building effective agents, for the distinction between fixed workflows and autonomous agents.
- OpenAI: API documentation, for model invocation and token limits.
- MCP architecture overview, for tool and resource concepts used in pipeline design.
For a complementary planning exercise, use the workflow design guide to define where AI adds value in automated workflows.