Working Code
Use the claude -p flag for non-interactive (headless) mode:
claude -p "Analyze the architecture of this project"
The result is printed and the process exits. No interactive session is opened, making it ideal for scripts.
You can also pipe input:
cat error.log | claude -p "Analyze this error log"
For JSON output:
claude -p --output-format json "Analyze the dependencies in package.json"
You get structured JSON that other tools can parse.
Try It Yourself
Build a daily automation script:
#!/bin/bash
# daily-check.sh — Morning project health check script
echo "=== Code Quality Check ==="
claude -p "Find all TODO, FIXME, and HACK comments in src/ and prioritize them"
echo ""
echo "=== Security Check ==="
claude -p "Check if any dependencies in package.json have security vulnerabilities"
echo ""
echo "=== Test Status ==="
claude -p "Run npm test and analyze the cause if anything fails"
Register this script in cron or CI to run automated daily checks.
"Why?" — Beyond Conversations, Into Systems
So far you've used Claude Code interactively in the terminal. Headless mode turns Claude Code into a CLI tool. Use it as one step in a pipeline, just like grep, jq, or curl.
Key Flags
| Flag | Function |
| ----------------------------- | --------------------------------------------------- |
| -p "prompt" | Non-interactive execution (prints result and exits) |
| --output-format json | JSON format output |
| --output-format stream-json | Streaming JSON output |
| --max-turns N | Limit maximum number of turns |
Pipeline Usage Patterns
# Analyze error logs
cat error.log | claude -p "Find the root cause of this error"
# Review a PR diff
gh pr diff 42 | claude -p "Review these changes as a senior developer"
# Code conversion
cat legacy.js | claude -p "Convert this code to TypeScript" > modern.ts
# Generate release notes
git log --oneline v1.0..HEAD | claude -p "Write release notes from these commits"
Claude Code SDK
Call it programmatically from TypeScript/JavaScript:
import { claude } from "@anthropic-ai/claude-code";
const result = await claude({
prompt: "Analyze security vulnerabilities in this file",
workingDirectory: "./my-project",
});
console.log(result.stdout);
With the SDK you can:
- Build custom automation tools.
- Integrate Claude Code into CI/CD pipelines.
- Automate code analysis in monitoring systems.
CI/CD Integration Example
# .github/workflows/ai-review.yml
name: AI Code Review
on:
pull_request:
types: [opened, synchronize]
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install Claude Code
run: npm install -g @anthropic-ai/claude-code
- name: Review PR
run: |
gh pr diff ${{ github.event.number }} | \
claude -p "Review this PR for bugs, security, and performance." \
> review.md
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
- name: Post Review Comment
run: gh pr comment ${{ github.event.number }} --body-file review.md
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Deep Dive
What about costs in non-interactive mode?
claude -p consumes tokens just like a normal session. If you run it on every PR in CI/CD, costs can add up:
- Always set a budget limit for API usage.
- Use Sonnet for simple tasks; reserve Opus for complex analysis.
- Use
--max-turnsto limit execution.
What's the difference between stdout and stderr?
Output from claude -p:
- stdout: Claude's final response (can be piped)
- stderr: Progress updates, tool usage logs, etc. (displayed in terminal)
In scripts, capture only stdout for clean results:
result=$(claude -p "Analyze this" 2>/dev/null)
- Run
claude -pto extract a TODO list from your project. - Use
git log | claude -pto auto-generate release notes from recent commits. - Build a daily check script that runs 3 or more automated checks.
Q1. What mode do you use to run Claude Code in a CI/CD pipeline?
- A) Plan mode (
Shift+Tab) - B) Interactive mode (
claude) - C) Non-interactive mode (
claude -p) - D) Worktree mode (
claude -w)