How to run Claude Code headless in CI for QA checks
Run Claude Code with claude -p in CI to flag untested changes on every pull request, with narrow tool access, turn limits and JSON your pipeline can read.
You want an AI agent to look at every pull request and say what is untested. Nobody is at the keyboard in CI, so the agent cannot stop and ask for permission, and a paragraph of prose in a log file helps no one.
The answer is Claude Code's headless mode. You run it with -p, hand it a short list of tools it may use, cap how long it can run, and ask for JSON that matches a schema. Your pipeline then reads a field instead of parsing English.
This guide shows the command, the guardrails that matter, and what good output looks like.
What headless mode is
Headless mode is Claude Code without the interactive session. The headless docs put it simply: pass -p with your prompt and the CLI options you need. Claude does the work, prints the result and exits.
Two details make it fit for CI. Claude Code exits with code 0 on success and a non-zero code when the run fails, so a broken run fails the job instead of passing quietly. And it reads standard input, so you can pipe a diff or a build log straight into the prompt.
The docs also recommend --bare for scripted calls. Bare mode skips auto-discovery of hooks, skills, custom commands, subagents, plugins, MCP servers and CLAUDE.md, so a scripted call starts faster and behaves the same on every machine. The trade is that bare mode does not use your subscription login, so you set ANTHROPIC_API_KEY in the environment, and you pass project context in yourself.
How to run Claude Code headless in CI
Start with two small files in the repo. The first is the context the agent needs about your product, for example qa/context.md: where tests live, which framework you use, which areas are fragile, what you do not want flagged. Because bare mode does not read CLAUDE.md, this file is how the agent learns your conventions.
The second is the shape of the answer you want, qa/schema.json:
{
"type": "object",
"properties": {
"risk": { "type": "string", "enum": ["low", "medium", "high"] },
"untested_changes": { "type": "array", "items": { "type": "string" } },
"suggested_tests": { "type": "array", "items": { "type": "string" } }
},
"required": ["risk", "untested_changes", "suggested_tests"]
}Then the command. It pipes the pull request diff in, appends your context file to the system prompt, allows reading files and two read-only git commands, and caps both turns and spend:
gh pr diff "$PR_NUMBER" | claude --bare -p \
"List the changed behavior in this diff that no existing test covers, and suggest one test for each." \
--append-system-prompt-file qa/context.md \
--allowedTools "Read,Bash(git log *),Bash(git diff *)" \
--max-turns 10 \
--max-budget-usd 2 \
--output-format json \
--json-schema "$(cat qa/schema.json)" \
> qa-report.jsonEvery flag there is in the CLI reference. The pattern of piping a pull request diff into claude -p with an appended system prompt comes from the headless docs themselves.
--json-schema gets you validated JSON matching your schema after the agent finishes, and it lands in the structured_output field of the response. The docs are explicit that your script should check that field is present before using it, so make that check the step that decides whether the job passes. --output-format json also carries the session ID and a total_cost_usd figure, which is the honest way to answer "what does this cost per pull request" after a week of real runs.
Doing the same thing with the GitHub Action
If you are on GitHub, the Claude Code GitHub Action wraps the same CLI, and /install-github-app inside Claude Code sets it up. It saves your credential as a repository secret: ANTHROPIC_API_KEY for an API key, or CLAUDE_CODE_OAUTH_TOKEN for a subscription token.
Two of its inputs do the work. prompt holds the instructions for an automated run, which is what you want here, rather than waiting for someone to type a trigger phrase in a comment. claude_args passes CLI arguments straight through, so --max-turns, --model and --allowedTools all work exactly as above. The docs name --max-turns in claude_args and workflow-level timeouts as the two cost controls.
The job also needs permissions. The docs' own review example grants read access to contents, pull requests and issues, plus id-token: write, which the action needs for its default GitHub App authentication. Add write scopes only when you actually want the agent to change something, and read the action's security notes before you do. On keys the docs leave no room: "Never commit API keys or OAuth tokens directly to your repository."
One scheduling note if you later move this to a cron job rather than a pull request trigger: GitHub runs scheduled workflows only from the default branch, and in public repositories it disables the schedule after 60 days without repository activity.
Guardrails that matter for a QA agent
A QA agent in CI runs on every push, so a sloppy setup repeats its mistake hundreds of times. Set these once.
- List tools narrowly.
--allowedToolsdecides what runs without a permission prompt, and it takes patterns likeBash(git diff *). The docs point out that the space before the asterisk matters, becauseBash(git diff*)would also matchgit diff-index. - Cap turns and spend.
--max-turnsis print-mode only and exits with an error when the limit is reached.--max-budget-usdstops the run once estimated spend on API calls reaches your number. Neither is set for you: turns have no limit by default. - Start read-only. A report that names missing tests is easy to review and safe to ignore. An agent that writes tests into your branch is a later step, once you trust its reports.
- Expect the CI gap. If the agent does eventually commit tests back, note that GitHub does not trigger workflows on commits made with the default token. Better to know that now than to wonder why the new tests never ran.
What good output looks like
A good report is specific enough that an engineer could write the test without reopening the diff:
{
"risk": "medium",
"untested_changes": [
"applyCoupon() now rejects codes after their expiry date; no test covers an expired code",
"Checkout totals round to two decimals in the new formatPrice(); existing tests only use whole numbers"
],
"suggested_tests": [
"checkout.test.ts: a coupon applied one day after expiry shows 'Coupon expired' and keeps the full price",
"price.test.ts: formatPrice(19.9) returns '19.90' rather than '19.9'"
]
}Each item names a function, the input that matters and the expected result. Compare that with the output you should throw away: "consider adding tests for edge cases", or a suggested test for code the diff never touched.
The first few weeks will contain misses. When the agent flags something that is already covered, or walks past a real gap, the fix is almost always the context file or the prompt rather than the model. Keep a list of the misses and edit qa/context.md each time. That loop matters more than any flag above, because a green job is a signal and not a verdict, which is the argument in more automated tests don't always mean better testing. It is also why a check like this belongs on the pull request, with the caveats in is finding bugs earlier really cheaper?
Where GetQA fits
The command is the easy part. The work is the context file, the schema, and the weekly pass over what the agent got wrong. GetQA sets up AI QA agents in your repo, tracker and CI, then keeps reviewing and tuning them as your product grows. You can see what a setup produces on our deliverables page.
