Skip to main content
Code Guide
T02 Intermediate Technical

Non-Interactive & Headless Mode

Using Claude Code without human interaction: scripts, CI, pipes

Base flag: -p

The -p flag (or --print) activates non-interactive mode. Claude returns the response in the terminal and exits immediately without waiting for user input.

Terminal window
# Direct prompt
claude -p "Analyze this file and list the bugs"
# With pipe stdin (content becomes the input)
git diff | claude -p "Explain these changes"
cat error.log | claude -p "Identify the root cause"

Output formats

--output-format controls the response structure, which is essential for integration into scripts.

FormatUsage
textHuman-readable, default
jsonParseable by jq
stream-jsonReal-time streaming
Terminal window
# JSON output for automatic parsing
git status --short | claude -p "Categorize the changes" \
--output-format json | jq '.categories'
# Stream JSON for long operations
cat report.txt | claude -p "Summarize" --output-format stream-json

--no-stream and flow control

By default, Claude Code streams the response character by character. --no-stream waits for the complete response before displaying anything, which simplifies pipes with tools that expect complete input.

Terminal window
claude -p "Generate a report" --no-stream > report.md

CI/CD usage

In an isolated container, --dangerously-skip-permissions suppresses all confirmation requests. Reserve exclusively for sandboxed environments where Claude cannot access sensitive resources.

Terminal window
# GitHub Actions (isolated container)
claude -p "Run the tests and fix failures" \
--dangerously-skip-permissions \
--output-format json

Common patterns

Terminal window
# Automated log analysis
tail -n 200 app.log | claude -p "Critical alerts only" \
--output-format json
# PR review (package.json integration)
git diff main...HEAD | claude -p "Security review, JSON format" \
--output-format json > review.json

Interactive vs Non-Interactive

AspectInteractiveNon-Interactive (-p)
OutputStream UIRaw stdout
PermissionsPromptsAuto or skip
UsageDevelopmentCI, scripts, pipes
SessionPersistentSingle, stateless

Best practice: limit pipe size to avoid exceeding the context window. Filter with head, grep or --name-only before sending to Claude.