brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: stream claude -p output as JSON and print only the text as Claude writes it

Claude
~/tips/tip-claude-stream-json-jq

claude -p "Explain what this repository does" \

Shows the answer of a headless Claude Code run live instead of waiting for the end.

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

More Claude tips /recap /focus /skill-doctor /autocompact 500k claude purge ~/work/my-repo --dry-run All Claude tips (83) All topics

Link