Claude Code shows 'API Error: Connection closed mid-response' on long responses: is there a workaround?
Claude Code stops partway through long responses or long agentic sessions with 'API Error: Connection closed mid-response. The response above may be incomplete.' The reports cover WSL2, native Linux, macOS, Windows on ARM, the CLI, the VS Code extension and the desktop app. Is there a way to prevent or recover from these drops?
Originally asked on GitHub: https://github.com/anthropics/claude-code/issues/69415
4 answers
Status: the issue was closed as a duplicate, and this thread records no maintainer fix or explanation. Later comments say the error still happens, so treat the underlying problem as unresolved.
One commenter reported a workaround: they set API_TIMEOUT_MS and CLAUDE_STREAM_IDLE_TIMEOUT_MS both to 36000000 ms (10 hours). They said long generations then ran for hours without the error. They did not say which variable made the difference, so setting both is the tested option.
Another commenter inspected the installed CLI v2.1.186 with strings and listed undocumented stream watchdog settings: CLAUDE_ENABLE_BYTE_WATCHDOG (reported default on, idle window about 3 minutes, tunable with CLAUDE_BYTE_STREAM_IDLE_TIMEOUT_MS) and CLAUDE_ENABLE_STREAM_WATCHDOG (reported default off). These names were not tested or confirmed in the docs and may change between releases.
According to the thread, a dropped turn is not resumed automatically, so you have to type continue and re-establish context yourself.
General advice: set the variables in your shell environment, restart Claude Code, and retry the long task.
Did this work for you?What @Imyamoe tried on GitHub (Worked)
Set API_TIMEOUT_MS and CLAUDE_STREAM_IDLE_TIMEOUT_MS both to 36000000 ms. The commenter reported long generations running for hours afterward.
Source: https://github.com/anthropics/claude-code/issues/69415#issuecomment-4871191651 (opens external site)
Did this work for you?What @delor tried on GitHub (Outcome unclear)
Inspected the installed CLI v2.1.186 with strings and listed the undocumented watchdog variables CLAUDE_ENABLE_BYTE_WATCHDOG, CLAUDE_ENABLE_STREAM_WATCHDOG and CLAUDE_BYTE_STREAM_IDLE_TIMEOUT_MS. Did not test changing them.
Source: https://github.com/anthropics/claude-code/issues/69415#issuecomment-4777588452 (opens external site)
Did this work for you?What a GitHub user tried on GitHub (Outcome unclear)
Reporter ran long multi-file read and report tasks in Claude Code on WSL2 with the VS Code extension and hit the mid-response drop repeatedly, with no proxy or firewall. No fix was tried in the issue body.
Source: https://github.com/anthropics/claude-code/issues/69415 (opens external site)
Did this work for you?