Claude Streaming Backpressure Timeout
Anthropic closes your streaming connection because your client isn't reading fast enough. When per-token processing (DB writes, LLM chains, slow UI updates) blocks the read loop, TCP buffers fill and Anthropic disconnects. Decouple reading from processing.
Never do slow work inside the stream read loop. Push each chunk to an asyncio.Queue (or Node stream), consume it in a separate task. If your DB write / downstream API / heavy UI update takes >100ms per chunk, batch or offload it. Never do await slow_thing(chunk) before reading the next chunk.
The Error Messages You're Seeing
anthropic.APIConnectionError: Connection aborted → Anthropic closed the socket; you were too slow httpx.ReadError: [Errno 104] Connection reset by peer → TCP RST from server after buffer fill timeout asyncio.exceptions.IncompleteReadError → Stream ended before message_stop event Stream stalls after ~30 seconds of processing → Your per-chunk processing exceeded read cadence Response cuts off at random points during long generation → Backpressure disconnect, not model completion
Why This Happens
requests.post() inside async loop halts event loop.Fixes That Work (Tested Nov 2026)
1Producer-Consumer Pattern (Python asyncio)
2Node.js Streams with Backpressure Handling
3Server-Sent Events to Browser (Backend Proxy)
X-Accel-Buffering: no header matters
Nginx and other reverse proxies buffer responses by default. This kills streaming UX and can cause backpressure timeouts on the Anthropic side if your proxy blocks. Set X-Accel-Buffering: no in your streaming responses, or configure proxy_buffering off in nginx.
Preventing This Error Going Forward
- Read as fast as possible. Never block the read loop.
- Push chunks to a queue. Separate consumer processes at its own pace.
- Batch DB writes. One write per 20 chunks or 200ms is fine.
- Fire-and-forget non-critical side effects. Log failures, don't await.
- Disable proxy buffering. nginx, Cloudflare, corporate proxies can all interfere.
- Set read timeout > expected generation time. Long responses need patience.
- Monitor read loop cadence. If chunks arrive >500ms apart, investigate.
Researcher · AI Error Hub
Frequently Asked Questions
Not officially published, but in practice ~30-60 seconds of no reads triggers disconnection. Under heavy load or with tight throughput budgets, it can be shorter. Design as if you have 5 seconds of tolerance to be safe.
No. Non-streaming waits for the full response server-side then delivers as one HTTP body. Backpressure only affects streaming (SSE) responses where slow consumers can fill TCP buffers.
Yes, whatever tokens were generated up to the disconnect point are billed. Use prompt caching + non-streaming for expensive prompts if backpressure is a persistent issue.
Check for message_stop event. If the stream ends without it, that's abnormal. Also check for HTTP-level errors (RST, connection aborted). The SDK raises APIConnectionError on unexpected close.
Yes, but fix the root cause first. Retrying without changing your read loop will just fail again. Add queuing + batching, then retry with exponential backoff.
Related Errors
Build Production-Grade Streaming
Weekly deep dives on backpressure, queues, and streaming UX patterns. 12,000+ developers subscribe.
Subscribe Free →