I mostly used Claude Code interactively. I’d open a terminal, type out my thoughts, and watch it work. It felt productive.
Then I noticed something annoying. Half my prompts were the same thing every single PR. “Look at this diff.” “Anything broken?” “Double-check the logic.”
That’s not a conversation. That’s a job. And jobs belong in CI.
So I moved it there.
Running Claude Code headless is surprisingly simple. You don’t need a complex framework or a server. Just the CLI, a pipe, and a bit of discipline. Here is how I set it up to stop wasting my brain on repetitive code review tasks.
The basic command
Headless mode uses the -p flag. It runs once, prints the answer, and exits. No interactive prompt. No waiting for you to hit enter.
Here is the simplest version:
claude -p "Summarize what changed in this repo since the last tag"
It works exactly like a standard Unix command. You can pipe data into it. This is where it gets interesting for code review.
git diff main | claude -p "Review this diff for bugs. Be terse."
That’s it. It takes the diff, reads it, generates the review, and prints it to standard out. You can redirect that output to a file, or pipe it into another command. I use this pattern constantly because it removes the friction of “opening an IDE, loading context, and thinking about what to ask.”
Parsing the output
If you’re writing scripts, you don’t want markdown in your logs. You want data. Claude Code supports structured output via --output-format json.
The response is a JSON object with a result field containing the text. You can extract it cleanly with jq.
claude -p "Review this diff" --output-format json | jq -r '.result'
This makes it trivial to parse the review in a shell script. You can check if the output contains specific keywords, count lines, or fail the build if the model finds critical issues. I usually just dump the text into a file for now, but knowing you can parse it keeps the door open for more automated logic later.
Locking down the tools
This is the part that matters most in CI. In interactive mode, you can see what it’s doing. In headless mode, if it goes off the rails, you might not notice until the bill arrives or the timeout hits.
You control which tools Claude can use with --allowedTools. For a code review job, I want it to read files and search for context. I don’t want it running arbitrary bash commands or writing new files.
For a read-only reviewer:
claude -p "Review this diff" --allowedTools "Read,Grep,Glob"
That’s safe. It can look at the code, search for patterns, and list files. It can’t run npm test or rm -rf ..
If you want it to run tests, be specific. Don’t give it all of Bash. Give it only the specific command you need.
claude -p "Run the tests and report failures" --allowedTools "Bash(npm test:*)"
The :* suffix means prefix match. It allows any command that starts with npm test, like npm test itself or npm test -- --watch=false. It’s much tighter than just saying Bash.
There is also --max-turns. This limits how many steps the model can take. If it gets stuck in a loop, it stops after N turns. I set this to 5 for reviews. If it needs more than 5 steps to review a diff, something is probably wrong with the prompt or the code complexity.
claude -p "Review" --allowedTools "Read,Grep,Glob" --max-turns 5
This combination of restricted tools and limited turns keeps the cost predictable and the behavior sane.
GitHub Actions setup
Here is the actual job I use. It’s a standard GitHub Actions workflow file.
First, you need to install the CLI. The package is @anthropic-ai/claude-code.
Then, you generate the review. I pipe the diff into Claude. I restrict the tools to read-only operations. I limit the turns. I save the output to a markdown file.
Without full history, git diff origin/main...HEAD fails because it can’t find the merge base. So we fetch everything.
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Run Code Review
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
npm install -g @anthropic-ai/claude-code
git diff origin/main...HEAD | claude -p "Review this diff for bugs and security issues. Be terse." --allowedTools "Read,Grep,Glob" --max-turns 5 > review.md
Then, you post the result to the PR. I use the gh CLI because it’s already in the GitHub Actions runner environment.
The GITHUB_TOKEN needs permission to comment on the PR. Add this to your job level:
permissions:
pull-requests: write
contents: read
- name: Post Review
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh pr comment ${{ github.event.pull_request.number }} --body-file review.md
That’s the whole thing. No custom Docker image. No complex setup script. Just a few lines of YAML.
I’ve seen people use the official anthropics/claude-code-action. That’s nice if you want @claude mentions in PRs. It’s more interactive. But I prefer the plain CLI version because I can see exactly what commands are running. No magic. No hidden steps. If it fails, I know which line failed.
Debugging test failures
The second use case is my favorite. When tests fail in CI, I don’t want to read 400 lines of stack traces. I want an explanation.
I run Claude with the test output as context.
npm test 2>&1 | claude -p "Here is the test output. Explain the likely cause of the failure in one paragraph." --allowedTools "Read" --max-turns 3
It doesn’t fix the code. It doesn’t try to be a senior engineer. It just reads the error, looks at the relevant files (if I let it), and gives me a human-readable summary.
It saves me time. Instead of squinting at logs, I get a sentence like “The test fails because user.id is undefined in the mock data.” That’s enough for me to find the bug.
Mistakes I made
I gave it full Bash access on the first try. Don’t do this. In CI, you don’t have an eye on it. If it decides to run npm install or node server.js, you’re waiting forever or burning tokens. Start with Read, Grep, Glob. Add Bash only when you need specific commands.
I also wrote vague prompts at first. “Review this code.” The result was a wall of nitpicks. Formatting issues. Variable names. Things I didn’t care about.
I fixed it by being specific in the prompt:
“Review this diff for bugs and security issues only. Ignore style. Max 5 points. If nothing is wrong, say ‘LGTM’.”
That changed everything. The output became useful. It stopped trying to be a linter and started being a reviewer.
The boring work is the best work
The interactive mode gets the attention. It feels like hacking. It feels smart. But the headless runs are where it saves me time every single day.
I don’t have to think about “should I review this PR?” anymore. The bot does it. I just read the comment. If it says “LGTM,” I skim the diff myself and merge. The bot catches what I’d miss, but it doesn’t replace reading the code. If it points out a null pointer, I fix it. It’s faster. It’s consistent. And it never gets tired of checking for undefined checks.
You don’t need a complex AI pipeline. You need a CLI command and a pipe. That’s enough.