KI-Pipeline für Code-Reviews: So kombinieren Sie Claude und GitHub Actions
Viele Teams leiten komplette git diffs bei jedem Pull Request an teure Reasoning-Modelle wie Claude 3.5 Sonnet weiter und rennen dabei in hohe Token-Kosten. Ein smarterer Pipeline-Aufbau mit zwei Modellen vermeidet das.
Die meisten Teams, die versuchen, LLMs in ihren GitHub-Workflow zu integrieren, machen denselben teuren Fehler: Sie leiten komplette rohe git-Diffs bei jedem pull_request-Trigger direkt in hochwertige Reasoning-Modelle wie Claude 3.5 Sonnet.
Um eine zuverlässige, produktionsreife KI-Review-Engine zu bauen, benötigen Sie eine Tiered Dual-Model Routing Architecture in Kombination mit deterministischem CI-Preprocessing.
So entwerfen und implementieren Sie dieses Muster in GitHub Actions.
Anstatt jeden Pull Request identisch zu behandeln, teilen Sie die Aufgaben zwischen zwei spezialisierten Modellen auf:
Indem Sie Sonnet hinter Haikus Klassifizierungsdurchlauf schalten, senken Sie den Token-Verbrauch bei typischer Repository-Aktivität um bis zu 60 Prozent.
Senden Sie niemals rohe Ausgaben von git diff origin/main...HEAD direkt an ein LLM. Rohe Diffs enthalten minifizierte Assets, Lockfile-Updates und Stördaten, die Context Windows verbrauchen, ohne Signal zu liefern.
Fügen Sie in Ihrem Workflow einen Preprocessing-Schritt hinzu, der nicht prüfbare Dateien ignoriert:
# 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 Enter fullscreen mode Exit fullscreen mode 3. Production Workflow: Event Gate & Action Runner Hier ist ein minimaler, robuster GitHub-Actions-Schritt, der Schutzmaßnahmen umsetzt: die Verhinderung von Selbstauslöse-Schleifen durch Bot-Accounts, die Sicherstellung, dass der Pull Request Code geändert hat, und der sichere Aufruf der Anthropic-API.
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.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") ' Enter fullscreen mode Exit fullscreen mode Scaling Up to Full Repository Autonomy PR-Reviews sind nur eine Ebene der Repository-Orchestrierung.
Sobald Ihr Diff-Filtering und Ihr Modell-Tiering stehen, können Sie diese identische Infrastruktur auf Folgendes anwenden:
Wenn Sie vollständige, sofort einsatzbereite GitHub-Actions-Workflows, robuste Prompt-Vorlagen, Skripte zur Kostenüberwachung und sicherheitsgehärtete Konfigurationen möchten, werfen Sie einen Blick auf The Claude & GitHub Actions Automation Playbook (22 Dollar).
Es enthält produktionsgetestete Konfigurationen für automatisierte PR-Reviews, selbstheilende CI-Pipelines, Issue-Triage-Bots und strenge Token-Budget-Begrenzer, sodass Sie Ihre Repository-Operationen automatisieren können, ohne Budget oder Sicherheit zu riskieren.
Automatisch übersetzt. Das Original auf Englisch ist unten aufklappbar.
Original (Englisch) anzeigen
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.
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.
Instead of treating every pull request identically, split responsibilities between two specialized models:
By gating Sonnet behind Haiku's classification pass, you slash token consumption by up to 60% across standard repository activity.
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 Enter fullscreen mode Exit fullscreen mode 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.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") ' Enter fullscreen mode Exit fullscreen mode 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:
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.