The CLI was previously called “headless mode.” The
-p flag and all CLI options work the same way.-p with your prompt and any CLI options:
claude -p). For the Python and TypeScript SDK packages with structured outputs, tool approval callbacks, and native message objects, see the full Agent SDK documentation.
Basic usage
Add the-p (or --print) flag to any claude command to run it non-interactively. All CLI options work with -p, including:
--continuefor continuing conversations--allowedToolsfor auto-approving tools--output-formatfor structured output
Examples
These examples highlight common CLI patterns.Get structured output
Use--output-format to control how responses are returned:
text(default): plain text outputjson: structured JSON with result, session ID, and metadatastream-json: newline-delimited JSON for real-time streaming
result field:
--output-format json with --json-schema and a JSON Schema definition. The response includes metadata about the request (session ID, usage, etc.) with the structured output in the structured_output field.
This example extracts function names and returns them as an array of strings:
Stream responses
Use--output-format stream-json with --verbose and --include-partial-messages to receive tokens as they’re generated. Each line is a JSON object representing an event:
-r flag outputs raw strings (no quotes) and -j joins without newlines so tokens stream continuously:
Auto-approve tools
Use--allowedTools to let Claude use certain tools without prompting. This example runs a test suite and fixes failures, allowing Claude to execute Bash commands and read/edit files without asking for permission:
Create a commit
This example reviews staged changes and creates a commit with an appropriate message:--allowedTools flag uses permission rule syntax. The trailing * enables prefix matching, so Bash(git diff *) allows any command starting with git diff. The space before * is important: without it, Bash(git diff*) would also match git diff-index.
User-invoked skills like
/commit and built-in commands are only available in interactive mode. In -p mode, describe the task you want to accomplish instead.Customize the system prompt
Use--append-system-prompt to add instructions while keeping Claude Code’s default behavior. This example pipes a PR diff to Claude and instructs it to review for security vulnerabilities:
--system-prompt to fully replace the default prompt.
Continue conversations
Use--continue to continue the most recent conversation, or --resume with a session ID to continue a specific conversation. This example runs a review, then sends follow-up prompts: