[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
This issue has 7 comments on GitHub. Read the full discussion on GitHub ↗