Skip to content

aiskillstore/marketplace / skills/0xdarkmatter/claude-code-headless

claude-code-headless

Verified

Run Claude Code programmatically without interactive UI. Triggers on: headless, CLI automation, --print, output-format, stream-json, CI/CD, scripting.

FileKindSize
SKILL.mdskill md3.2 KB
references/cli-options.mdmarkdown4.2 KB
references/integration-patterns.mdmarkdown8.3 KB
references/output-formats.mdmarkdown4.6 KB
skill-report.jsonconfig102.5 KB

references/output-formats.md

View on GitHub
# Output Formats Reference

Detailed documentation for Claude Code output formats.

## Text Format (Default)

Human-readable output for terminal use.

```bash
claude -p "Hello"
# Output: Hello! How can I help you today?
```

Characteristics:
- Plain text response
- May include ANSI colors (disable with `NO_COLOR=1`)
- No metadata (session ID, cost, etc.)
- Best for interactive scripts

## JSON Format

Structured output for programmatic parsing.

```bash
claude -p "Hello" --output-format json
```

### Success Response

```json
{
  "type": "result",
  "subtype": "success",
  "result": "Hello! How can I help you today?",
  "session_id": "session_abc123xyz",
  "total_cost_usd": 0.00123,
  "duration_ms": 1542,
  "num_turns": 1,
  "is_error": false
}
```

### Error Response

```json
{
  "type": "result",
  "subtype": "error",
  "result": "Error message here",
  "session_id": "session_abc123xyz",
  "total_cost_usd": 0.0005,
  "duration_ms": 234,
  "num_turns": 0,
  "is_error": true
}
```

### Field Reference

| Field | Type | Description |
|-------|------|-------------|
| `type` | string | Always "result" |
| `subtype` | string | "success" or "error" |
| `result` | string | Response text or error message |
| `session_id` | string | Session identifier for resumption |
| `total_cost_usd` | number | Total API cost in USD |
| `duration_ms` | number | Total execution time in milliseconds |
| `num_turns` | number | Number of conversation turns |
| `is_error` | boolean | Whether result is an error |

### Parsing Examples

```bash
# Extract session ID
session=$(claude -p "Start" --output-format json | jq -r '.session_id')

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

# Get cost
cost=$(echo "$result" | jq -r '.total_cost_usd')
echo "Cost: \$${cost}"
```

## Stream-JSON Format

Real-time JSONL (JSON Lines) output for streaming applications.

```bash
claude -p "Count to 5" --output-format stream-json
```

### Message Types

#### Assistant Message

```json
{"type": "assistant", "content": "1", "timestamp": "2024-01-15T10:30:00Z"}
{"type": "assistant", "content": "2", "timestamp": "2024-01-15T10:30:01Z"}
```

#### Tool Use

```json
{"type": "tool_use", "tool": "Read", "input": {"file_path": "/path/to/file"}, "timestamp": "..."}
{"type": "tool_result", "tool": "Read", "output": "file contents...", "timestamp": "..."}
```

#### Final Result

```json
{"type": "result", "session_id": "...", "total_cost_usd": 0.01, "duration_ms": 5000}
```

### Stream Processing

```bash
# Process each line as it arrives
claude -p "Long task" --output-format stream-json | while IFS= read -r line; do
    type=$(echo "$line" | jq -r '.type')
    case "$type" in
        assistant)
            echo "Claude: $(echo "$line" | jq -r '.content')"
            ;;
        tool_use)
            echo "Using: $(echo "$line" | jq -r '.tool')"
            ;;
        result)
            echo "Done! Cost: \$$(echo "$line" | jq -r '.total_cost_usd')"
            ;;
    esac
done
```

### Node.js Stream Processing

```javascript
const { spawn } = require('child_process');
const readline = require('readline');

const claude = spawn('claude', ['-p', 'Task', '--output-format', 'stream-json']);

const rl = readline.createInterface({ input: claude.stdout });

rl.on('line', (line) => {
    const event = JSON.parse(line);
    switch (event.type) {
        case 'assistant':
            process.stdout.write(event.content);
            break;
        case 'result':
            console.log(`\nCost: $${event.total_cost_usd}`);
            break;
    }
});
```

### Python Stream Processing

```python
import subprocess
import json

proc = subprocess.Popen(
    ['claude', '-p', 'Task', '--output-format', 'stream-json'],
    stdout=subprocess.PIPE,
    text=True
)

for line in proc.stdout:
    event = json.loads(line)
    if event['type'] == 'assistant':
        print(event['content'], end='', flush=True)
    elif event['type'] == 'result':
        print(f"\nCost: ${event['total_cost_usd']}")
```

## Format Comparison

| Feature | text | json | stream-json |
|---------|------|------|-------------|
| Real-time output | Yes | No | Yes |
| Structured data | No | Yes | Yes |
| Session ID | No | Yes | Yes |
| Cost tracking | No | Yes | Yes |
| Easy parsing | No | Yes | Yes |
| Best for | Humans | Scripts | Real-time apps |

## Format Selection Guide

| Use Case | Format |
|----------|--------|
| Interactive terminal | text |
| CI/CD pipelines | json |
| Web applications | stream-json |
| Cost tracking | json |
| Session management | json |
| Live progress display | stream-json |
writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deploymentStars: 322kUpdated Claude CodeCodexVerifiedInstall
xlsxanthropics/skillsUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .xltx, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
shipping-and-launchaddyosmani/agent-skillsPrepares production launches. Use when preparing to deploy to production, or when asking what needs to be in place before shipping. Use when you need a pre-launch checklist, when setting up monitoring, when planning a staged rollout, or when you need a rollback strategy.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
childhoodthedotmack/claude-memBabysit a shipped release after merge, publication, or deployment. Verify published packages, production behavior, installed plugins, and late CI or user reports until the observation window is clean; use babysit for PR readiness before release.Stars: 118kUpdated Claude CodeCodexVerifiedInstall