Latest AI news

Home  /  Guides  /  Claude 3.5 Sonnet code review automation with GitHub Actions

Claude 3.5 Sonnet code review automation with GitHub Actions

Claude 3.5 Sonnet can be called from a GitHub Actions workflow using the Anthropic API. By passing the changed files as a prompt, the model returns a review comment that you post back to the pull request, creating an automated AI‑driven code review step in your CI pipeline.

By AI Decodes Editorial  ·  Published  ·  Last verified 29 November 2026

Diagram showing a GitHub Actions pipeline that calls Claude 3.5 Sonnet and posts a review comment.
A visual overview of the automated code‑review flow.

How do I set up the Anthropic API key for GitHub Actions?

  • Sign up for an Anthropic account and obtain a secret API key from the Anthropic API page.
  • In your GitHub repo, go to Settings → Secrets and variables → Actions and add a new secret named ANTHROPIC_API_KEY.
  • The key will be accessed in the workflow as ${{ secrets.ANTHROPIC_API_KEY }}.
  • Use the standard Authorization: Bearer <key> header as described in the MDN Authorization header documentation.

What does the GitHub Actions workflow file look like?

Create a file .github/workflows/claude-review.yml with the following content: yaml name: Claude 3.5 Sonnet Code Review on: pull_request: types: [opened, synchronize]

jobs: ai-review: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 with: fetch-depth: 0

- name: Get changed files id: changes run: | echo "files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | paste -sd ',' -)" >> $GITHUB_OUTPUT

- name: Call Claude 3.5 Sonnet id: review env: ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }} run: | payload=$(jq -n \ --arg files "${{ steps.changes.outputs.files }}" \ --arg prompt "Review the following changed files and point out potential bugs, style issues, and missed tests. Files: $files" \ '{model: "claude-3-5-sonnet-20240620", max_tokens: 1024, temperature: 0, messages: [{role: "user", content: $prompt}]}' ) response=$(curl -s https://api.anthropic.com/v1/messages \ -H "x-api-key: $ANTHROPIC_API_KEY" \ -H "Content-Type: application/json" \ -d "$payload") echo "review=$(echo $response | jq -r '.content[0].text')" >> $GITHUB_OUTPUT

- name: Post review comment uses: actions/github-script@v6 with: script: | github.rest.issues.createComment({ owner: context.repo.owner, repo: context.repo.repo, issue_number: context.payload.pull_request.number, body: 🤖 Claude 3.5 Sonnet Review\n\n${{ steps.review.outputs.review }} })

This workflow triggers on pull‑request events, gathers changed files, sends them to Claude 3.5 Sonnet, and posts the model’s feedback as a PR comment.

*Key points from the model’s documentation:* Claude 3.5 Sonnet supports a 200K token context window, allowing you to include multiple files without hitting limits (S1). It runs at twice the speed of Claude 3 Opus, keeping CI times low (S1).

Terminal view of a curl command sending a request to the Anthropic API.
Example API call used in the workflow.

What best‑practice tips improve the review quality?

  • Limit context: Even with a 200K token window, sending only the diff and any error output yields clearer suggestions and lower cost (S1, S6).
  • Use a low temperature (e.g., 0) to get deterministic, factual feedback suitable for automated gating (S5 mentions key parameters).
  • Split large PRs: If the file list exceeds a reasonable size, run the workflow in parallel jobs, each handling a subset of files.
  • Cache stable files: Store long‑standing library files in a separate artifact and only send them when they change, reducing token usage.
  • Validate output: Run unit tests after applying any suggested changes before merging; the model’s code suggestions are not guaranteed to compile.
  • Leverage Claude’s “Artifacts” feature: When using Claude on Claude.ai, generated code appears as an editable artifact, making manual review easier (S1).
  • Monitor cost: With pricing of $3 per million input tokens and $15 per million output tokens, keep prompts concise (S1, S6).

What to do if it does not work

  1. Check the secret – Verify that ANTHROPIC_API_KEY is correctly stored and not expired.
  2. Confirm the endpoint – The API URL must be exactly https://api.anthropic.com/v1/messages as listed in Anthropic’s docs (S1).
  3. Validate the Authorization header – It should be x-api-key: <your‑key> or Authorization: Bearer <your‑key> according to the HTTP Authorization header spec (S3).
  4. Inspect the response – Use curl -v or add -d logging to see error messages; common issues are 401 Unauthorized (bad key) or 429 Too Many Requests (rate‑limit).
  5. Review token limits – Ensure the payload does not exceed the 200K token context window; trim file list if needed.
  6. Run the workflow locally – Use act to simulate GitHub Actions and see step‑by‑step output.
  7. Consult the Anthropic API tutorial for authentication and first‑call examples (S5).

Menus and names can change as products update. If a step looks different, check the product's own help page.

Frequently asked questions

Can Claude 3.5 Sonnet replace traditional linters?
It can surface style issues and potential bugs, but it does not enforce rule‑based checks; keep existing linters for deterministic enforcement.

Is the model still available for new projects?
Claude 3.5 Sonnet is offered via Anthropic’s API, Bedrock, and Vertex AI, with the pricing shown in the announcement (S1).

How does the cost compare to other AI reviewers?
At $3 per million input tokens and $15 per million output tokens, it is cheaper than many larger models while delivering higher speed (S1).

Do I need to install any special GitHub Action?
The workflow uses built‑in actions (checkout, github-script) and a plain curl call; no extra action is required.