Artificial IntelligenceSoftware DevelopmentProjects

Build an Automated GitHub PR Review Agent with Claude

TT
TopicTrick
Build an Automated GitHub PR Review Agent with Claude

What Does This PR Review Agent Do?

This project builds a GitHub PR review agent that fetches pull request diffs via the GitHub API, sends each changed file to Claude with a structured review prompt, receives JSON-formatted findings categorised by severity, and posts a formatted review comment directly to the pull request — automatically triggered by GitHub Actions on every PR open or update.

Code review is one of the highest-leverage activities in software development — and one of the most time-consuming. Every PR that sits unreviewed blocks a developer. Every review that misses a security issue costs tenfold later. Most teams are perpetually behind on review.

A PR review agent does not replace your senior engineer's judgment on architecture and design. But it can reliably handle the first pass: checking for common bugs, security issues, missing tests, style violations, and documentation gaps — before a human even opens the diff. That frees human reviewers to focus on the things that cannot be automated.

In this project you will build a complete GitHub PR review agent that:

  • Reads a pull request diff from GitHub
  • Analyses it across five dimensions: functionality, security, test coverage, code quality, and documentation
  • Posts structured review comments back to the PR via the GitHub API
  • Runs automatically on every new PR via GitHub Actions

Prerequisites

bash

You need:

  • An Anthropic API key
  • A GitHub personal access token with repo scope (or a GitHub App for production)
  • A GitHub repository to test against

How GitHub PR Reviews Work via API

Before building the agent, understand the GitHub objects involved:

  • Pull Request: has a title, description, base branch, and head branch
  • Files changed: each file has a patch (unified diff) and status (added/modified/removed)
  • Review: a top-level review object with overall verdict: APPROVE, REQUEST_CHANGES, or COMMENT
  • Review comments: inline comments attached to specific lines in the diff
  • Issue comments: general comments on the PR conversation thread

The agent reads the diff, passes it to Claude, Claude returns structured feedback, and the agent posts that feedback as a PR review with inline comments where appropriate.


Step 1: GitHub Client

python

Step 2: Build the Review Prompt

The key to a good review agent is a well-structured prompt. You want Claude to produce consistent, actionable output that maps cleanly to GitHub's review format.

python

Step 3: The Review Agent

python

Step 4: GitHub Actions Integration

Create .github/workflows/ai-pr-review.yml in your repository to run the agent automatically on every PR:

yaml

Add your secrets in GitHub repository settings:

  • ANTHROPIC_API_KEY: your Anthropic API key
  • GITHUB_TOKEN: this is automatically provided by GitHub Actions — no manual setup needed

Fork PR Security

The GitHub Actions workflow above includes `if: github.event.pull_request.head.repo.full_name == github.repository` to prevent external forks from triggering the workflow and potentially leaking your API key or abusing your quota. Never remove this check for public repositories.


    Step 5: Local Testing

    Test the agent locally before deploying to GitHub Actions:

    python

    Customisation: Adding Review Focus Areas

    You can extend the agent to focus on your team's specific concerns by modifying the prompt. Common additions:

    Framework-specific checks (e.g., Django):

    python

    Dependency security:

    python

    Performance:

    python

    Project File Structure

    text

    Key Takeaways

    • A PR review agent reads the unified diff from GitHub's API and passes it to Claude with a structured review prompt
    • Using a JSON output format from Claude makes it straightforward to parse the review and format it for different output targets
    • Always truncate large diffs — PR review quality drops on diffs over 5,000 lines and costs escalate fast
    • GitHub Actions integration makes this zero-maintenance — the agent runs on every PR automatically
    • Block fork PRs from triggering the action to prevent API key abuse
    • The agent is most valuable on security, tests, and code quality — invest in customising the prompt for your stack and team conventions

    What's Next in the AI Coding Agents Series

    1. What Are AI Coding Agents?
    2. AI Coding Agents Compared: GitHub Copilot vs Cursor vs Devin vs Claude Code
    3. Build Your First AI Coding Agent with the Claude API
    4. Build an Automated GitHub PR Review Agent ← you are here
    5. Build an Autonomous Bug Fixer Agent
    6. AI Coding Agents in CI/CD: Automate Code Reviews and Fixes in Production

    This post is part of the AI Coding Agents Series. Previous post: Build Your First AI Coding Agent with the Claude API.

    To deploy this agent in a full CI/CD pipeline with cost controls and human approval gates, see AI Coding Agents in CI/CD. For a simplified standalone code review assistant (without the full agentic loop), see Build a Code Review Assistant for GitHub PRs.

    External Resources