Most teams attempting to integrate LLMs into their GitHub workflow make the same expensive mistake: they pipe entire raw git diffs straight into high-end reasoning models like Claude 3.5 Sonnet on every pull_request trigger.
Within a week, three problems emerge:
- Runaway token bills: Hundreds of dollars spent parsing generated bundle files, lockfile churn, and SVG changes.
- Security vulnerabilities: Running AI triage on unvetted fork pull requests without strict secret isolation.
- Bot feedback loops: The automated reviewer triggers another webhook, triggering another review, exhausting rate limits.
To build a dependable, production-ready AI review engine, you need a Tiered Dual-Model Routing Architecture paired with deterministic CI pre-processing.
Here is how to design and implement this pattern in GitHub Actions.
1. Dual-Model Architecture: Haiku Triage, Sonnet Synthesis
Instead of treating every pull request identically, split responsibilities between two specialized models:
- Claude 3.5 Haiku (Fast Classifier): Evaluates PR complexity, determines if review is necessary, categorizes touched subsystems (e.g., frontend, auth, database migration), and filters out trivial changes (docs, typo fixes).
- Claude 3.5 Sonnet (Deep Reasoning): Executes the actual code review, audits logic boundaries, verifies test coverage, and writes actionable inline suggestions.
By gating Sonnet behind Haiku's classification pass, you slash token consumption by up to 60% across standard repository activity.
2. Sanitize and Filter the Diff Before Calling the API
Never send raw git diff origin/main...HEAD outputs directly to an LLM. Raw diffs contain minified assets, lockfile updates, and noise that eat context windows without adding signal.
Add a pre-processing step in your workflow that ignores non-reviewable files:
# Extract only code-relevant diffs under 500 lines total git diff origin/${{ github.base_ref }}...HEAD \ -- . \ ':(exclude)*.lock' \ ':(exclude)*-lock.json' \ ':(exclude)dist/*' \ ':(exclude)*.min.js' \ ':(exclude)*.svg' > filtered_diff.txt # If diff is empty or too large, fail-safe or exit early DIFF_LINES=$(wc -l < filtered_diff.txt) if [ "$DIFF_LINES" -eq 0 ] || [ "$DIFF_LINES" -gt 2500 ]; then echo "Diff out of automated review bounds ($DIFF_LINES lines). Skipping." exit 0 fi 3. Production Workflow: Event Gate & Action Runner
Here is a minimal, robust GitHub Actions step that implements guardrails: preventing self-trigger loops from bot accounts, ensuring the pull request has changed code, and calling the Anthropic API securely.
name: Claude PR Reviewer on: pull_request: types: [opened, synchronize] paths-ignore: - 'docs/**' - '**.md' jobs: review: # Prevent review loops on bot PRs and untrusted public forks if: github.actor != 'dependabot[bot]' && github.event.pull_request.head.repo.full_name == github.repository runs-on: ubuntu-latest permissions: contents: read pull-requests: write steps: - uses: actions/checkout@v4 with: fetch-depth: 0 - name: Extract Sanitized Diff id: diff run: | git diff origin/${{ github.base_ref }}...HEAD \ -- ':(exclude)*.lock' ':(exclude)*-lock.json' > pr_diff.txt echo "diff_present=$( [ -s pr_diff.txt ] && echo 'true' || echo 'false' )" >> $GITHUB_OUTPUT - name: Run Review with Claude if: steps.diff.outputs.diff_present == 'true' env: ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} PR_NUMBER: ${{ github.event.pull_request.number }} run: | python3 -c ' import os, anthropic client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"]) with open("pr_diff.txt", "r") as f: diff = f.read()[:15000] # Hard guardrail limit prompt = f"""Review this PR diff for critical bugs, edge cases, and security vulnerabilities only. Keep feedback concise and actionable. Diff:\n{diff}""" message = client.messages.create( model="claude-3-5-sonnet-20241022", max_tokens=1000, messages=[{"role": "user", "content": prompt}] ) # Write comment via GitHub CLI review_body = message.content[0].text with open("review.md", "w") as rf: rf.write(f"### Claude Automated Review\n\n{review_body}") os.system(f"gh pr comment {os.environ[\"PR_NUMBER\"]} --body-file review.md") ' Scaling Up to Full Repository Autonomy
PR review is just one layer of repository orchestration. Once your diff filtering and model tiering are in place, you can apply this identical infrastructure to:
- Automated Issue Triage: Using Claude 3.5 Haiku to label incoming bug reports, detect missing reproduction steps, and request clarifications automatically.
- Autonomous Bugfix Branches: Using Sonnet to reproduce failures from test logs, apply patches, and open ready-to-merge PRs.
- Changelog & Release Notes: Synthesizing conventional commits into production changelogs on deployment tags.
If you want full, copy-paste-ready GitHub Actions workflows, robust prompt templates, cost-monitoring scripts, and security-hardened configurations, check out The Claude & GitHub Actions Automation Playbook ($22).
It contains production-tested configs for automated PR reviews, self-healing CI pipelines, issue triage bots, and strict token-budget limiters so you can automate your repository operations without risking your budget or security.
