Get the FREE Ultimate OpenClaw Setup Guide →

claude-code-headless

Scanned
npx machina-cli add skill aiskillstore/marketplace/claude-code-headless --openclaw
Files (1)
SKILL.md
3.2 KB

Claude Code Headless Mode

Run Claude Code from scripts without interactive UI.

Quick Start

# Basic headless execution
claude -p "Explain this code" --allowedTools "Read,Grep"

# JSON output for parsing
claude -p "List files" --output-format json

# Continue conversation
claude -p "Start analysis" --output-format json > result.json
session=$(jq -r '.session_id' result.json)
claude --resume "$session" "Now fix the issues"

Essential CLI Options

FlagDescription
-p, --printNon-interactive (headless) mode
--output-formattext, json, stream-json
-r, --resumeResume by session ID
-c, --continueContinue most recent session
--allowedToolsComma-separated allowed tools
--disallowedToolsComma-separated denied tools
--mcp-configPath to MCP server config JSON
--verboseEnable verbose logging
--append-system-promptAdd to system prompt

Permission Modes

ModeFlagEffect
Default(none)Prompt for permissions
Accept edits--permission-mode acceptEditsAuto-accept file changes
Bypass--permission-mode bypassPermissionsSkip all prompts

Output Formats

Text (default)

claude -p "Hello"
# Outputs: Human-readable response

JSON

claude -p "Hello" --output-format json
{
  "type": "result",
  "subtype": "success",
  "result": "Hello! How can I help?",
  "session_id": "abc123",
  "total_cost_usd": 0.001,
  "duration_ms": 1234,
  "num_turns": 1
}

Stream-JSON

claude -p "Hello" --output-format stream-json
# Real-time JSONL output for each message

Common Patterns

Script with tool restrictions

claude -p "Analyze the codebase" \
  --allowedTools "Read,Grep,Glob" \
  --disallowedTools "Write,Edit,Bash"

CI/CD integration

claude -p "Review this PR diff" \
  --permission-mode acceptEdits \
  --output-format json \
  --append-system-prompt "Focus on security issues"

Multi-turn automation

session=$(claude -p "Start task" --output-format json | jq -r '.session_id')
claude --resume "$session" "Continue with step 2"
claude --resume "$session" "Finalize and report"

Error Handling

result=$(claude -p "Task" --output-format json)
if [[ $(echo "$result" | jq -r '.is_error') == "true" ]]; then
    echo "Error: $(echo "$result" | jq -r '.result')" >&2
    exit 1
fi

Official Documentation

Additional Resources

  • ./references/cli-options.md - Complete CLI flag reference
  • ./references/output-formats.md - Output format schemas
  • ./references/integration-patterns.md - CI/CD and scripting examples

See Also: claude-code-hooks for automation events, claude-code-debug for troubleshooting

Source

git clone https://github.com/aiskillstore/marketplace/blob/main/skills/0xdarkmatter/claude-code-headless/SKILL.mdView on GitHub

Overview

Claude Code Headless Mode lets you run Claude Code from scripts without an interactive UI. It enables non-interactive execution with JSON or stream outputs and supports session-based multi-turn conversations, making automation, CI/CD, and scripting workflows practical.

How This Skill Works

The CLI uses -p/--print for headless execution and --output-format to select text, json, or stream-json. You can resume conversations with -r/--resume and continue with -c/--continue, while --allowedTools and --disallowedTools restrict what tools can be used in automation.

When to Use It

  • Automating code explanations or reviews in CI/CD pipelines without a UI
  • Parsing Claude's structured responses in scripts using --output-format json or stream-json
  • Running multi-turn analyses across steps by resuming a session
  • Enforcing tool restrictions in automation with --allowedTools/--disallowedTools
  • Integrating Claude Code into PR or code-review automation workflows

Quick Start

  1. Step 1: claude -p "Explain this code" --allowedTools "Read,Grep"
  2. Step 2: claude -p "List files" --output-format json
  3. Step 3: session=$(jq -r '.session_id' result.json); claude --resume "$session" "Now fix the issues"

Best Practices

  • Use non-interactive mode (-p/--print) for scripts and automation
  • Prefer JSON or stream-json output to simplify parsing with jq or similar tools
  • Leverage --resume and --continue for orchestrated multi-turn tasks
  • Limit capabilities in CI with --allowedTools/--disallowedTools
  • Capture and log total_cost_usd, duration_ms, and session_id from outputs

Example Use Cases

  • CI job that explains a code snippet and parses the result in JSON
  • Multi-turn code analysis across steps using a shared session
  • List files and parse results in a downstream step
  • Review PR diffs with a security-focused prompt and augmented system prompt
  • Fail-fast script that checks is_error and exits with a non-zero status

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers