Headless mode: scripts and CI
Claude Code does not have to be interactive. With the -p flag it takes one prompt, prints the answer and exits, so you can use it in scripts, Git hooks and CI jobs.
Run one prompt#
claude -p "summarize what changed in the last 5 commits"Pipe things in#
Send a file or command output straight to Claude:
cat error.log | claude -p "explain the root cause of this error"
git diff | claude -p "review this diff for bugs"Get structured output#
Ask for JSON when another program will read the result:
claude -p "list the TODO comments in src/" --output-format jsonLimit what it can do#
Unattended runs should be tightly restricted. Allow only the tools the job needs:
claude -p "run the tests and summarize failures" \
--allowedTools "Bash(npm test:*)" "Read"Use it in CI#
- Store your API key as a secret in your CI provider. Never write it in the script.
- Keep jobs small and single-purpose, such as a review comment or a changelog draft.
- Anthropic also publishes a GitHub Action for Claude Code. Read its README for the current setup.