Code reviews are essential for keeping your codebase healthy, but they can slow things down. If you already have a few tickets waiting and someone adds a huge pull request, switching focus can be tough. Imagine if you could automate the first round of code reviews without sharing your company’s code with a paid third-party service. In this article, I’ll show you how to build an AI Code Review Bot for your GitHub.
AI Code Review Bot for GitHub: Getting Started
Today, we’ll build a working AI-powered code review bot with Python. You’ll connect an open-source large language model running on your own computer to your GitHub repository. The bot will fetch pull requests, review the code changes, and post suggestions right on GitHub. This means no API costs, full control over your data, and a big productivity boost for your team.
First, install the project’s basic Python dependency:
pip install requests
Next, install Ollama to run open-source LLMs locally. After installing Ollama, pull an open-source model such as Mistral:
ollama pull mistral
This command downloads the model so you can run it on your own machine. Now you have everything you need to run an AI model locally.
Step 1: Create a GitHub Personal Access Token
Your script needs permission to read pull requests and post comments. Go to:
GitHub → Settings → Developer Settings → Personal Access Tokens
Create a token with these permissions:
- repo
- pull_requests
After you create your token, copy it and continue to the next step.
Step 2: Environment Setup and Authentication
Start by setting up your environment variables and GitHub authentication headers:
import requests
import os
GITHUB_TOKEN = "your_github_token"
REPO_OWNER = "your_github_username"
REPO_NAME = "repository_name"
headers = {
"Authorization": f"token {GITHUB_TOKEN}",
"Accept": "application/vnd.github.v3+json"
}Step 3: Fetching Pull Requests and Code Diffs
Next, you’ll need functions to interact with your repository. The goal is to find open pull requests and get the specific files that have changed:
def get_pull_requests():
# Endpoint to list pull requests
url = f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/pulls"
response = requests.get(url, headers=headers)
return response.json()
def get_pr_files(pr_number):
# Endpoint to get the specific files changed in a PR
url = f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/pulls/{pr_number}/files"
response = requests.get(url, headers=headers)
return response.json()The get_pull_requests function gets a list of all active pull requests. After you pick a pull request to review, pass its ID to get_pr_files. This function returns the patch data, showing the lines of code that were added or removed. That’s exactly what the AI needs to review.
Step 4: Analyzing Code with a Local LLM
This step is the heart of the bot. You’ll take the code patch and put it into a well-structured prompt. Good prompt design is important here because you need to tell the language model what role to take and what kind of feedback to give:
import json
def analyze_code(code_patch):
# Define the system prompt and instructions
prompt = f"""
You are an expert software engineer performing a code review.
Analyze the following code changes and suggest improvements,
possible bugs, performance issues, and style improvements.
Code Changes:
{code_patch}
Provide clear suggestions.
"""
# Send the prompt to our local Ollama instance running Mistral
response = requests.post(
"http://localhost:11434/api/generate",
json={
"model": "mistral",
"prompt": prompt,
"stream": False
}
)
result = response.json()
return result["response"]Ollama provides a local REST API on port 11434, so you interact with it just like a remote web service. You send the model name, your prompt, and set stream to False to get the full review in one response.
Step 5: Posting the Review back to GitHub
After Mistral creates its review, you need to send that feedback to the developer. In GitHub’s API, pull request comments use the Issue Comments endpoint, since every pull request is actually an issue under the hood:
def post_comment(pr_number, comment):
# GitHub API treats PR comments as issue comments
url = f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/issues/{pr_number}/comments"
data = {
"body": f"AI Code Review Suggestions:\n\n{comment}"
}
requests.post(url, headers=headers, json=data)Add an introductory header to the comment so your team knows it was generated by AI. Being transparent is important when you add AI tools to your workflow.
Closing Thoughts
Building this AI Code Review Bot for GitHub is more than just automating a task. It helps you learn how traditional software engineering skills like API integration and authentication connect with new GenAI tools like prompt design and running models locally.
One key takeaway is that AI can be affordable and doesn’t have to depend on cloud APIs. Open-source models like Mistral or Llama are powerful and let you build secure, privacy-focused AI tools on your own laptop.
If you found this article helpful, you can follow me on Instagram for daily AI tips and practical resources. You may also be interested in my latest book, Hands-On GenAI, LLMs & AI Agents, a step-by-step guide to prepare you for careers in today’s AI industry.





