Fix Network Error / Connection Failed in Claude Code and Codex CLI
Step-by-step fixes for the "Network Error / Connection Failed" error when connecting Claude Code or Codex CLI to DeepSeek, OpenRouter, and other providers.
Error output
Common variations
The exact text varies by agent version and provider. Match the status code and the message above.
Quick diagnosis
Isolate the network layer from the config: if curl fails from the same shell, the problem is network, not configuration.
curl -I https://api.deepseek.com/anthropic curl -I https://api.deepseek.com Common causes
- The machine cannot reach the provider endpoint (DNS, routing, or connectivity).
- An intercepting proxy, firewall, or VPN rewrites or blocks the request.
- Regional restrictions block direct access to the provider host.
- The network changed (new Wi-Fi, VPN session ended) since the last successful run.
Fix by scenario
1 curl fails from the same machine
- Check DNS: `nslookup api.deepseek.com` should resolve.
- Review proxy and firewall rules; try a different network to confirm.
- Some regional networks block direct access — a different route may be needed.
2 curl works but the agent fails
- CLI traffic can be blocked by rules browsers bypass (MITM proxies, sandboxed networks).
- Run the failing command from the same shell that has the environment variables.
- Check for a local proxy variable (http_proxy/https_proxy) that the agent honors but curl -I ignores.
3 Fails after a network change
- Reconnect or switch networks and retry.
- If a VPN was active, confirm it is still up and routing provider traffic.
- Corporate networks often need an allowlist entry for the provider host.
Agent-specific fixes
Claude Code
- Fix
- Confirm ANTHROPIC_BASE_URL is reachable from the current network; adjust proxy/firewall rules or switch networks. Use the exact endpoint from the setup guide — third-party mirrors are not supported.
- Verify
- `curl -I <base url>` succeeds from the same machine and network.
Codex CLI
- Fix
- Verify connectivity to the base_url host and confirm the endpoint is reachable from the network that runs Codex.
- Verify
- `curl -I https://api.deepseek.com` (or https://openrouter.ai) succeeds, then `codex exec "hi"`.
Claude Code vs Codex CLI at a glance
| Claude Code | Codex CLI | |
|---|---|---|
| Connectivity test | curl -I <anthropic url> | curl -I <openai url> |
| Proxy variables | http_proxy/https_proxy honored | http_proxy/https_proxy honored |
| Mirror support | No third-party mirrors | No third-party mirrors |
Prevent it from happening again
- Keep a known-good network path documented for the provider hosts.
- Pin DNS or document the resolved IPs if your network DNS is flaky.
- Test connectivity with curl before long sessions after any network change.
Related errors
Related setup guides
Frequently asked questions
The site works in a browser but the CLI fails — why?
CLI traffic can be blocked by rules that browsers bypass, such as an MITM proxy that allows browser traffic but drops CLI connections, or a sandboxed network for terminal apps. Test with curl from the same shell.
Does the agent respect proxy environment variables?
Both Claude Code and Codex honor standard proxy variables (http_proxy, https_proxy, no_proxy) when resolving connections, which is why the curl test should run in the same shell as the agent.