Skip to main content

Headless and Print Mode

Print mode runs one prompt, prints the answer, and exits. Use it in scripts, pipes, and CI. It is also the base of the Agent SDK. Add -p (or --print) to start it. For example, claude -p "Summarize README.md" --output-format json prints one JSON result.

warning

Print mode skips the workspace trust dialog. This also happens when stdout is not a terminal, such as when you pipe or redirect output. Use it only in folders you trust.

At a Glance​

QuestionAnswer
Can you use it?✅ Yes. claude -p "<prompt>", or send the prompt on stdin.
Can you change it?✅ Yes. Flags set the output and input format, budget, schema, and permission handling.
Can you turn it on or off?-p turns it on. --bare strips extras. --no-session-persistence saves nothing.
Where does it live?Flags on the claude command. Sessions still save to ~/.claude/projects/ unless you turn that off.
StatusDocumented Undocumented

Source: Run Claude Code Programmatically, CLI Reference, and claude --help. The full flag list is on CLI Flags.

FlagStatusWhat It DoesNotes
-p, --printDocumentedPrints the answer and exits.Skips the trust dialog.
--output-format <format>DocumentedSets the output.text (default), json (one result object), or stream-json (one JSON object per line, live).
--input-format <format>DocumentedSets the input.text (default) or stream-json (live input, and control requests).
--include-partial-messagesDocumentedAdds partial message chunks as the answer grows.Needs --output-format=stream-json. Adds stream_event lines.
--include-hook-eventsDocumentedAdds hook lifecycle events to the stream.Needs --output-format=stream-json.
--forward-subagent-textDocumentedAdds subagent text and thinking to the stream.Messages carry parent_tool_use_id. Needs stream-json output.
--replay-user-messagesDocumentedSends user messages from stdin back on stdout as an acknowledgment.Needs both stream-json formats.
--json-schema <schema>DocumentedChecks the final answer against a JSON Schema.Pass the schema as a JSON string.
--max-budget-usd <amount>DocumentedStops at a spend limit in dollars.Print mode only.
--prompt-suggestions [value]DocumentedTurns on prompt suggestions.Print mode sends a prompt_suggestion message after each turn.
--permission-prompts <target>DocumentedSays who answers permission prompts.host (the SDK host or --permission-prompt-tool) or none (anything that would prompt is denied; the permission mode decides the rest).
--no-session-persistenceDocumentedSaves nothing to disk.The session cannot be resumed. Print mode only.
--bareDocumentedStarts with a minimal setup.Skips hooks, LSP, plugin sync, attribution, auto memory, background prefetches, keychain reads, and CLAUDE.md discovery.
--restrictedDocumentedRemoves the tools that run code and WebFetch, and ignores user, project, and local settings.Good for untrusted input.
--allowedTools, --disallowedTools, --toolsDocumentedChoose which tools run without a prompt, which are blocked, and which exist.See CLI Flags.
--fallback-model <model>DocumentedNames models to try when the main model is overloaded or not available.See Models.
--resume, --continue, --fork-session, --session-idDocumentedPick, branch, or name the session.See Sessions and Storage.
--mcp-config, --strict-mcp-configDocumentedLoad MCP servers for this run.See MCP Settings and Policy.

Example​

One JSON answer, with a budget and a schema
claude -p "List the three biggest files in this repo" \
--output-format json \
--max-budget-usd 1 \
--json-schema '{"type":"object","properties":{"files":{"type":"array","items":{"type":"string"}}},"required":["files"]}'
Watch the answer grow, line by line
claude -p "Explain this repo" --output-format stream-json --verbose --include-partial-messages \
| while read -r line; do echo "$line" | jq -r '.type'; done

The second command prints a system line for init, then stream_event lines, then one result line.

How It Works​

The loop is the same as in interactive mode. See Agent Loop. Permission prompts have no screen to appear on, so a host program (or --permission-prompts none) must decide.

Pages in This Section​

PageWhat It Covers
Stream JSON MessagesEvery message type that print mode writes
Control ProtocolEvery control request a host can send on the same stream