How to run Claude Code in headless mode for automation, scripting, and CI/CD integration. Use when user asks about non-interactive mode, automation, scripting, or programmatic usage.
Executes Claude Code non-interactively for automation, scripting, and CI/CD workflows.
/plugin marketplace add reggiechan74/claude-plugins/plugin install claude-code-metaskill@claude-pluginsThis skill inherits all available tools. When active, it can use any tool Claude has access to.
Headless mode enables running Claude Code programmatically via command line without an interactive UI, making it suitable for automation, scripts, and integration with other tools.
The primary interface is the claude command with the --print (or -p) flag for non-interactive execution:
claude -p "Stage my changes and write commits" \
--allowedTools "Bash,Read" \
--permission-mode acceptEdits
| Flag | Purpose | Example |
|---|---|---|
--print, -p | Run non-interactively | claude -p "query" |
--output-format | Set output type (text, json, stream-json) | claude -p --output-format json |
--resume, -r | Resume by session ID | claude --resume abc123 |
--continue, -c | Continue recent conversation | claude --continue |
--allowedTools | Specify permitted tools | claude --allowedTools "Bash,Read" |
--mcp-config | Load MCP servers from JSON | claude --mcp-config servers.json |
Returns plain text response.
Provides structured data with metadata:
{
"type": "result",
"subtype": "success",
"total_cost_usd": 0.003,
"duration_ms": 1234,
"num_turns": 6,
"result": "Response text...",
"session_id": "abc123"
}
Emits messages as received in JSONL format, useful for real-time processing.
Direct arguments or stdin:
echo "Explain this code" | claude -p
Multiple conversation turns via stdin using JSONL format with -p, --output-format stream-json, and --input-format stream-json.
Resume existing sessions:
claude --continue "Refactor for performance"
claude --resume 550e8400-e29b-41d4-a716-446655440000 "Fix linting issues"
Diagnose issues with severity assessment using allowed tools and MCP configuration for monitoring integrations.
Audit pull request diffs for vulnerabilities and compliance issues using Read and WebSearch tools.
Multi-step document review maintaining session context across separate requests.
jq for data extraction#!/bin/bash
# Review PR changes in headless mode
RESULT=$(claude -p "Review changes in git diff for security issues" \
--allowedTools "Bash,Read,Grep" \
--output-format json \
--permission-mode acceptAll)
# Parse result
echo "$RESULT" | jq -r '.result'
# Check exit code
if [ $? -ne 0 ]; then
echo "Review failed"
exit 1
fi
# In .gitlab-ci.yml or GitHub Actions
claude -p "Run tests and fix any failures" \
--allowedTools "Bash,Read,Edit,Write" \
--max-turns 10 \
--output-format json
Check exit codes and parse error messages:
if ! claude -p "task" 2>error.log; then
echo "Error: $(cat error.log)"
exit 1
fi
Save session IDs for continuation:
# First run
RESULT=$(claude -p "Start analysis" --output-format json)
SESSION_ID=$(echo "$RESULT" | jq -r '.session_id')
# Continue later
claude --resume "$SESSION_ID" "Continue analysis"
This skill should be used when the user asks about libraries, frameworks, API references, or needs code examples. Activates for setup questions, code generation involving libraries, or mentions of specific frameworks like React, Vue, Next.js, Prisma, Supabase, etc.