Black Friday sale: 85 Claude Code agents for $29 one-time · lifetime updatesGet access →

Headless mode: scripts and CI

Updated · 4 min read

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#

terminal
claude -p "summarize what changed in the last 5 commits"

Pipe things in#

Send a file or command output straight to Claude:

terminal
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:

terminal
claude -p "list the TODO comments in src/" --output-format json

Limit what it can do#

Unattended runs should be tightly restricted. Allow only the tools the job needs:

terminal
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.

Keep reading