← Blog

Claude API Code Review Bot Tutorial

2026-11-15 · 5 min read · SubToAPI Team

Building a code review bot with the Claude API means wiring three pieces together: a webhook that fires when a pull request opens or updates, a prompt that turns a diff into useful feedback, and a way to post that feedback back as PR comments. This tutorial walks through all three, using GitHub as the example platform, and shows where things typically break.

If you've searched for a Claude API code review bot tutorial, you're probably trying to decide between writing raw diffs into a one-shot prompt or building something closer to a real reviewer that understands file context, flags security issues, and doesn't comment on things that don't matter. The second option takes more code but is the only version that survives contact with a real engineering team. This guide builds that version.

What the bot actually needs to do

A code review bot that people tolerate (let alone like) needs to:

Skipping the "structured comments" step is the most common mistake. If you just paste Claude's prose response as one giant PR comment, nobody reads it after the second review.

Step 1: Set up the webhook

Create a GitHub App or a repo webhook pointed at your server, subscribed to pull_request events. On opened and synchronize, fetch the diff:

curl -s \
  -H "Authorization: Bearer $GITHUB_TOKEN" \
  -H "Accept: application/vnd.github.v3.diff" \
  "https://api.github.com/repos/$OWNER/$REPO/pulls/$PR_NUMBER" \
  -o pr.diff

Keep the diff size in check. Large PRs should be chunked per file, or you'll blow past context limits and get vague, generic feedback because Claude is trying to hold the whole thing in its head at once.

Step 2: Design a prompt that produces structured output

Ask for JSON directly. This is the single biggest quality improvement over free-text prompts, because it forces the model to commit to specific files and line numbers instead of hedging in paragraphs.

You are a senior code reviewer. Review the following diff for:
- bugs and logic errors
- security issues (injection, secrets, unsafe deserialization)
- obvious performance problems
- missing error handling

Ignore style nits unless they cause real bugs. Only comment on lines
that changed. If there is nothing worth flagging, return an empty array.

Return ONLY valid JSON matching this shape:
[{"file": "path/to/file.js", "line": 42, "severity": "high|medium|low", "comment": "..."}]

Diff:
<<<DIFF>>>

Calling Claude through SubToAPI

If you're already using Claude through SubToAPI, the request looks like a standard Messages call with your sub_live_... key:

curl https://api.subtoapi.app/v1/messages \
  -H "Authorization: Bearer $SUBTOAPI_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-sonnet-4",
    "max_tokens": 2000,
    "messages": [
      {"role": "user", "content": "REVIEW_PROMPT_WITH_DIFF"}
    ]
  }'

This is useful specifically for a review bot because PR volume on a busy repo can spike unpredictably, and you get usage metadata per request without building your own tracking. See /docs/quickstart for setup and /docs/messages for the full request format.

Step 3: Parse and post comments

Validate the JSON before posting anything — Claude is good at following the schema but you should still guard against malformed output, especially on large diffs.

const raw = response.content[0].text;
let comments;
try {
  comments = JSON.parse(raw);
} catch {
  console.error("Non-JSON response, skipping this PR");
  return;
}

const reviewComments = comments
  .filter(c => c.severity !== "low")
  .map(c => ({
    path: c.file,
    line: c.line,
    body: `**[${c.severity}]** ${c.comment}`
  }));

await fetch(`https://api.github.com/repos/${owner}/${repo}/pulls/${pr}/reviews`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${githubToken}`,
    Accept: "application/vnd.github.v3+json"
  },
  body: JSON.stringify({
    event: "COMMENT",
    comments: reviewComments
  })
});

Filtering out low severity before posting keeps the bot from drowning real issues in nitpicks. Teams generally want high and medium severity surfaced automatically, with low-severity stuff available on request only.

Handling streaming and long diffs

For large PRs you don't need token-by-token streaming — the bot is posting one batched review, not a live chat — but if you want to show progress in a CI log while the model works through a big diff, Claude's streaming responses work the same way here as in any other integration. If you're routing through SubToAPI, streaming is documented at /docs/streaming.

If your bot needs to actually run commands (fetch related files, run a linter, check test coverage) rather than just read a diff, that's a tool-use pattern rather than a plain completion. SubToAPI exposes tool calling the same way the native API does — see /docs/tools if you want the bot to pull in extra context before forming its review.

Reducing false positives

A few practical tweaks matter more than prompt wording:

Deployment notes

Run this as a small webhook server (Node/Express, or a serverless function) rather than a long-running process — reviews are bursty and stateless. Store the GitHub token and your Claude API key as environment secrets, never in the repo. If you're evaluating providers, start with a trial — /signup gives free trial access, and plans scale from Solo at €9 up through Team and Scale seats on /pricing if multiple repos or reviewers need their own keys.

Questions

Does the bot need to see the full repo or just the diff? Just the diff plus a small amount of surrounding context per file. Sending the whole repo wastes tokens and often produces worse, less focused feedback.

How do I stop the bot from commenting on every PR? Filter by severity before posting, cap the number of inline comments, and skip generated or vendored files entirely in your diff-fetching step.

Can I use this with private repos? Yes — the GitHub side just needs a token with repo read access and PR write access; nothing about the Claude API call changes between public and private repos.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →