[BUG] API Error: Connection closed mid-response — occurs immediately in new context window

Status Open
Reported on v2.1.173
Maintainer reply None cached
Activity 19 comments · opened Jun 18, 2026

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

  1. Start Claude Code session (web chat via Agent SDK, self-hosted)
  2. Run a multi-step task involving multiple tool calls (file edits, bash commands, git operations)
  3. Allow the session to grow — especially after context summarization triggers
  4. Ask Claude to perform any task
  5. Observe thinking phase begins (tokens consumed)
  6. 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

View original on GitHub ↗

14 Comments

github-actions[bot] · 2 months ago

Found 3 possible duplicate issues:

  1. https://github.com/anthropics/claude-code/issues/66095
  2. https://github.com/anthropics/claude-code/issues/59243
  3. https://github.com/anthropics/claude-code/issues/57492

This issue will be automatically closed as a duplicate in 3 days.

  • If your issue is a duplicate, please close it and 👍 the existing issue instead
  • To prevent auto-closure, add a comment or 👎 this comment

🤖 Generated with Claude Code

bravoxx6 · 2 months ago

Same here

bravoxx6 · 2 months ago

Same here

mapo9k · 2 months ago

any fixes yet? it is getting more frequent

Freylaverse · 2 months ago

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.

tcc-andy-shaw · 2 months ago

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.

DevTK0 · 2 months ago
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?

braisfmitx · 1 month ago

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

WZZMMM · 1 month ago

Same here, and it happened more and more frequently. Windows. VS Code terminal / extension. v2.1.216-218.

bartmkamphuis · 1 month ago

Same here. Claude code is broken for me for days now.

xuhancn · 1 month ago

Same here. v2.1.220

nate456789 · 1 month ago

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" />

ykatyhoney · 26 days ago

this issue is resolved? I'm getting stuck on using claude here

ykatyhoney · 26 days ago

I resolved this issue by downgrading the Claude version to 2.1.220. So it's working now.

Showing cached comments. Read the full discussion on GitHub ↗