Run Claude Code programmatically without interactive UI
claude
command. Use the --print
(or -p
) flag to run in non-interactive mode and print the final result:
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 |
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.
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
.