Headless mode
Run Claude Code programmatically without interactive UI
Overview
The headless mode allows you to run Claude Code programmatically from command line scripts and automation tools without any interactive UI.
Basic usage
The primary command-line interface to Claude Code is the claude
command. Use the --print
(or -p
) flag to run in non-interactive mode and print the final result:
Configuration Options
The SDK leverages all the CLI options available in Claude Code. Here are the key ones for SDK usage:
Flag | Description | Example |
---|---|---|
--print , -p | Run in non-interactive mode | claude -p "query" |
--output-format | Specify output format (text , json , stream-json ) | claude -p --output-format json |
--resume , -r | Resume a conversation by session ID | claude --resume abc123 |
--continue , -c | Continue the most recent conversation | claude --continue |
--verbose | Enable verbose logging | claude --verbose |
--append-system-prompt | Append to system prompt (only with --print ) | claude --append-system-prompt "Custom instruction" |
--allowedTools | Space-separated list of allowed tools, or string of comma-separated list of allowed tools | claude --allowedTools mcp__slack mcp__filesystem claude --allowedTools "Bash(npm install),mcp__filesystem" |
--disallowedTools | Space-separated list of denied tools, or string of comma-separated list of denied tools | claude --disallowedTools mcp__splunk mcp__github claude --disallowedTools "Bash(git commit),mcp__github" |
--mcp-config | Load MCP servers from a JSON file | claude --mcp-config servers.json |
--permission-prompt-tool | MCP tool for handling permission prompts (only with --print ) | claude --permission-prompt-tool mcp__auth__prompt |
For a complete list of CLI options and features, see the CLI reference documentation.
Multi-turn conversations
For multi-turn conversations, you can resume conversations or continue from the most recent session:
Output Formats
Text Output (Default)
JSON Output
Returns structured data including metadata:
Response format:
Streaming JSON Output
Streams each message as it is received:
Each conversation begins with an initial init
system message, followed by a list of user and assistant messages, followed by a final result
system message with stats. Each message is emitted as a separate JSON object.
Input Formats
Text Input (Default)
Streaming JSON Input
A stream of messages provided via stdin
where each message represents a user turn. This allows multiple turns of a conversation without re-launching the claude
binary and allows providing guidance to the model while it is processing a request.
Each message is a JSON ‘User message’ object, following the same format as the output message schema. Messages are formatted using the jsonl format where each line of input is a complete JSON object. Streaming JSON input requires -p
and --output-format stream-json
.
Agent Integration Examples
SRE Incident Response Bot
Automated Security Review
Multi-turn Legal Assistant
Best Practices
-
Use JSON output format for programmatic parsing of responses:
-
Handle errors gracefully - check exit codes and stderr:
-
Use session management for maintaining context in multi-turn conversations
-
Consider timeouts for long-running operations:
-
Respect rate limits when making multiple requests by adding delays between calls
Related Resources
- CLI usage and controls - Complete CLI documentation
- Common workflows - Step-by-step guides for common use cases