Infrastructure
developer tools
How to Create an Automated Code Review System
Review code automatically with AI-powered quality analysis.
Jay Banlasan
The AI Systems Guy
An automated code review system with AI quality analysis catches bugs, style issues, and security problems before code ships. I run AI reviews on every commit in my projects. It is not a replacement for human code review on critical paths, but it handles the 90% of reviews that are about consistency and basic quality.
What You Need Before Starting
- Python 3.8+
- Anthropic API key
- Git installed
- A project repository
Step 1: Get the Diff to Review
import subprocess
def get_git_diff(base_branch="main"):
result = subprocess.run(
["git", "diff", base_branch, "--unified=5", "--diff-filter=ACMR"],
capture_output=True, text=True
)
return result.stdout
def get_staged_diff():
result = subprocess.run(
["git", "diff", "--cached", "--unified=5"],
capture_output=True, text=True
)
return result.stdout
Step 2: Build the Review Engine
import anthropic
from dotenv import load_dotenv
load_dotenv()
REVIEW_PROMPT = """Review this code diff. Focus on:
1. Bugs or logic errors
2. Security vulnerabilities (SQL injection, hardcoded secrets, etc.)
3. Performance issues
4. Missing error handling
5. Code style inconsistencies
For each issue found, respond with:
- File and line number
- Severity (critical/warning/suggestion)
- What the problem is
- How to fix it
If the code looks good, say so briefly. Do not invent problems that do not exist."""
def review_diff(diff_text, context=""):
client = anthropic.Anthropic()
message = f"Code context: {context}\n\n" if context else ""
message += f"Review this diff:\n\n```diff\n{diff_text}\n```"
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=2048,
system=REVIEW_PROMPT,
messages=[{"role": "user", "content": message}]
)
return response.content[0].text
Step 3: Parse Review Results
import re
def parse_review(review_text):
issues = []
current_issue = None
for line in review_text.split("\n"):
severity_match = re.search(r"\**(critical|warning|suggestion)\**", line.lower())
if severity_match:
if current_issue:
issues.append(current_issue)
current_issue = {
"severity": severity_match.group(1),
"text": line.strip()
}
elif current_issue:
current_issue["text"] += "\n" + line.strip()
if current_issue:
issues.append(current_issue)
return issues
Step 4: Build the CLI
import argparse
def main():
parser = argparse.ArgumentParser(description="AI Code Review")
parser.add_argument("--staged", action="store_true", help="Review staged changes")
parser.add_argument("--branch", default="main", help="Base branch for diff")
parser.add_argument("--context", default="", help="Project context for better reviews")
args = parser.parse_args()
if args.staged:
diff = get_staged_diff()
else:
diff = get_git_diff(args.branch)
if not diff.strip():
print("No changes to review.")
return
print(f"Reviewing {len(diff.splitlines())} lines of changes...\n")
review = review_diff(diff, args.context)
print(review)
issues = parse_review(review)
critical = [i for i in issues if i["severity"] == "critical"]
if critical:
print(f"\n{len(critical)} critical issues found. Fix before merging.")
exit(1)
if __name__ == "__main__":
main()
Step 5: Add as a Git Hook
Create .git/hooks/pre-push:
#!/bin/bash
echo "Running AI code review..."
python3 /path/to/code_review.py --branch origin/main
if [ $? -ne 0 ]; then
echo "Critical issues found. Push blocked."
exit 1
fi
chmod +x .git/hooks/pre-push
What to Build Next
Add a review history database. Track which files get the most issues over time. That tells you where to focus refactoring effort.
Related Reading
- The Testing Pyramid for AI Operations - quality layers for AI-powered development
- Claude Code: The AI Coding Tool That Changes Operations - AI in the coding workflow
- Building Your First Automation: A Complete Guide - automation fundamentals
Want this system built for your business?
Get a free assessment. We will map every system your business needs and show you the ROI.
Get Your Free Assessment