home / skills / 0xdarkmatter / claude-mods / claude-code-headless

claude-code-headless skill

/skills/claude-code-headless

This skill runs Claude Code headlessly from scripts, enabling automated, non-interactive tasks with JSON output and session management.

npx playbooks add skill 0xdarkmatter/claude-mods --skill claude-code-headless

Review the files below or copy the command above to add this skill to your agents.

Files (6)
SKILL.md
3.2 KB
---
name: claude-code-headless
description: "Run Claude Code programmatically without interactive UI. Triggers on: headless, CLI automation, --print, output-format, stream-json, CI/CD, scripting."
compatibility: "Claude Code CLI"
allowed-tools: "Bash Read"
depends-on: []
related-skills: [claude-code-hooks, claude-code-debug]
---

# Claude Code Headless Mode

Run Claude Code from scripts without interactive UI.

## Quick Start

```bash
# 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

| Flag | Description |
|------|-------------|
| `-p`, `--print` | Non-interactive (headless) mode |
| `--output-format` | text, json, stream-json |
| `-r`, `--resume` | Resume by session ID |
| `-c`, `--continue` | Continue most recent session |
| `--allowedTools` | Comma-separated allowed tools |
| `--disallowedTools` | Comma-separated denied tools |
| `--mcp-config` | Path to MCP server config JSON |
| `--verbose` | Enable verbose logging |
| `--append-system-prompt` | Add to system prompt |

## Permission Modes

| Mode | Flag | Effect |
|------|------|--------|
| Default | (none) | Prompt for permissions |
| Accept edits | `--permission-mode acceptEdits` | Auto-accept file changes |
| Bypass | `--permission-mode bypassPermissions` | Skip all prompts |

## Output Formats

### Text (default)
```bash
claude -p "Hello"
# Outputs: Human-readable response
```

### JSON
```bash
claude -p "Hello" --output-format json
```
```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
```bash
claude -p "Hello" --output-format stream-json
# Real-time JSONL output for each message
```

## Common Patterns

### Script with tool restrictions
```bash
claude -p "Analyze the codebase" \
  --allowedTools "Read,Grep,Glob" \
  --disallowedTools "Write,Edit,Bash"
```

### CI/CD integration
```bash
claude -p "Review this PR diff" \
  --permission-mode acceptEdits \
  --output-format json \
  --append-system-prompt "Focus on security issues"
```

### Multi-turn automation
```bash
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

```bash
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

- https://code.claude.com/docs/en/headless - Headless mode reference
- https://code.claude.com/docs/en/settings - Settings and permissions

## 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

Overview

This skill enables running Claude Code programmatically from scripts and CI/CD pipelines without an interactive UI. It exposes headless CLI options for non-interactive prompts, JSON and streaming output formats, and session management for multi-turn automation. Use it to integrate Claude Code into automated workflows, testing, and batch processing.

How this skill works

The skill runs the claude CLI in headless mode using the --print or -p flag to suppress interactive prompts. It supports output-format options (text, json, stream-json), session resume and continue flags for multi-turn flows, and permission-mode flags to control interactive approvals. Tool access can be restricted with --allowedTools and --disallowedTools, and outputs can be parsed programmatically with jq or other JSON tools.

When to use it

  • Automating code reviews, diffs, or linting in CI/CD pipelines.
  • Batch-processing requests or generating reports without human intervention.
  • Embedding Claude Code into scripts that require deterministic outputs.
  • Testing and fuzzing language-model-driven tools programmatically.
  • Long-running multi-step tasks where sessions must be resumed across steps.

Best practices

  • Prefer --output-format json or stream-json for reliable parsing and structured metadata.
  • Lock down capabilities with --allowedTools / --disallowedTools to limit side effects.
  • Use --permission-mode acceptEdits or bypassPermissions in CI only when safe and reviewed.
  • Capture session_id from JSON outputs for multi-turn automation and reproducibility.
  • Log verbose output during development with --verbose, but keep CI logs minimal in production.

Example use cases

  • CI job that runs claude -p "Review PR diff" --permission-mode acceptEdits --output-format json and fails the build on reported security issues.
  • Shell script that lists project files with --output-format json, extracts session_id, then resumes the session to apply fixes.
  • Automated reporting pipeline using stream-json to consume real-time JSONL responses and stream into an aggregator.
  • Tool-restricted analysis: run claude -p "Analyze codebase" --allowedTools "Read,Grep" to prevent modifications during review.

FAQ

How do I continue a multi-turn conversation non-interactively?

Save session_id from a JSON response and call claude --resume "$session_id" "Your next prompt" to continue the session.

Which output format should I use for CI integration?

Use --output-format json for single responses and stream-json for real-time processing; both include metadata like session_id, cost, and duration.