Repository navigation
Description
Summary
OpenCode cannot call a custom OpenAI-compatible provider whose baseURL points at a LAN IP (e.g. http://192.168.1.1:8317/v1). The embedded Bun runtime fails at the TCP layer with ECONNREFUSED / FailedToOpenSocket, while Node.js, system curl, and other tools on the same machine connect successfully.
This is not a model/auth/config-schema issue. Setting HTTP_PROXY=http://127.0.0.1:7897 makes OpenCode work immediately (traffic goes to localhost proxy, which then reaches the LAN host).
Related: #28418, #2667, #10961, #26807
Environment
| Item | Value |
|---|---|
| OpenCode | 1.18.3 |
| OS | macOS 15.7.3 (Darwin 24.6.0), x86_64 |
| Binary | opencode-darwin-x64 (codesign Identifier=bun, Team 7FRXF46ZSN, Bun v1.3.14) |
| Proxy / VPN | Clash Verge Rev, system HTTP/HTTPS/SOCKS proxy 127.0.0.1:7897, TUN enabled (stack: gvisor, auto-route: true), DNS fake-ip |
| System proxy exceptions | includes 192.168.0.0/16, 10.0.0.0/8, 127.0.0.1, localhost |
| Provider | Custom baseURL to CLIProxyAPI / OpenAI-compatible gateway on LAN |
Config (minimal)
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"xai": {
"name": "xAI (CPA)",
"options": {
"baseURL": "http://192.168.1.1:8317/v1",
"apiKey": "sk-opencode"
},
"models": {
"grok-4.5": { "name": "grok-4.5" }
}
},
"openai": {
"options": {
"baseURL": "http://192.168.1.1:8317/v1",
"apiKey": "sk-opencode"
},
"models": {
"gpt-5.5": { "name": "gpt-5.5" }
}
}
},
"model": "xai/grok-4.5"
}Both xai/* and openai/* fail the same way when proxy env is unset.
Error (OpenCode log)
ERROR message="stream error" providerID=xai modelID=grok-4.5
error.error="AI_APICallError: Cannot connect to API: Was there a typo in the url or port?
(cause: Error: Was there a typo in the url or port?)"
Isolating with the embedded Bun binary (BUN_BE_BUN=1 on the OpenCode binary):
FailedToOpenSocket
path: "http://192.168.1.1:8317/v1/models"
errno: 0
code: "FailedToOpenSocket"
message: "Was there a typo in the url or port?"
Raw TCP:
bun net.connect(192.168.1.1:8317) → ECONNREFUSED
node net.connect(192.168.1.1:8317) → connected
Reproduction
- Run an OpenAI-compatible API on a LAN host (not localhost), e.g.
http://192.168.x.x:8317/v1. - Point OpenCode
provider.*.options.baseURLat that URL. - Ensure no
HTTP_PROXY/HTTPS_PROXYis set for the OpenCode process. - Run:
opencode run -m xai/grok-4.5 "hi"
# or
opencode run -m openai/gpt-5.5 "hi"- Observe connect failure.
Same-machine controls
| Client | Target | Result |
|---|---|---|
system /usr/bin/curl |
http://192.168.1.1:8317/v1/models |
200 |
Node.js fetch / net.connect |
same | OK |
Python urllib / raw socket |
same | OK |
OpenCode / embedded Bun fetch |
same | FailedToOpenSocket |
Embedded Bun net.connect |
192.168.1.1:8317 |
ECONNREFUSED |
Embedded Bun net.connect |
127.0.0.1:7897 |
OK |
Embedded Bun net.connect |
192.168.1.5:<local-server> (self LAN IP) |
OK when something listens |
Embedded Bun fetch with HTTP_PROXY=http://127.0.0.1:7897 |
http://192.168.1.1:8317/v1/models |
200 |
OpenCode with HTTP_PROXY/HTTPS_PROXY |
full opencode run |
works (model responds) |
Port scan on the gateway IP (illustrative):
Port on 192.168.1.1 |
Bun | Node |
|---|---|---|
| 80 | ECONNREFUSED | OK (if open) |
| 8317 | ECONNREFUSED | OK |
| 53 | ECONNREFUSED | OK |
| 22 / 443 (closed) | ECONNREFUSED | ECONNREFUSED |
So Bun cannot open TCP to other LAN hosts (here the gateway), while Node can. Loopback works for Bun.
Minimal Bun reproduction (using OpenCode binary as Bun)
OC="$(node -p "require('path').join(require('path').dirname(require('fs').realpathSync(process.execPath)),'..','lib/node_modules/opencode-ai/node_modules/opencode-darwin-x64/bin/opencode")"
# or absolute path to opencode-darwin-*/bin/opencode
# FAIL — direct
BUN_BE_BUN=1 "$OC" -e '
import { connect } from "net";
const s = connect(8317, "192.168.1.1");
s.on("connect", () => { console.log("connected"); s.end(); });
s.on("error", e => console.log(e.code, e.message));
'
# OK — via local HTTP proxy
HTTP_PROXY=http://127.0.0.1:7897 BUN_BE_BUN=1 "$OC" -e '
const r = await fetch("http://192.168.1.1:8317/v1/models", {
headers: { Authorization: "Bearer sk-opencode" },
});
console.log(r.status);
'Expected behavior
OpenCode should connect to a custom provider baseURL on a LAN IP whenever the host OS can reach that IP (as proven by curl/Node), without requiring a manual HTTP_PROXY workaround.
Actual behavior
Embedded Bun fails with ECONNREFUSED / FailedToOpenSocket before any HTTP status is returned. User-facing error is the generic “Was there a typo in the url or port?”.
Workarounds
- Export proxy env when launching OpenCode (works reliably on this setup):
export HTTP_PROXY=http://127.0.0.1:7897
export HTTPS_PROXY=http://127.0.0.1:7897
opencode-
Run the API gateway on localhost / SSH port-forward and set
baseURLtohttp://127.0.0.1:.... -
Note: top-level
opencode.jsonhas noenvfield for process-wideHTTP_PROXY(unlike Claude Codesettings.json), so users cannot declare the workaround in config alone.
Analysis / likely root cause
- Failure is at TCP connect in the embedded Bun runtime used by the OpenCode binary, not in provider JSON or API key handling.
- Node and system curl succeed to the same
host:portat the same time → host firewall / server not simply “down”. - HTTP_PROXY to 127.0.0.1 succeeds → OpenCode’s higher-level HTTP client is fine once Bun can open a socket to a reachable target.
- Environment also runs Clash TUN (gVisor). System proxy is enabled with LAN CIDRs in ExceptionsList, so “direct” LAN paths are expected; Bun’s direct path still gets
ECONNREFUSEDfor the gateway IP. - OpenCode CLI is not a
.app(Info.plist=not bound, codesign idbun), so it does not appear under System Settings → Privacy & Security → Local Network, and no Local Network prompt is shown. Even if macOS Local Network privacy is involved for some CLIs, users cannot grant it via the UI for this binary. - Overlaps with @ai-sdk/openai-compatible provider fails to connect to LAN addresses — FailedToOpenSocket errno:0 #28418 (LAN
FailedToOpenSocketwith embedded Bun). This report adds: rawnet.connectmatrix, gateway-wide refusal, Clash TUN/system-proxy context, and confirmed HTTP_PROXY workaround for fullopencode run.
Feature / fix suggestions
- Document that LAN
baseURLcan fail under VPN/TUN/proxy setups and documentHTTP_PROXYworkaround. - Consider supporting process
envinopencode.json(or provider-level proxy options) so users can set proxy without shell aliases. - Investigate Bun embedding / networking on macOS with Network Extensions / TUN (happy eyeballs, interface selection, comparison with standalone Bun).
- Clearer error message when
FailedToOpenSockethits a private IP (hint: proxy, localhost forward, VPN/TUN).
Additional notes
- Codex CLI and Claude Code on the same machine can use the same LAN gateway (Claude injects
HTTP_PROXYin settings; Codex’s network stack reaches the host). - Custom OpenSSL-based curl builds may also fail where system SecureTransport curl succeeds — another sign of stack-specific path issues, not a dead server.
Happy to provide more logs (opencode run --print-logs --log-level DEBUG) or test a patch build.
Activity
github-actions commentedon Jul 17, 2026
This issue might be a duplicate of existing issues. Please check:
@ai-sdk/openai-compatibleprovider fails to connect to LAN addresses withFailedToOpenSocket errno:0on macOS using embedded BunEHOSTUNREACH/FailedToOpenSocketwhen VPN Network Extension leaves orphaned virtual interfaces (closed, but closely related networking context)github-actions commentedon Sep 16, 2026
To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.