Use --output-format stream-json for Real-Time Processing
If you're building tools that wrap Claude Code, you need structured output you can process line-by-line as it arrives. --output-format stream-json gives you exactly that — a stream of newline-delimited JSON events.
claude -p --output-format stream-json "Analyse the API routes in src/routes/"
Each line is a self-contained JSON object representing one event in the session:
{"type":"system","subtype":"init","session_id":"abc123","tools":["bash","read","write"]}
{"type":"assistant","message":{"content":[{"type":"text","text":"Looking at the routes..."}]}}
{"type":"assistant","message":{"content":[{"type":"tool_use","name":"read","input":{"file_path":"src/routes/api.ts"}}]}}
{"type":"result","subtype":"success","cost_usd":0.004,"duration_ms":3241}
Process the stream with jq to extract exactly what you need:
# Stream just the text content as it arrives
claude -p --output-format stream-json "Review the routes" | \
jq -r 'select(.type=="assistant") | .message.content[] | select(.type=="text") | .text'
# Log cost after every automated run
claude -p --output-format stream-json "Run the audit" | \
jq -r 'select(.type=="result") | "Cost: $\(.cost_usd) | Duration: \(.duration_ms)ms"'
If you don't need real-time streaming and prefer a single JSON object at the end, use --output-format json instead — same structure, but delivered all at once after the session completes.
stream-json turns Claude Code from a black box into a live event stream you can build on.
Log in to leave a comment.
Set up Claude Code as an automated reviewer in your CI pipeline — on every pull request, it reads the diff, checks for bugs, security issues, missing tests, and convention violations, then posts its findings as a PR comment. Your human reviewers get a head start because the obvious issues are already flagged before they look.
Before deploying, tell Claude to read your project — migrations, environment variables, queue workers, scheduled tasks, caching, third-party integrations — and generate a deployment checklist that's specific to your app. Not a generic "did you run migrations?" list, but one that knows YOUR infrastructure and catches the things YOUR deploy can break.
Instead of writing a README from memory or copying a template, tell Claude to read your project and generate one that's actually accurate — real setup instructions from your config, real architecture from your directory structure, real API examples from your routes, and real prerequisites from your dependency files.