Claude auto PR review
Copied from the official Solutions Guide basic example (prompt shortened).
pull_request: opened, synchronize
Est. cost per run~$0.18Checked Oct 16, 2026
Not executedJust shippedOur pick for the best security, implementation, and planning skills.Best skills for each job.See the picks →
When CI fails, Claude uses structured output to decide whether the failure looks flaky. Flaky runs are retried automatically; real failures are left for a human.
workflow_run: CI completed (failure)
How it works
Setup
.github/workflows/claude-code-action-test-failure-analysis.ymlANTHROPIC_API_KEY. The file reads it as secrets.NAME. Do not paste the key into the YAML.gh with github.token use the token Actions already provides. You do not create that secret.Let your agent do it
Paste it into your coding agent in this repo. It does the setup and asks before anything leaves your machine.
Runs in GitHub Actions on your Claude Pro or Max plan through an OAuth token from claude setup-token. No API credits.
Set up the "Auto-retry flaky tests: Claude classifies CI failures and retries flaky ones" GitHub workflow from AgentFlows (https://agentflows.sh/flows/claude-code-action-test-failure-analysis) so it runs on my Claude Pro or Max subscription instead of API credits.
Install these agent skills first. Download each SKILL.md from its source (respect its license) into .claude/skills/<name>/SKILL.md, or wherever this agent loads skills from:
- Debug: https://github.com/anthropics/knowledge-work-plugins/blob/main/engineering/skills/debug/SKILL.md (Apache-2.0), notes at https://agentflows.sh/skills/debug
- Testing strategy: https://github.com/anthropics/knowledge-work-plugins/blob/main/engineering/skills/testing-strategy/SKILL.md (Apache-2.0), notes at https://agentflows.sh/skills/testing-strategy
Save this file as .github/workflows/claude-code-action-test-failure-analysis.yml:
```yaml
name: Auto-Retry Flaky Tests
# This example demonstrates using structured outputs to detect flaky test failures
# and automatically retry them, reducing noise from intermittent failures.
#
# Use case: When CI fails, automatically determine if it's likely flaky and retry if so.
on:
workflow_run:
workflows: ["CI"]
types: [completed]
permissions:
contents: read
actions: write
jobs:
detect-flaky:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Detect flaky test failures
id: detect
uses: anthropics/claude-code-action@main
with:
claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
prompt: |
The CI workflow failed: ${{ github.event.workflow_run.html_url }}
Check the logs: gh run view ${{ github.event.workflow_run.id }} --log-failed
Determine if this looks like a flaky test failure by checking for:
- Timeout errors
- Race conditions
- Network errors
- "Expected X but got Y" intermittent failures
- Tests that passed in previous commits
Return:
- is_flaky: true if likely flaky, false if real bug
- confidence: number 0-1 indicating confidence level
- summary: brief one-sentence explanation
claude_args: |
--json-schema '{"type":"object","properties":{"is_flaky":{"type":"boolean","description":"Whether this appears to be a flaky test failure"},"confidence":{"type":"number","minimum":0,"maximum":1,"description":"Confidence level in the determination"},"summary":{"type":"string","description":"One-sentence explanation of the failure"}},"required":["is_flaky","confidence","summary"]}'
# Auto-retry only if flaky AND high confidence (>= 0.7)
- name: Retry flaky tests
if: |
fromJSON(steps.detect.outputs.structured_output).is_flaky == true &&
fromJSON(steps.detect.outputs.structured_output).confidence >= 0.7
env:
GH_TOKEN: ${{ github.token }}
WORKFLOW_NAME: ${{ github.event.workflow_run.name }}
HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
run: |
OUTPUT='${{ steps.detect.outputs.structured_output }}'
CONFIDENCE=$(echo "$OUTPUT" | jq -r '.confidence')
SUMMARY=$(echo "$OUTPUT" | jq -r '.summary')
echo "🔄 Flaky test detected (confidence: $CONFIDENCE)"
echo "Summary: $SUMMARY"
echo ""
echo "Triggering automatic retry..."
gh workflow run "$WORKFLOW_NAME" --ref "$HEAD_BRANCH"
# Low confidence flaky detection - skip retry
- name: Low confidence detection
if: |
fromJSON(steps.detect.outputs.structured_output).is_flaky == true &&
fromJSON(steps.detect.outputs.structured_output).confidence < 0.7
run: |
OUTPUT='${{ steps.detect.outputs.structured_output }}'
CONFIDENCE=$(echo "$OUTPUT" | jq -r '.confidence')
echo "⚠️ Possible flaky test but confidence too low ($CONFIDENCE)"
echo "Not retrying automatically - manual review recommended"
# Comment on PR if this was a PR build
- name: Comment on PR
if: github.event.workflow_run.event == 'pull_request'
env:
GH_TOKEN: ${{ github.token }}
HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
run: |
OUTPUT='${{ steps.detect.outputs.structured_output }}'
IS_FLAKY=$(echo "$OUTPUT" | jq -r '.is_flaky')
CONFIDENCE=$(echo "$OUTPUT" | jq -r '.confidence')
SUMMARY=$(echo "$OUTPUT" | jq -r '.summary')
pr_number=$(gh pr list --head "$HEAD_BRANCH" --json number --jq '.[0].number')
if [ -n "$pr_number" ]; then
if [ "$IS_FLAKY" = "true" ]; then
TITLE="🔄 Flaky Test Detected"
ACTION="✅ Automatically retrying the workflow"
else
TITLE="❌ Test Failure"
ACTION="⚠️ This appears to be a real bug - manual intervention needed"
fi
gh pr comment "$pr_number" --body "$(cat <<EOF
## $TITLE
**Analysis**: $SUMMARY
**Confidence**: $CONFIDENCE
$ACTION
[View workflow run](${{ github.event.workflow_run.html_url }})
EOF
)"
fi
```
It authenticates with CLAUDE_CODE_OAUTH_TOKEN. Tell me to run `claude setup-token` in my own terminal to create it, then run `gh secret set CLAUDE_CODE_OAUTH_TOKEN` and let me paste it. Never write the token into a file.
It runs on: workflow_run: CI completed (failure). Runs count against my subscription's usage limits, and GitHub Actions minutes still apply.
Show me the diff, then commit on a new branch and open a pull request.The file
Copy the file as-is. The notes beside each block explain it.
.github/workflows/claude-code-action-test-failure-analysis.yml
Name
The name GitHub shows in the Actions tab. The file path is under Where to paste this.
name: Auto-Retry Flaky Tests# This example demonstrates using structured outputs to detect flaky test failures# and automatically retry them, reducing noise from intermittent failures.## Use case: When CI fails, automatically determine if it's likely flaky and retry if so.Trigger
GitHub starts a run when the workflow named CI finishes.
on: workflow_run: workflows: ["CI"] types: [completed]Permissions
This token may read the repo, and change workflow runs. Anything not listed is not granted.
permissions: contents: read actions: writeJobs
Each job is one runner. The steps inside it run top to bottom.
jobs: detect-flaky: runs-on: ubuntu-latest if: ${{ github.event.workflow_run.conclusion == 'failure' }}Steps
These steps run in order on that job.
steps: - name: Checkout repository uses: actions/checkout@v4 - name: Detect flaky test failures id: detectAgent
Runs Claude Code with anthropics/claude-code-action@main. Inputs: anthropic_api_key (the Claude API key secret); prompt (the task); claude_args (Claude Code flags, including allowed tools).
uses: anthropics/claude-code-action@main with: anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} prompt: | The CI workflow failed: ${{ github.event.workflow_run.html_url }} Check the logs: gh run view ${{ github.event.workflow_run.id }} --log-failed Determine if this looks like a flaky test failure by checking for: - Timeout errors - Race conditions - Network errors - "Expected X but got Y" intermittent failures - Tests that passed in previous commits Return: - is_flaky: true if likely flaky, false if real bug - confidence: number 0-1 indicating confidence level - summary: brief one-sentence explanation claude_args: | --json-schema '{"type":"object","properties":{"is_flaky":{"type":"boolean","description":"Whether this appears to be a flaky test failure"},"confidence":{"type":"number","minimum":0,"maximum":1,"description":"Confidence level in the determination"},"summary":{"type":"string","description":"One-sentence explanation of the failure"}},"required":["is_flaky","confidence","summary"]}' # Auto-retry only if flaky AND high confidence (>= 0.7) - name: Retry flaky tests if: | fromJSON(steps.detect.outputs.structured_output).is_flaky == true && fromJSON(steps.detect.outputs.structured_output).confidence >= 0.7 env: GH_TOKEN: ${{ github.token }} WORKFLOW_NAME: ${{ github.event.workflow_run.name }} HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }} run: | OUTPUT='${{ steps.detect.outputs.structured_output }}' CONFIDENCE=$(echo "$OUTPUT" | jq -r '.confidence') SUMMARY=$(echo "$OUTPUT" | jq -r '.summary') echo "🔄 Flaky test detected (confidence: $CONFIDENCE)" echo "Summary: $SUMMARY" echo "" echo "Triggering automatic retry..." gh workflow run "$WORKFLOW_NAME" --ref "$HEAD_BRANCH" # Low confidence flaky detection - skip retry - name: Low confidence detection if: | fromJSON(steps.detect.outputs.structured_output).is_flaky == true && fromJSON(steps.detect.outputs.structured_output).confidence < 0.7 run: | OUTPUT='${{ steps.detect.outputs.structured_output }}' CONFIDENCE=$(echo "$OUTPUT" | jq -r '.confidence') echo "⚠️ Possible flaky test but confidence too low ($CONFIDENCE)" echo "Not retrying automatically - manual review recommended" # Comment on PR if this was a PR build - name: Comment on PR if: github.event.workflow_run.event == 'pull_request' env: GH_TOKEN: ${{ github.token }} HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }} run: | OUTPUT='${{ steps.detect.outputs.structured_output }}' IS_FLAKY=$(echo "$OUTPUT" | jq -r '.is_flaky') CONFIDENCE=$(echo "$OUTPUT" | jq -r '.confidence') SUMMARY=$(echo "$OUTPUT" | jq -r '.summary') pr_number=$(gh pr list --head "$HEAD_BRANCH" --json number --jq '.[0].number') if [ -n "$pr_number" ]; then if [ "$IS_FLAKY" = "true" ]; then TITLE="🔄 Flaky Test Detected" ACTION="✅ Automatically retrying the workflow" else TITLE="❌ Test Failure" ACTION="⚠️ This appears to be a real bug - manual intervention needed" fi gh pr comment "$pr_number" --body "$(cat <<EOF ## $TITLE **Analysis**: $SUMMARY **Confidence**: $CONFIDENCE $ACTION [View workflow run](${{ github.event.workflow_run.html_url }}) EOF )" fiKeep going
Copied from the official Solutions Guide basic example (prompt shortened).
pull_request: opened, synchronize
Est. cost per run~$0.18Checked Oct 16, 2026
Not executed`track_progress: true` posts a checkbox tracking comment (per Solutions Guide).
pull_request: opened, synchronize, ready_for_review, reopened
Est. cost per run~$0.22Checked Oct 16, 2026
Not executedNo `prompt` → the action runs in tag/interactive mode and responds to the comment. `trigger_phrase` default is `@claude` (usage.md).
issue_comment / pull_request_review_comment containing @claude
Est. cost per run~$0.27Checked Oct 16, 2026
Not executed· 1 caveat