Response JSON Parse Failure — Fix (2026)
The Error
SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>)
Response body: '{"id":"msg_01X...","type":"message","content":[{"type":"text","tex'
This error occurs when the API response is truncated before the SDK can parse it. The JSON body is incomplete, usually cut off mid-stream.
The Fix
- Increase the request timeout to allow longer responses:
export ANTHROPIC_TIMEOUT=120000
- If using the Python SDK, set timeout explicitly:
import anthropic
client = anthropic.Anthropic(timeout=120.0)
- If using the Node.js SDK:
const client = new Anthropic({ timeout: 120000 });
- Test with a short prompt to confirm connectivity:
claude "say ok"
Why This Happens
The response is cut short by a network timeout, TCP reset, or proxy that enforces a maximum response size. The SDK receives a partial JSON body and attempts to parse it, causing the SyntaxError. This is especially common with long responses (high max_tokens) or slow connections.
If That Doesn’t Work
- Switch to streaming mode which processes chunks incrementally:
claude config set streaming true
- Check if your proxy has a response size limit:
curl -v https://api.anthropic.com/v1/messages \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{"model":"claude-sonnet-4-20250514","max_tokens":100,"messages":[{"role":"user","content":"hi"}]}' 2>&1 | grep -i "content-length\|transfer-encoding"
- Reduce
max_tokensto get shorter responses that complete within timeouts.
Prevention
Add this to your CLAUDE.md:
# Response Handling
- Always use streaming for responses expected to exceed 4096 tokens.
- Set ANTHROPIC_TIMEOUT=120000 in all environments.
- Implement retry with exponential backoff for parse failures.
See Also
- IPv6 Fallback Failure Error — Fix (2026)
- JSON Parse Error on Malformed Response Fix
- Response Truncation Max Tokens Hit Fix
Related Error Messages
This fix also applies if you see these related error messages:
ETIMEDOUT: connection timed outRequestTimeout: request took longer than 120000msESOCKETTIMEDOUTECONNREFUSED: connection refused through proxyError: unable to verify the first certificate
Frequently Asked Questions
What is the default timeout for Claude Code API requests?
The default timeout is 120 seconds (120000ms). For complex operations involving large codebases or multi-file edits, this may be insufficient. Increase it with claude config set api_timeout 300000 for a 5-minute timeout.
Can network latency cause timeouts?
Yes. Corporate proxies, VPNs, and DNS filtering services add round-trip latency. Measure your baseline latency with curl -o /dev/null -s -w '%{time_total}' https://api.anthropic.com/v1/messages. If it exceeds 5 seconds, route API traffic outside the proxy.
Do timeouts consume API credits?
Partially. If the server began processing your request before the client timed out, the input tokens are consumed even though you never received a response. Long timeouts reduce wasted credits by allowing the response to complete.
How do I configure Claude Code to use a corporate proxy?
Set the HTTPS_PROXY environment variable: export HTTPS_PROXY=http://proxy.corp.com:8080. Claude Code respects standard proxy environment variables. Add this to your shell profile for persistence.
Related Guides
Try it: Paste your error into our Error Diagnostic for an instant fix.
- Prisma Generate Failure After Schema
- Pre-Commit Hook Failure on Claude — Fix (2026)
- TLS Version Negotiation Failure — Fix
- ESM vs CJS Module Resolution Failure — Fix (2026)
JSON Error Patterns in Claude Code
JSON parsing errors occur in several contexts within Claude Code, each with a different root cause:
API response parsing. The Anthropic API returns JSON responses. If the response is truncated (network timeout, proxy interference) or modified (corporate proxy injecting error pages), the JSON parser fails. Check network connectivity first.
Configuration file parsing. Files like settings.json, tsconfig.json, and package.json must be valid JSON. Common issues: trailing commas (not allowed in JSON), comments (not allowed in standard JSON, but allowed in tsconfig.json), and UTF-8 BOM characters at the start of the file.
Tool output parsing. When Claude Code’s tools return results, they are JSON-encoded. If a bash command produces output that interferes with JSON encoding (binary data, very long lines, or null bytes), the parsing fails.
Debugging JSON Parse Errors
Follow this sequence to identify and fix the problem:
# Validate a JSON file
python3 -m json.tool file.json 2>&1
# Find the exact error location
python3 -c "
import json
with open('file.json') as f:
try:
json.load(f)
except json.JSONDecodeError as e:
print(f'Error at line {e.lineno}, column {e.colno}: {e.msg}')
"
# Remove BOM characters
sed -i '1s/^//' file.json
# Check for trailing commas (common mistake)
grep -n ',[ ]*[}\]]' file.json