> cli flags & headless

Print mode (-p / --print) turns Claude Code into a Unix tool: it reads stdin, writes the result to stdout, and exits, so you can pipe diffs and logs in, redirect answers out, and drop it into CI. This section covers the flags that make automation reliable — structured JSON output, streaming events, turn and budget caps, model selection, and extra context. For reproducible runs, --bare skips all local auto-discovery so only the flags you pass take effect.

// cli flags & headless

7 commands

// faq

How do I use Claude Code in a script or CI pipeline?

Run claude -p "your prompt" and read stdout. Add --output-format json to get the result plus cost and session metadata as parseable JSON, --allowedTools to pre-approve tools so it doesn't stall on a prompt, and --bare so it ignores whatever hooks or MCP servers happen to be configured on the machine. --max-turns and --max-budget-usd bound how far an unattended run can go.

What's the difference between --output-format json and stream-json?

json returns one final object with the complete result, cost, and session ID — best when you just want the answer. stream-json emits newline-delimited JSON events in real time (one per tool call, message, and, with --include-partial-messages, token delta), so you can react as the agent works. stream-json requires --verbose in print mode.

How do I control which model runs?

Pass --model with an alias (sonnet, opus) or a full ID (claude-sonnet-5). Aliases always resolve to the current generation, so pin a full ID when you need reproducibility across releases. --fallback-model lets Claude drop to another model when the primary is overloaded, and --effort trades speed for reasoning depth.

How do I cap what a headless run costs?

--max-budget-usd sets a dollar ceiling on API calls in print mode, and --max-turns limits agentic turns. Both stop the run when hit, so set them high enough to finish a realistic task. Read total_cost_usd from the --output-format json payload to calibrate the right ceiling for your workload.