[BUG] Partial output when using the non-interactive mode
Environment
- Platform (select one):
- [x] Anthropic API
- [ ] AWS Bedrock
- [ ] Google Vertex AI
- [ ] Other: <!-- specify -->
- Claude CLI version: 1.0.48 (Claude Code)
- Operating System: macOS 15.5
- Node version: v22.12.0
- Terminal: kitty
Bug Description
Hey,
When working on an integration, I've noticed that Claude Code sometimes exits before outputting valid messages. I've added handling on partial data, but I've noticed the same behavior when using the Claude Code SDK.
I suspect the cause might be an unflushed buffer before exiting, but I am not sure if it's just the request failing.
Steps to Reproduce
- Start a new request with a significantly large context and using the
stream-jsonoutput format. In my case, I am using a 47460 bytes long system prompt and a 221264 bytes long prompt.
Expected Behavior
The output is piped entirely to stdout or a proper error indication is returned.
Actual Behavior
The SDK throws an error when it fails to parse JSON:
SyntaxError: Unterminated string in JSON at position 8000 (line 1 column 8001)
at JSON.parse (<anonymous>)
at Query.readMessages (file:///test/node_modules/@anthropic-ai/claude-code/sdk.mjs:255:32)
Additional Context
When this does happen, the output is consistently either 8 or 10k characters long. The partial chunk is mostly the result one.
I am using the latest version of Claude Code and of the SDK, but I have noticed it in all the versions I tested since the release.
This issue has 8 comments on GitHub. Read the full discussion on GitHub ↗