askill
claude-headless-mode

claude-headless-modeSafety 95Repository

Guide for using claude CLI in headless mode with --print, output formats, and JSON schema

1 stars
1.2k downloads
Updated 2/15/2026

Package Files

Loading files...
SKILL.md

Claude Headless Mode

Use claude -p (or --print) for non-interactive execution in scripts, CI, and automation.

Basic Usage

# Simple prompt
claude -p "Summarize this repo"

# With file input
claude -p "Review this code" < file.py

# Pipe output
claude -p "List all functions" | grep "def "

Output Formats

FormatFlagUse Case
Text--output-format textHuman-readable (default)
JSON--output-format jsonSingle JSON result
Stream JSON--output-format stream-json --verboseReal-time streaming

Text Output (Default)

claude -p "What is 2+2?"
# Output: 4

JSON Output

claude -p "What is 2+2?" --output-format json

Returns wrapper with metadata:

{
  "type": "result",
  "subtype": "success",
  "result": "4",
  "duration_ms": 1234,
  "total_cost_usd": 0.01,
  "session_id": "...",
  "structured_output": null
}

Extract just the result:

claude -p "What is 2+2?" --output-format json | jq -r '.result'

Stream JSON Output

claude -p "Long analysis" --output-format stream-json --verbose
  • Streams newline-delimited JSON as execution progresses
  • Shows tool calls, messages, progress in real-time
  • Final line contains the result
  • Requires --verbose flag

Extract final result:

claude -p "Analyze code" --output-format stream-json --verbose \
  | tee /dev/stderr \
  | tail -1 \
  | jq -r '.result'

Structured Output with JSON Schema

Force Claude to return data matching a specific schema:

claude -p "Extract function names from auth.py" \
  --output-format json \
  --json-schema '{
    "type": "object",
    "properties": {
      "functions": { "type": "array", "items": { "type": "string" } }
    },
    "required": ["functions"]
  }'

Result appears in structured_output field:

{
  "result": "...",
  "structured_output": {
    "functions": ["login", "logout", "verify_token"]
  }
}

Schema Examples

Simple object:

{
  "type": "object",
  "properties": {
    "answer": { "type": "integer" }
  },
  "required": ["answer"]
}

Task result (success/failure):

{
  "type": "object",
  "properties": {
    "status": { "type": "string", "enum": ["success", "failure"] },
    "summary": { "type": "string" },
    "files_changed": { "type": "array", "items": { "type": "string" } },
    "error_category": { "type": "string" },
    "suggestion": { "type": "string" }
  },
  "required": ["status", "summary"]
}

Extract from schema result:

OUTPUT=$(claude -p "$PROMPT" --output-format json --json-schema "$SCHEMA")
STATUS=$(echo "$OUTPUT" | jq -r '.structured_output.status')
SUMMARY=$(echo "$OUTPUT" | jq -r '.structured_output.summary')

Common Options

OptionDescription
-p, --printHeadless mode (required)
--output-formattext, json, stream-json
--json-schemaEnforce output schema
--verboseRequired for stream-json
--modelSelect model (sonnet, opus, haiku)
--max-turnsLimit agentic turns
--permission-modebypassPermissions, plan, etc.
--allowedToolsRestrict available tools

Permission Modes

# Skip all permission prompts (trusted environments only)
claude -p "Fix all linting errors" --permission-mode bypassPermissions

# Plan mode (read-only exploration)
claude -p "Analyze architecture" --permission-mode plan

Examples

CI/CD Integration

# Run tests and get structured result
RESULT=$(claude -p "Run tests and report results" \
  --output-format json \
  --json-schema '{"type":"object","properties":{"passed":{"type":"boolean"},"failures":{"type":"array","items":{"type":"string"}}},"required":["passed"]}')

if [ "$(echo $RESULT | jq '.structured_output.passed')" = "true" ]; then
  echo "Tests passed"
else
  echo "Tests failed"
  exit 1
fi

Batch Processing

for file in src/*.py; do
  claude -p "Review $file for security issues" \
    --output-format json \
    --json-schema '{"type":"object","properties":{"issues":{"type":"array"}},"required":["issues"]}' \
    | jq ".structured_output.issues"
done

Fresh Context Task Execution

# Each invocation is independent (no conversation history)
claude -p "Task 1: Create migration" --output-format json
claude -p "Task 2: Add model" --output-format json
claude -p "Task 3: Write tests" --output-format json

Notes

  • Each -p invocation starts fresh (no context from previous runs)
  • Use --output-format json for programmatic parsing
  • structured_output field contains schema-validated data
  • result field contains raw text response
  • Stream JSON requires --verbose flag
  • Cost info available in JSON output: total_cost_usd

Install

Download ZIP
Requires askill CLI v1.0+

AI Quality Score

95/100Analyzed 2/19/2026

Comprehensive skill covering Claude CLI headless mode with excellent examples for basic usage, output formats (text/JSON/stream-json), JSON schema structured output, common options, permission modes, and practical CI/CD integration examples. Well-organized with tables, code blocks, and clear sections. Includes proper safety notes about permission modes. Not project-specific, making it highly reusable.

95
95
95
90
95

Metadata

Licenseunknown
Version-
Updated2/15/2026
Publishermajiayu000

Tags

ci-cdllmpromptingsecurity