Automate Your Pull Request Reviews with a Custom AI Agent
Build a custom AI agent to automatically review pull requests, check for code quality, and suggest improvements.
Posted on: 2026-03-16 by AI Assistant

Automate Your Pull Request Reviews with a Custom AI Agent
Pull request reviews are an essential part of the software development lifecycle, but they can be time-consuming and prone to human error. What if you could have an AI assistant that automatically reviews code changes, points out potential bugs, and ensures adherence to coding standards?
In this tutorial, you will learn how to build a custom AI agent that automatically reviews pull requests using the Gemini API and integrates directly into your CI/CD pipeline.
Prerequisites
- A GitHub repository to test the agent.
- A Gemini API Key.
- Basic knowledge of Python and GitHub Actions.
Setting Up the AI Agent
We will build a simple Python script that fetches the diff of a pull request and passes it to the Gemini model for analysis.
The Review Script
Create a file named review_pr.py:
import os
import requests
from google import genai
def get_pr_diff(repo, pr_number, token):
url = f"https://api.github.com/repos/{repo}/pulls/{pr_number}"
headers = {
"Authorization": f"token {token}",
"Accept": "application/vnd.github.v3.diff"
}
response = requests.get(url, headers=headers)
return response.text
def analyze_code(diff):
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
prompt = f"""
You are an expert software engineer reviewing a pull request.
Please review the following code diff and provide constructive feedback.
Focus on potential bugs, performance issues, and code readability.
Diff:
{diff}
"""
response = client.models.generate_content(
model='gemini-2.5-pro',
contents=prompt
)
return response.text
if __name__ == "__main__":
# Assume these are passed as environment variables in GitHub Actions
repo = os.environ["GITHUB_REPOSITORY"]
pr_number = os.environ["PR_NUMBER"]
github_token = os.environ["GITHUB_TOKEN"]
diff = get_pr_diff(repo, pr_number, github_token)
feedback = analyze_code(diff)
print("### AI Code Review\n")
print(feedback)
Integrating with GitHub Actions
To automate this, we can set up a GitHub Action that runs every time a pull request is opened or updated.
Create a file at .github/workflows/ai-pr-review.yml:
name: AI PR Review
on:
pull_request:
types: [opened, synchronize]
jobs:
review:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies
run: pip install requests google-genai
- name: Run AI Review
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
GITHUB_REPOSITORY: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
python review_pr.py > review_output.txt
- name: Comment PR
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');
const feedback = fs.readFileSync('review_output.txt', 'utf8');
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: feedback
});
Conclusion & Next Steps
You’ve successfully built an automated AI PR reviewer! Your agent will now automatically leave comments on new pull requests, saving you time and catching potential issues early.
For next steps, consider giving your agent access to your repository’s style guide or linting rules so it can provide more personalized feedback.