Streaming output in `--verbose --print`
Status Fixed / completed
Maintainer reply ✓ Yes — ashwin-ant
Activity 9 comments · opened Apr 7, 2025 · closed Apr 9, 2025
💡 Likely answer: A maintainer (ashwin-ant, collaborator)
responded on this thread — see the highlighted reply below.
Followup from https://github.com/anthropics/claude-code/issues/365#issuecomment-2784136143
When using --verbose with --print, output is only available as json:
Error: When using --print, --verbose must be used together with --json
at sg1.<anonymous> (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:1616:246)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async sg1.parseAsync (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:424:4184)
at async Or5 (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:1619:4348)
at async dr5 (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:1615:41755)
filipesilva@m4 ~/r/sandbox [1]> claude --verbose --json --print "say hello!"
[
{
"role": "user",
"content": "say hello!"
},
{
"id": "msg_01Xx3rAZ93AyDpMALs8nCJ9n",
"type": "message",
"role": "assistant",
"model": "claude-3-7-sonnet-20250219",
"content": [
{
"type": "text",
"text": "Hello!"
}
],
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 4,
"cache_creation_input_tokens": 26313,
"cache_read_input_tokens": 0,
"output_tokens": 5
}
},
{
"role": "system",
"cost_usd": 0.09876075000000001,
"duration_ms": 3365,
"duration_api_ms": 3366,
"result": "Hello!"
}
]
It would be much nicer for CI and debugging scenarios if it would stream the output like in the non-print call then exited:
filipesilva@m4 ~/r/sandbox> claude "say hello!"
╭────────────────────────────────────────────╮
│ ✻ Welcome to Claude Code research preview! │
│ │
│ /help for help │
│ │
│ cwd: /Users/filipesilva/repos/sandbox │
╰────────────────────────────────────────────╯
Tips for getting started:
1. Run /init to create a CLAUDE.md file with instructions for Claude
2. Run /terminal-setup to set up terminal integration
3. Use Claude to help with file analysis, editing, bash commands and git
4. Be as specific as you would with another engineer for the best results
> say hello!
⏺ Hello!
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ > Try "how does <filepath> work?" │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Press Ctrl-C again to exit
Total cost: $0.0080
Total duration (API): 1.7s
Total duration (wall): 11.2s
Total code changes: 0 lines added, 0 lines removed
9 Comments
Streaming + json would be nice too. Currently I have no way of extracting the partial script of the command stops before completion.
We've added this in 0.2.66! Try running:
It'll log individual JSON objects, so if you want to get a full JSON array at the end while also streaming, try:
Regular (non-streaming) JSON is still available via
--json(deprecated) or via--output-format json.For anyone wondering how to get this to work in Github Actions, where you need to force a TTY, you are welcome in advance.
https://github.com/actions/runner/issues/241#issuecomment-924327172
^ Another option in GitHub Actions is to do it in two steps:
@ashwin-ant the
--output-format stream-jsonis pretty nice, but the issue was originally about more of astream-textformat instead: just print whatclaudewould have printed otherwise.Consider the different for the
what's the current directoryprompt below:The
claude --print "whats the current directory"version does not show me thatclauderanpwd, which is a very important thing. The--output-format stream-jsondoes, but now you need to parse the json as its streamed, which is not that straightforward.The CI scenario is the one I really want to highlight here... if I'm running claude in CI I'd very much like to see the output as it comes, like pretty much any other logging in CI, as if I ran
claude <prompt>followed by exiting. One-shotting it, if you will, since CI is non-interactive.I don't have permissions to reopen but would like to, because streaming json was not the original ask, but also understand if you want to close it as something you're not interested in tackling.
I agree with @filipesilva but I can understand how a streaming text implementation could be a bit odd. Also one thing to consider is that the JSON can be somewhat hard to read for a human and does not include nice stuff like syntax highlighting for diffs, etc. Those things are nice when you are watching this stuff in CI.
Understood. We'll potentially revisit this, but given the variety of use cases to support, keeping it very raw right now hopefully addresses the most needs. You can stream out just messages with something like:
which outputs:
Or if you want tools as well, you can do:
which outputs:
You could extract
tool_resultmessages similarly.It's definitely a bit more work, but it should be pretty flexible. Claude Code is quite good at writing out
jqcommands 😉Some recent update changed the format to JSONL? At least this now works against 0.2.120:
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.