[BUG] API Error: Connection closed mid-response — occurs immediately in new context window
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
During active conversations, Claude Code enters a "thinking" phase (tokens are visibly consumed), but instead of returning a response, the following error appears:
API Error: Connection closed mid-response. The response above may be incomplete.
The error occurs randomly — sometimes after a few seconds of thinking, sometimes after longer processing. No response is returned. The issue appears more frequently in long sessions with many tool calls and after cont
What Should Happen?
Claude should complete processing and return a full response. If the connection is
interrupted, it should recover gracefully or at minimum display a retry option —
not silently drop the response after consuming tokens.
Error Messages/Logs
API Error: Connection closed mid-response. The response above may be incomplete.
Observed behavior:
- Thinking indicator is visible (tokens being consumed)
- Response begins generating (sometimes 1-2 sentences appear)
- Connection drops mid-response with the above error
- No partial output is recoverable
- Happens randomly — not tied to response length
Steps to Reproduce
- Start Claude Code session (web chat via Agent SDK, self-hosted)
- Run a multi-step task involving multiple tool calls (file edits, bash commands, git operations)
- Allow the session to grow — especially after context summarization triggers
- Ask Claude to perform any task
- Observe thinking phase begins (tokens consumed)
- Error appears: "API Error: Connection closed mid-response"
Reproducibility: random, but significantly more frequent after context summarization.
Not reproducible on demand, but occurs multiple times per long session.
Claude Model
Sonnet (default)
Is this a regression?
Yes, this worked in a previous version
Last Working Version
no response
Claude Code Version
2.1.173
Platform
Anthropic API
Operating System
Ubuntu/Debian Linux
Terminal/Shell
VS Code integrated terminal
Additional Information
- Self-hosted Claude Code Agent SDK on Debian 13 (trixie), x86_64
- Claude Code version: 2.1.173
- Accessed via custom web chat over WebSocket (uvicorn/FastAPI)
- Error occurs both mid-generation and immediately at start of new context window
after summarization
- Workaround: starting a completely new conversation avoids the issue temporarily
- Tokens appear to be consumed during the failed thinking phase (no refund observed)
- No network instability on the host — other services unaffected
Showing cached comments. Read the full discussion on GitHub ↗
14 Comments
Found 3 possible duplicate issues:
This issue will be automatically closed as a duplicate in 3 days.
🤖 Generated with Claude Code
Same here
Same here
any fixes yet? it is getting more frequent
I am now having this issue on almost every message I send, even if it is the first one in a thread. I do not believe it is an issue with my own internet connection.
I recently started getting this error when I configured claude to use a gateway (litellm). It appears that the reverse proxy for my litellm docker has a timeout (60 seconds) and the responses through the gateway routinely take much longer than that. Anything over 60s gives me the API error. If I swap back to a bedrock inference profile or an Anthropic subscription the errors go away. I am not sure if the timeout is related and I haven't had a chance to change the ALB settings, but maybe it will help others debug the issue.
We are also using Litellm and some of our users are facing this issue. Is everyone here who is encountering this issue also using Litellm or does this issue also occur with other providers as well?
Same here, in my case i use claude code with litellm and this seems to be happening whenever claude code need to write down a big file, like a tech plan in one operation. Whenever it occurs in consistently happens to me, i have to instruct claude to write down the file in parts and then join it. its the only way i find to bypass the issue
Same here, and it happened more and more frequently. Windows. VS Code terminal / extension. v2.1.216-218.
Same here. Claude code is broken for me for days now.
Same here. v2.1.220
Constantly get this and it wastes so many tokens.
What is going on with this service? Doesn't matter if it is desktop or CLI
<img width="800" height="2804" alt="Image" src="https://github.com/user-attachments/assets/15e01a9d-5726-4499-9364-3f42269f6334" />
this issue is resolved? I'm getting stuck on using claude here
I resolved this issue by downgrading the Claude version to 2.1.220. So it's working now.