Claude Code stream-json output: flags, events and jq

Run claude -p with --output-format stream-json and --verbose, and Claude Code prints one JSON object per line as the run goes: a system init event first, then assistant and user messages, and a result event last with the answer, cost and session ID.

Contents

Claude Code prints stream-json output when you run claude -p "your prompt" --output-format stream-json --verbose: one JSON object per line, written as the run goes. A system init line comes first, with the model, tools and MCP servers. assistant and user lines follow as the agent thinks and calls tools, and a result line closes the run with the answer, the cost and the session ID.

Flags and field names below come from Anthropic's headless docs, CLI reference and Agent SDK message types, read on 24 October 2026. They change between versions, so if a flag is refused, run claude --version before anything else.

The stream-json flags#

Flag What it does Needs
--output-format stream-json Newline-delimited JSON, one event per line -p
--verbose Full turn-by-turn output. Anthropic's examples always pass it with stream-json
--include-partial-messages Adds stream_event lines for text as it's generated -p, stream-json
--forward-subagent-text Adds subagents' text and thinking, not only their tool calls -p, stream-json
--include-hook-events Adds hook lifecycle events (SessionStart and Setup hooks are always in) stream-json
--input-format stream-json Reads user messages from stdin, one JSON object per line -p
--replay-user-messages Echoes each stdin message back on stdout when it's taken both formats stream-json
--json-schema '<schema>' Puts schema-checked output in the result's structured_output -p

A first run on a prompt that only reads files:

bash
claude -p "List the TODO comments in src/" --output-format stream-json --verbose

The events you'll meet#

Every line has a type. Most also carry session_id and a uuid.

type (subtype) When Fields worth reading
system (init) First, unless startup hooks or plugin installs print before it model, tools, mcp_servers, mcp_server_errors, plugins, plugin_errors, permissionMode, claude_code_version, cwd
assistant Each message from the model message.content: text, thinking and tool_use blocks; parent_tool_use_id
user Each tool result going back in, and a subagent's prompt message.content with tool_result blocks; parent_tool_use_id
stream_event Only with --include-partial-messages event, the raw API stream event; event.delta.text for text
system (api_retry) Before Claude Code retries a failed API call attempt, max_retries, retry_delay_ms, error_status, error (rate_limit, overloaded, billing_error…)
system (permission_denied) A tool call was refused Also collected in the result's permission_denials
result Last line subtype, result, is_error, num_turns, duration_ms, total_cost_usd, usage, modelUsage, session_id

A clean run ends with "subtype": "success" and the answer in result. The other four endings, error_max_turns, error_max_budget_usd, error_during_execution and error_max_structured_output_retries, have no result field, only an errors array, so check subtype as well as the exit code before you read the answer.

The SDK's message type lists more kinds than the table (task progress, status, compaction, rate-limit events and others). Skip a type your script doesn't know rather than failing on it; Anthropic gives the same advice for the capabilities list in init.

A stream-json run from top to bottom: one system init line, assistant tool_use and user tool_result lines, optional stream_event lines, and one result line at the end. system · initmodel, tools, MCP servers assistant · tool_usethe agent calls a tool stream_eventwith --include-partial-messages user · tool_resultthe result goes back in system · api_retryonly when a call is retried assistant · textthe answer resultanswer, cost, session_id

jq one-liners#

Live text, from Anthropic's docs (-j joins without newlines):

bash
claude -p "Write a poem" --output-format stream-json --verbose --include-partial-messages | \
  jq -rj 'select(.type == "stream_event" and .event.delta.type? == "text_delta") | .event.delta.text'

Four more, each reading a run saved with tee run.jsonl, so one run answers every question:

bash
# the final answer and what it cost
jq -r 'select(.type == "result") | "\(.subtype) $\(.total_cost_usd) \(.result // "")"' run.jsonl

# every tool the agent called, in order
jq -r 'select(.type == "assistant") | .message.content[] | select(.type == "tool_use") | .name' run.jsonl

# exit non-zero in CI when an --mcp-config entry was skipped as invalid
jq -e 'select(.type == "system" and .subtype == "init") | (.mcp_server_errors // []) | length == 0' run.jsonl

# each retry as a "rate limited, retrying in 8s" line for your own interface (pipe the live stream in to see them as they happen)
jq -r 'select(.type == "system" and .subtype == "api_retry") | "\(.error) attempt \(.attempt)/\(.max_retries), \(.retry_delay_ms)ms"' run.jsonl

mcp_server_errors is left out when there are no errors, and a server that fails to connect shows its status in mcp_servers instead.

Subagents in the stream#

assistant and user lines from a subagent look exactly like the main conversation's except for parent_tool_use_id. It is null in the main conversation. In a subagent's lines it holds the ID of the Agent or Skill tool call that started it, and a nested subagent's lines hold their own parent's ID, which is enough to rebuild the tree.

Without --forward-subagent-text (or the CLAUDE_CODE_FORWARD_SUBAGENT_TEXT variable) you see a subagent's tool calls and results, and none of its text or thinking. Each kind of nesting has a minimum Claude Code version in Anthropic's docs, and older versions leave some of those lines out.

bash
jq -r 'select(.parent_tool_use_id != null) | "\(.parent_tool_use_id) \(.type)"'

Sending prompts in stream-json#

--input-format stream-json lets one process take several prompts from stdin, one JSON line each, in the SDK's user-message shape:

bash
printf '%s\n' \
  '{"type":"user","message":{"role":"user","content":"Read README.md"},"parent_tool_use_id":null}' \
  | claude -p --input-format stream-json --output-format stream-json --verbose

Claude Code keeps reading until stdin closes, then finishes the work it has. --replay-user-messages echoes each of your messages back on stdout at the moment it is taken, which is how a program knows a queued prompt arrived. With --max-turns, a message still queued when the limit ends a turn stays queued and starts the next turn with a limit of its own.

Running several agents from one script#

To drive more than one Claude Code run from a script, start each claude -p as its own process, read each stdout line by line, and key everything on session_id. Anthropic's headless page adds three rules.

  • A slow reader delays the exit. Claude Code waits, up to 30 seconds, for queued output to drain.
  • kill -INT, not plain kill. Plain kill sends SIGTERM, which exits with code 143 and leaves the turn unfinished with no result; SIGINT ends the turn first.
  • --resume takes the session_id from init, so keep it (headless mode has resuming, budgets and the permission flags).

Several agents side by side in terminal tabs, with no script, is a different setup: running Claude Code agents in parallel.

Three things that tripped our own parser#

Shuriken's Claude Code adapter (agent.ts in our server) reads this stream for every chat. Three behaviours cost us a bug each.

  1. While Claude Code compacts the conversation, it sends a system status line with "status": "compacting" every 30 seconds until the compact_boundary line arrives. Our adapter used to open a "Compacting..." marker per line; only one ever closed, and the rest spun forever. Open one per compaction, and treat "compact_result": "failed" on a status line as the CLI giving up.
  2. Our live-stream path used to anchor turns on subagent lines too. A subagent line's uuid lives on the subagent's sidechain, not the main session, so a rewind to such a turn failed with Message <uuid> not found in session. Lines with a parent_tool_use_id are now skipped when we record where a turn starts.
  3. On chats routed through Shuriken's local proxy, a dead route used to cost ten tries, about 6.5 minutes against a closed port, each announced by an api_retry line with error_status: null, before the turn failed. A null status means the connection never opened, so we now repair the route on the first such line and the next try lands inside the same turn.

How Shuriken reads it#

Shuriken drives Claude Code through Anthropic's Agent SDK (@anthropic-ai/claude-agent-sdk 0.3.246 in our server's package.json), which runs Claude Code in this same streaming JSON mode, and turns the events into the chat you see:

  • includePartialMessages: true is set on every chat, so text appears as it arrives.
  • tool_use blocks become cards. A line whose parent_tool_use_id isn't null goes to that subagent's part of the chat, not the main thread.
  • Each result's usage and total_cost_usd go into the turn's cost. The figure you see before that line arrives is explained in what each turn costs, while it runs.
  • The system handler branches on eight subtypes, init, status, compact_boundary, task_notification, task_progress, api_retry, plugin_install and commands_changed, and on the line that names an original_model and a fallback_model when another model carries a turn on.
  • Stopping a chat calls the SDK's interrupt(), the docs' other way to end a turn instead of killing it.

If you'd rather not write the parser, download Shuriken.

Sources

  1. Claude Code docs: Run Claude Code programmatically (accessed 24 October 2026)
  2. Claude Code docs: CLI reference (accessed 24 October 2026)
  3. Claude Code docs: Agent SDK reference, TypeScript (message types) (accessed 24 October 2026)

Frequently asked questions

What is the difference between json and stream-json in Claude Code?

--output-format json waits for the run to end and prints one object with the result, session ID and cost. stream-json prints a line per event as the run goes and ends with that same result event, which is what a progress display needs.

Do I need --verbose with stream-json?

Every stream-json example with -p in Anthropic's docs passes --verbose, and every command on this page does too. Leave it in.

How do I get token-by-token text from claude -p?

Add --include-partial-messages. You then also get stream_event lines; filter the ones whose event.delta.type is text_delta and print event.delta.text.

How do I tell subagent messages apart in stream-json?

Look at parent_tool_use_id on each line: null is the main conversation, and an ID points at the tool call that started that subagent. --forward-subagent-text adds the subagents' text and thinking to the stream.

Can I send several prompts to one claude -p process?

Yes, with --input-format stream-json: write one user message per line to stdin. Add --replay-user-messages to have each one echoed back on stdout when Claude Code takes it.

Free tools