Getting Started Intermediate
Stream Responses (NDJSON)
Get real-time events as NDJSON for building UIs and pipelines
Command
$ "color:#7C5CFC">claude -p "Explain recursion" "color:#d97757">--output-format stream-json "color:#d97757">--verbose
Response
{"type": "system","subtype": "init","tools":[...],"model": "claude-opus-4-6"}
{"type": "assistant","message":{"content":[...]}}
{"type": "rate_limit_event","rate_limit_info":{...}}
{"type": "result","subtype": "success","total_cost_usd": 0.08} Parsing Code
059669">">// Parse NDJSON line by line — NOT as a JSON array 059669">">const lines = output.trim().split(059669059669">">'\n'); 059669">">for (059669">">const line 059669">">of lines) { 059669">">const event = JSON.parse(line); 059669">">if (event.type === 059669059669">">'result') console.log(event.result); }
Gotchas
! --verbose is required with stream-json to get the init event
! NDJSON = one JSON per line. Parse line by line, never JSON.parse the whole output