Stream responses as real-time JSON events

cli flags & headless

// what it does

claude -p "query" --output-format stream-json --verbose emits newline-delimited JSON, one object per event, so you can react to tool calls and text as they happen; add --include-partial-messages to also stream token deltas. The final line is a result message with the full text, cost, and session ID. The first event, system/init, reports the model, tools, MCP servers, and loaded plugins.

// shell

$ claude -p "explain recursion" --output-format stream-json --verbose --include-partial-messages

// gotcha

stream-json requires --verbose in print mode or you get an error. Subagent messages appear with a parent_tool_use_id field; by default only their tool_use blocks stream — pass --forward-subagent-text to also emit subagent text and thinking (Claude Code v2.1.211+).

// resources