With claude -p a script normally sees nothing until the whole answer is done. For long runs it’s nicer to watch the text appear.
claude -p "Explain what this repository does" \
--output-format stream-json --verbose --include-partial-messages | \
jq -rj 'select(.type == "stream_event" and .event.delta.type? == "text_delta") | .event.delta.text'
stream-json prints one JSON object per line as events happen. The docs pair it with --verbose, and --include-partial-messages adds the small text chunks as they’re generated. The jq filter keeps only the text deltas. -r prints raw strings and -j leaves out the newlines between chunks, so the words flow like in the terminal.
The other lines aren’t noise. The stream starts with a system event that lists the model, tools and MCP servers, and every turn ends with a result message with the cost and session ID. In a CI job I’d suggest saving the raw stream to a file with tee and filtering a copy for display.
Find the result by its type, not by being the last line. A background task can start another turn after a result, so the stream can contain more than one. And these are event formats for programs: fields get added over time, so match on the ones you need and ignore the rest.
Further reading: Run Claude Code programmatically: stream responses, CLI reference