Claude Code CLI JSON Response Truncation Issue
Summary
Claude Code CLI truncates long JSON responses at fixed character positions (4000, 6000, 8000, 10000, 12000, 16000 characters), causing JSON parsing errors when the truncation occurs mid-JSON
structure.
Environment
- Claude Code Version: 1.0.35
- Node.js Version: v22.15.0
- Platform: macOS (Darwin 21.6.0)
- Model: claude-3-5-sonnet-20241022
Expected Behavior
Claude Code CLI should return complete JSON responses regardless of length, allowing proper JSON parsing by downstream applications.
Actual Behavior
When Claude generates JSON responses longer than ~8000-10000 characters, the CLI truncates the output at fixed positions, resulting in malformed JSON that fails to parse with errors like:
SyntaxError: Unterminated string in JSON at position 8000
SyntaxError: Unexpected end of JSON input at position 4000
Reproduction Steps
- Create a test script that requests a long JSON response:
import { query } from '@anthropic-ai/claude-code';
const prompt = `Generate a JSON object with 25 detailed tasks. Each task should have comprehensive implementation details to ensure the response is over 15,000 characters. Respond ONLY with valid
JSON containing a 'tasks' key.`;
for await (const chunk of query({ prompt })) {
console.log(chunk);
}
2. Run the script and observe JSON parsing errors at specific character positions
Root Cause Analysis
The issue is in /lib/node_modules/@anthropic-ai/claude-code/sdk.mjs lines 125-130:
for await (const line of rl) {
if (line.trim()) {
yield JSON.parse(line); // ← Assumes each line is complete JSON
}
}
The problem mechanism:
- Claude CLI process writes long JSON response to stdout
- When response exceeds certain length, CLI internally truncates at fixed positions
- readline.createInterface reads line-by-line, but receives truncated incomplete JSON
- JSON.parse() attempts to parse incomplete JSON string, causing parse failure
Truncation Patterns Observed
Through systematic testing, we identified these truncation patterns:
- Short responses (<10K chars): No truncation
- Medium responses (10-30K chars): 8000 character truncation
- Long responses (30-50K chars): 4000 character truncation
- Very long responses: Even smaller truncation limits
Common truncation positions: 4000, 6000, 8000, 10000, 12000, 16000 characters
Evidence
Direct Claude API vs Claude Code CLI comparison:
- Direct Anthropic API calls return complete responses (tested with same prompts)
- Claude Code CLI consistently truncates at the same positions
- The actual assistant response content is complete, but CLI processing truncates it
SDK Debug Output:
🔍 [SDK.mjs] Line 1: length=8000 [SUSPECTED TRUNCATION AT 8000!]
🚨 [SDK.mjs] TRUNCATION PATTERN DETECTED: Line length is exactly 8000 characters!
🚨 [SDK.mjs] Line ends abruptly without JSON closure - CONFIRMED TRUNCATION
❌ [SDK.mjs] JSON parse failed: Unterminated string in JSON at position 8000
Impact
This issue affects:
- Applications that generate long structured JSON responses
- Task management systems that create detailed task breakdowns
- Any workflow requiring comprehensive JSON data from Claude
Workaround
We've implemented a temporary workaround in our application:
try {
const parsed = JSON.parse(line);
yield parsed;
} catch (parseError) {
// Check if we have valid text data despite JSON parsing error
if (error instanceof SyntaxError &&
error.message.includes('JSON') &&
error.message.includes('position') &&
text && text.length > 0) {
// Continue processing with the complete data we have
console.log('Working around Claude Code CLI truncation issue...');
// Skip error and continue
} else {
throw parseError;
}
}
Suggested Fix
The SDK should either:
- Buffer accumulation: Accumulate lines until complete JSON is received before parsing
- Streaming JSON parser: Use a streaming JSON parser that can handle partial chunks
- CLI output fix: Fix the underlying CLI truncation behavior
Request
Please fix this truncation issue in Claude Code CLI to ensure complete JSON responses are preserved regardless of length. This is critical for applications that rely on structured data output from
Claude.
Additional Information
Files involved:
- @anthropic-ai/claude-code/sdk.mjs (line 125-130)
Test cases available:
We have complete test scripts and debug data available if needed for investigation.
Claude Code CLI JSON Response Truncation Issue
Summary
Claude Code CLI truncates long JSON responses at fixed character positions (4000, 6000, 8000, 10000, 12000, 16000 characters), causing JSON parsing errors when the truncation occurs mid-JSON
structure.
Environment
Expected Behavior
Claude Code CLI should return complete JSON responses regardless of length, allowing proper JSON parsing by downstream applications.
Actual Behavior
When Claude generates JSON responses longer than ~8000-10000 characters, the CLI truncates the output at fixed positions, resulting in malformed JSON that fails to parse with errors like:
SyntaxError: Unterminated string in JSON at position 8000
SyntaxError: Unexpected end of JSON input at position 4000
Reproduction Steps
The problem mechanism:
Truncation Patterns Observed
Through systematic testing, we identified these truncation patterns:
Common truncation positions: 4000, 6000, 8000, 10000, 12000, 16000 characters
Evidence
Direct Claude API vs Claude Code CLI comparison:
SDK Debug Output:
🔍 [SDK.mjs] Line 1: length=8000 [SUSPECTED TRUNCATION AT 8000!]
🚨 [SDK.mjs] TRUNCATION PATTERN DETECTED: Line length is exactly 8000 characters!
🚨 [SDK.mjs] Line ends abruptly without JSON closure - CONFIRMED TRUNCATION
❌ [SDK.mjs] JSON parse failed: Unterminated string in JSON at position 8000
Impact
This issue affects:
Workaround
We've implemented a temporary workaround in our application:
Suggested Fix
The SDK should either:
Request
Please fix this truncation issue in Claude Code CLI to ensure complete JSON responses are preserved regardless of length. This is critical for applications that rely on structured data output from
Claude.
Additional Information
Files involved:
Test cases available:
We have complete test scripts and debug data available if needed for investigation.