Skip to content

Cannot connect to API: The socket connection was closed unexpectedly #23123

Description

@kyler505

Description

When using OpenCode, API connections intermittently fail with:

Cannot connect to API: The socket connection was closed unexpectedly. For more information, pass verbose: true in the second argument to fetch()

This error surfaces raw fetch() internals to the user rather than providing a meaningful error message or automatic recovery. While similar issues exist (#21643 for Zen models on QEMU, #22803 for SGLang ECONNRESET during streaming), this issue covers the general case where the underlying connection drops for any provider — including standard OpenAI/Anthropic endpoints on stable networks.

The core problem is that OpenCode does not gracefully handle transient socket closures. Expected behavior would be:

  1. Automatic retry with backoff (not infinite loops like Streaming ECONNRESET / "socket connection was closed unexpectedly" with SGLang OpenAI-compatible provider during tool-using agent runs (Qwen3.5 + reasoning content) #22803)
  2. A user-facing error message that explains the failure without exposing Bun/fetch internals
  3. A way to configure timeout and retry behavior

Plugins

none

OpenCode version

1.4.6

Steps to reproduce

  1. Use OpenCode with any provider (OpenAI, Anthropic, or custom OpenAI-compatible)
  2. Run an extended session that involves multiple tool calls or streaming
  3. Intermittently the socket drops and the raw error message appears

This is difficult to reproduce deterministically as it depends on network conditions, but occurs regularly in practice.

Screenshot and/or share link

No response

Operating System

Linux (various distributions)

Terminal

various

Activity

  1. added
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on Apr 17, 2026
  2. github-actions commented on Apr 17, 2026

    @github-actions
    Contributor

    This issue doesn't fully meet our contributing guidelines.

    What needs to be fixed:

    • No issue template was used — please use one of the three required templates: Bug Report, Feature Request, or Question
    • The Bug Report template requires a proper Description field, reproduction steps, and system information (OS, version, etc.)

    Please edit this issue to address the above within 2 hours, or it will be automatically closed.


    This issue might be a duplicate of existing issues. Please check:

    If you believe this was flagged incorrectly, please let a maintainer know.

  3. github-actions commented on Apr 17, 2026

    @github-actions
    Contributor

    Thank you for updating the issue! It now meets our contributing guidelines and includes all the required information. We appreciate you taking the time to provide the reproduction context, version, and system details.

  4. annejan-svg commented on Jul 29, 2026

    @annejan-svg

    I face the same problem now. I has worked for a couple of hours and now the cannot connect to API Error. Installed version is 2.3.8 in Home Assistant. Any help is much appreciated.

  5. OmidMasomi1 commented on Oct 8, 2026

    @OmidMasomi1

    So what's the solutions fir this issue?

  6. mzhmmzhm commented on Oct 8, 2026

    @mzhmmzhm

    So what's the solutions fir this issue?

    Try uninstalling and installing, i did it with npm uninstall -g opencode-ai
    Then npm install -g opencode-ai
    If that doesnt work try deleting the .local folder in C:\Users\DELL if you're on windows. This would delete any configurations you set i believe. Api keys and everything. It used to work for me a few months dont know about now and the error message would come up after about a week or so, to which i did the same again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

coreAnything pertaining to core functionality of the application (opencode server stuff)

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions