Claude API Code Review Automation for GitHub
Automating code review with the Claude API on GitHub
If you're searching for "claude api code review automation github," you're probably trying to wire Claude into your pull request workflow so it comments on diffs automatically, catches obvious bugs, and flags style issues before a human reviewer even opens the PR. This is a well-solved problem: you don't need a custom bot framework, just a GitHub Actions workflow that pulls the diff, sends it to Claude with a focused prompt, and posts the result back as a PR comment.
The short answer is: use a GitHub Action triggered on pull_request, fetch the diff with the GitHub API or git diff, call Claude's Messages API with that diff plus review instructions, and post the response using the GitHub Comments API (or actions/github-script). Below is a complete working setup, plus the prompt-engineering details that make the difference between a reviewer that's useful and one that spams noise on every PR.
Why automate code review with Claude
Claude is well suited to this task because it can reason about control flow, spot edge cases, and explain why something is risky — not just pattern-match against a linter ruleset. A good automated review doesn't replace human reviewers; it catches the boring stuff (unhandled errors, missing null checks, inconsistent naming, obvious security issues) so humans can focus on architecture and intent.
The things that actually matter when automating this:
- Scope the diff, not the whole repo. Sending entire files wastes tokens and buries the model in irrelevant context.
- Give Claude a fixed rubric. Vague prompts like "review this code" produce vague, inconsistent output.
- Keep the output structured. Markdown with file/line references is far easier to skim than a wall of prose.
- Rate-limit comments. Nobody wants a bot commenting on every single PR update.
A GitHub Actions workflow
Here's a minimal workflow that triggers on pull requests, grabs the diff, and calls Claude:
name: Claude Code Review
on:
pull_request:
types: [opened, synchronize]
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get diff
run: |
git diff origin/${{ github.base_ref }}...HEAD > diff.txt
- name: Run Claude review
env:
CLAUDE_KEY: ${{ secrets.CLAUDE_API_KEY }}
run: node .github/scripts/review.js
- name: Post comment
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const body = fs.readFileSync('review.md', 'utf8');
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body
});
And the script that calls Claude:
// .github/scripts/review.js
const fs = require("fs");
const diff = fs.readFileSync("diff.txt", "utf8").slice(0, 60000); // keep it bounded
const prompt = `You are reviewing a pull request diff. Focus only on:
1. Bugs or logic errors
2. Missing error handling
3. Security issues (injection, secrets, unsafe deserialization)
4. Clear readability problems
Ignore formatting/style that a linter would catch. For each issue, reference the
file and approximate line. If there are no issues, say "No blocking issues found."
Diff:
${diff}`;
async function run() {
const res = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"content-type": "application/json",
"x-api-key": process.env.CLAUDE_KEY,
"anthropic-version": "2023-06-01",
},
body: JSON.stringify({
model: "claude-opus-4-5",
max_tokens: 1024,
messages: [{ role: "user", content: prompt }],
}),
});
const data = await res.json();
const text = data.content[0].text;
fs.writeFileSync("review.md", `## Claude Review\n\n${text}`);
}
run();
This works, but calling api.anthropic.com directly from a CI job means managing a raw key as a repo secret, no visibility into which workflow run used how many tokens, and no easy way to swap models or rotate credentials without editing the secret manually.
Using SubToAPI instead of raw key management
If you'd rather not juggle the anthropic key directly in CI secrets, SubToAPI turns your existing Claude access into a standard HTTPS API with its own scoped keys (sub_live_...). That means your GitHub Action calls api.subtoapi.app instead, and you get per-key usage tracking so you can see exactly how many review runs are hitting the API and what they cost, without digging through Anthropic's own billing console.
Swapping the fetch call above is a one-line change:
const res = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"content-type": "application/json",
"Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
"anthropic-version": "2023-06-01",
},
body: JSON.stringify({
model: "claude-opus-4-5",
max_tokens: 1024,
messages: [{ role: "user", content: prompt }],
}),
});
This is useful when multiple repos or teams share one Claude subscription — you can issue a separate sub_live_ key per repository or per CI pipeline, see token usage broken out by key in the dashboard, and revoke a single key if a workflow misbehaves instead of rotating the whole account's credentials. See the quickstart and Messages API reference for the full request format, and pricing if you're evaluating it for a team.
Making the reviews actually useful
A few practical refinements that matter more than the plumbing:
- Skip trivial PRs. Add a check for diff size; if it's under ~5 lines, don't bother calling the API.
- Use labels to control behavior. A
skip-reviewlabel lets contributors opt out for docs-only or generated-file changes. - Cache repeated review on
synchronize. Only re-run on meaningfully new commits, not every force-push, to avoid duplicate comments. - Separate "blocking" from "suggestion". Have Claude tag issues by severity so your CI can optionally fail the build on blocking issues only.
- Log prompts and outputs. Store the diff and response as workflow artifacts so you can tune the prompt over time based on false positives.
If you want structured, enforceable output (e.g., failing the build only on high-severity findings), define a JSON schema in the prompt and use tool use so Claude returns a typed object instead of freeform markdown — this makes it trivial to parse severity in the next workflow step.
Questions
Does Claude have a native GitHub integration for code review? Not out of the box — there's no first-party GitHub App from Anthropic for this. You build it yourself with a GitHub Actions workflow calling the Messages API, which is what most teams actually do.
Will this slow down CI or block merges? Only if you make it a required check. Most teams run it as a non-blocking comment step, then later add a required check once they trust the output and have tuned false-positive rates down.
How do I avoid huge API bills from reviewing every PR? Bound the diff size you send, skip trivial changes, and use a smaller/faster model for routine PRs, reserving a stronger model for larger diffs. Tracking usage per key, as with SubToAPI, makes it easy to spot which repos are driving cost.