Building an AI Code Review Bot: GitHub Actions, Diffs and Feedback Loops
Build an AI code review bot with GitHub Actions: trigger on pull requests, send the diff to a model, post inline feedback, and tune the loop.
By Galactic News Editorial · Published 2026-11-20T10:26:07+00:00 · Updated 2026-11-20T10:26:07+00:00
Building an AI Code Review Bot: GitHub Actions, Diffs and Feedback Loops
An AI code review bot reads every pull request, checks the diff, and posts feedback before a human reviewer opens the code. This guide shows how to build one with GitHub Actions. You will set up the workflow, send the diff to a language model, post the review as a comment, and tune the feedback loop so the bot stays useful instead of noisy.
How the AI code review bot GitHub workflow fits together
The bot has four moving parts. A GitHub Actions workflow triggers on pull request events. A step extracts the diff. A step sends the diff to a model with a review prompt. A step posts the result back to the pull request.
The trigger is the easy part. GitHub Actions workflow files use YAML and live in .github/workflows/. The on key defines which events start the workflow. For a review bot, trigger on pull_request with types opened, synchronize and reopened. That covers new pull requests and every new push to an open one.
Permissions matter from the start. Give the workflow the smallest access it needs. That means contents: read so it can check out the code, and pull-requests: write so it can post comments. Never give the bot write access to code. It reviews. It does not commit.
Security deserves a warning here. Do not use the pull_request_target trigger with a checkout of the pull request code. That combination can let untrusted code run with your secrets. The plain pull_request trigger is the safe default for this bot.
Step 1: Create the workflow file
Create .github/workflows/ai-review.yml in your repository. The file below is a working starting point. It checks out the code, builds the diff, calls a model API, and posts a comment.
name: AI Code Review
on:
pull_request:
types: [opened, synchronize]
permissions:
contents: read
pull-requests: write
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get PR diff
run: |
git diff origin/${{ github.base_ref }}...HEAD > diff.txt
- name: AI Review
env:
API_KEY: ${{ secrets.MODEL_API_KEY }}
run: |
DIFF=$(head -c 10000 diff.txt)
curl -s https://api.example-model.com/v1/messages -H "Authorization: Bearer $API_KEY" -d "{"prompt": "Review this code diff for bugs, security issues and improvements. Be concise.\n\n$DIFF"}" -o review.txt
- name: Post Review Comment
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const review = fs.readFileSync('review.txt', 'utf8');
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: `## AI Code Review
${review}`
});
The example uses a placeholder model endpoint. Swap in your provider's real API, such as Anthropic, OpenAI or Google Gemini. The structure stays the same. Each provider needs an API key and a prompt. Keep the key in repository secrets, never in the workflow file.
Step 2: Store the API key safely
Go to your repository settings, then Secrets and variables, then Actions. Add a new repository secret with your model provider's API key. Name it clearly, such as MODEL_API_KEY or ANTHROPIC_API_KEY.
The workflow reads the secret through the secrets context. GitHub masks secret values in logs. Even so, never print a secret, and never pass it to a step that echoes its inputs. Rotate the key if it ever appears in a log.
One key per repository is enough for a small team. Larger teams may prefer one key per organisation, stored as an organisation secret. That keeps rotation in one place.
Step 3: Get the diff right
The diff is the bot's only input. A bad diff means a bad review. The command git diff origin/main...HEAD compares the pull request branch against its base. The fetch-depth: 0 setting on the checkout step makes sure the full history is available, so the diff is complete.
Large diffs are a problem. Models have context limits, and a 5,000-line diff will drown the review in noise. Trim the diff before you send it. Filter to the file types you care about, such as *.ts, *.js, *.py and *.go. Exclude lockfiles, generated code and minified files. Cap the size, for example at 10,000 characters for a simple setup, or a larger cap if your model supports it.
OpenAI's own guide to this pattern takes the same approach. It gathers the diff of changed files, excludes JSON and image files, and sends the changes for analysis. Its workflow then posts suggested fixes as a comment on the pull request. A second job checks the code against defined enterprise standards and returns a summary table.
For inline comments, you need line numbers. Parse the diff headers to find the changed lines, then ask the model for structured output, such as JSON with file, line and comment fields. A script can then post each comment on the exact line it refers to. One tutorial builds exactly this with the Gemini API and a short Python script inside the workflow.
Step 4: Write a prompt that gives useful reviews
The prompt decides the quality of the review. A vague prompt returns vague praise. A focused prompt returns findings a developer can act on.
Ask for four categories: bugs, security issues, error handling, and code quality. Bugs cover off-by-one errors, null dereferences and unhandled promises. Security covers hardcoded secrets, injection patterns and cross-site scripting vectors. Error handling covers missing try and catch blocks and unhandled rejections. Quality covers naming, magic numbers and overly complex logic.
Ask the model to be concise. Ask it to quote the code it refers to. Ask it to skip style nits that your linter already catches. A review with three real findings beats a review with thirty guesses.
Include your team's standards in the prompt. If your project forbids a certain pattern, say so. If your project requires tests for new functions, say so. The bot can only enforce rules it knows about.
Step 5: Build the feedback loop
A review bot without feedback goes stale. Developers ignore it, and it keeps making the same mistakes. Build a loop that improves it every week.
First, track the signal. Count how many comments developers act on, and how many they dismiss. If the fix rate drops below half, the prompt or the diff filter needs work.
Second, collect corrections. When a developer marks a comment as wrong, note why. Common causes are missing context, outdated standards in the prompt, and generated code the bot was told to skip but did not.
Third, tune in small steps. Change one thing at a time: the prompt, the diff filter, or the file list. Then watch the fix rate for a week. Changing three things at once tells you nothing.
Fourth, keep the bot advisory. Never block a merge on the bot's opinion alone. A human reviewer makes the final call. The bot is a second pair of eyes, not a gatekeeper.
Comparing build approaches
| Approach | Setup effort | Comment style | Best for |
|---|---|---|---|
| Simple curl workflow | Low, one YAML file | Single summary comment | Small teams starting out |
| Structured JSON output | Medium, needs a parser script | Inline comments on exact lines | Teams that want precise feedback |
| Managed review app | Low, install from marketplace | Varies by vendor | Teams that prefer no maintenance |
| Two-job standards check | Medium, two jobs in one workflow | Summary comment plus standards table | Teams with written coding standards |
Costs depend on diff size and model pricing. One tutorial reports about $0.02 per pull request using a Claude API setup, but your cost will vary with the model you choose and the size of your diffs. Check your provider's pricing page and set a monthly budget alert.
Bottom line
Start with the simple workflow: trigger, diff, model call, comment. Run it on one repository for two weeks. Watch the fix rate. Then add inline comments and a feedback loop. A review bot earns its place by catching real bugs early. If developers start ignoring it, fix the prompt before you add features.