Anthropic SDK Streaming Connection (2026)

The Error

anthropic.APIConnectionError: Connection error.
  Request was interrupted during streaming response.
  Received 1,247 of expected ~4,000 tokens before connection was lost.

Or in TypeScript:

AnthropicError: Connection terminated unexpectedly
  at StreamingMessageResponse._readableStream

The Fix

  1. Wrap streaming calls with reconnection logic that preserves partial output
import anthropic
def stream_with_recovery(client, messages, max_retries=3, **kwargs):
    collected_text = ""
    for attempt in range(max_retries):
        try:
            with client.messages.stream(
                messages=messages,
                **kwargs
            ) as stream:
                for text in stream.text_stream:
                    collected_text += text
                    print(text, end="", flush=True)
                return collected_text
        except anthropic.APIConnectionError:
            if attempt == max_retries - 1:
                raise
            messages = messages + [
                {"role": "assistant", "content": collected_text},
                {"role": "user", "content": "Continue from where you left off."}
            ]
  1. Increase your HTTP client timeout settings
client = anthropic.Anthropic(
    timeout=600.0,  # 10 minutes for long responses
)
  1. Verify the fix:
python3 -c "
import anthropic
client = anthropic.Anthropic(timeout=600.0)
with client.messages.stream(model='claude-sonnet-4-20250514', max_tokens=1000, messages=[{'role':'user','content':'Write a 500-word essay about trees.'}]) as s:
    text = ''.join([t for t in s.text_stream])
print(f'Received {len(text)} chars successfully')
"
# Expected: Received XXXX chars successfully

Why This Happens

Streaming connections use long-lived HTTP/2 or SSE connections that are vulnerable to network interruptions. Corporate proxies, load balancers with idle timeout settings (often 60-120 seconds), and unstable WiFi connections commonly terminate these streams mid-response. The server sends chunks incrementally, and any break in the TCP connection causes data loss.

If That Doesn’t Work

  • Alternative 1: Use non-streaming client.messages.create() for critical requests — it buffers the full response server-side
  • Alternative 2: Run behind a stable wired connection if on WiFi
  • Check: Test your network stability with curl -N https://api.anthropic.com/v1/messages --max-time 300 to see if the connection holds

Prevention

Add to your CLAUDE.md:

Use streaming with reconnection logic for any response expected to exceed 500 tokens. Set HTTP timeout to at least 300s. Avoid streaming through proxies with aggressive idle timeouts.

Related articles: Claude API Timeout Handling, Claude API Error Handling, Claude Code Slow Response Fix

This fix also applies if you see variations of this error:

  • Connection or process errors with similar root causes in the same subsystem
  • Timeout variants where the operation starts but does not complete
  • Permission variants where access is denied to the same resource
  • Configuration variants where the same setting is missing or malformed

If your specific error message differs slightly from the one shown above, the fix is likely the same. The key indicator is the operation that failed (shown in the stack trace) rather than the exact wording of the message.

Frequently Asked Questions

Does this error affect all operating systems?

This error can occur on macOS, Linux, and Windows (WSL). The exact error message may differ slightly between platforms, but the root cause and fix are the same. macOS users may see additional Gatekeeper or notarization prompts. Linux users should check that the relevant system packages are installed. Windows users should ensure they are running inside WSL2, not native Windows.

Will this error come back after updating Claude Code?

Updates can occasionally reintroduce this error if the update changes default configurations or dependency requirements. After updating Claude Code, verify your project still builds and runs correctly. If the error returns, reapply the fix and check the changelog for breaking changes.

Can this error cause data loss?

No, this error occurs before or during an operation and does not corrupt existing files. Claude Code’s edit operations are atomic — they either complete fully or not at all. However, if the error occurs during a multi-step operation, you may have partial changes that need to be reviewed with git diff before continuing.

How do I report this error to Anthropic if the fix does not work?

Open an issue at github.com/anthropics/claude-code with: (1) the full error message including stack trace, (2) your Node.js version (node --version), (3) your Claude Code version (claude --version), (4) your operating system and version, and (5) the command or operation that triggered the error.

Configure MCP → Build your server config with our MCP Config Generator.

Try it: Paste your error into our Error Diagnostic for an instant fix.