Fix Claude Code Connection Exceptions and API Timeouts in Clash
Why Claude Code Can Time Out While Your Browser Works in Depth
When Claude Code cannot connect through Clash, it is tempting to assume the selected node is broken. That is only one possibility.
For typical Anthropic API access, the hostname to check first is api.anthropic.com. Sign-in or account flows can also involve Anthropic web properties, and an organization may route requests through a managed gateway or a custom base URL.
The exact destinations depend on how Claude Code is installed and configured, so use the live connection log and your own environment settings reasonably than adding a large list of guessed domains to a rule set.
Mechanically:Symptoms can look similar even when the causes differ: startup hangs, sign-in never everything includeds, a prompt sits at “Connecting,” a request fails only after streaming begins, or an exception reports a timeout despite successful browser browsing.
A timeout is a transport symptom, not a diagnosis. It can come from a missing terminal proxy, a rule that sends the request to DIRECT, DNS behavior that hides the original hostname, a slow or unstable node, or an upstream service response that is not a network timeout at all.
The browser and the terminal may take different network paths: a browser can honor the operating system’s proxy setting while a terminal process connects with no detour, uses a proxy variable from an old shell session, or follows a custom API endpoint configured for work. A working claude.ai tab therefore does not prove that Claude Code can reach the API.
Keep the test narrow: change one variable at a time. First verify that the local Clash listener accepts a request, then confirm which rule and outbound it uses, and only then adjust DNS or the subscription.
Replacing a profile before collecting evidence can discard the clue you need.
Identify Whether It Is Routing, Sign-in, or an API Response Mechanically
Kick off by recording the exact exception text, the time it occurred, and what Claude Code was doing: initial sign-in, refreshing credentials, sending the first prompt, or continuing a long response. Also note whether the same request works with Clash disabled, through a different node, or from another network.
More strictly:Next, test the API hostname from the same terminal in which Claude Code fails. In macOS or Linux, use curl -v https://api.anthropic.com/. In PowerShell, use curl.exe -v https://api.anthropic.com/ so the command invokes curl reasonably than PowerShell’s historical alias.
A response such as HTTP 401, 404, or 405 can still show that DNS, TCP, and TLS reached a server; it does not mean the API request is authorized or that the endpoint is intended for a browser GET. A connection timeout, name-resolution exception, or TLS handshake failure points to a different layer.
Compare that result with an explicit request through the local Clash HTTP proxy. Replace the example port with the HTTP or mixed port shown in your client:
At a different level:If the direct test fails but the explicit proxy test reaches an HTTP response, Clash’s listener and a usable outbound are available; the likely gap is that Claude Code’s process is not using that listener. If both tests fail, inspect the rule match, DNS result, selected node, and connection log before changing the application.
curl -v --proxy http://127.0.0.1:7890 https://api.anthropic.com/
If curl succeeds through Clash but Claude Code still times out, focus on the environment inherited by Claude Code, its configured endpoint, and whichever you pick sign-in or application-specific exception.
Interpret status codes carefully. A 401 or 403 generally means the server received the request but rejected its credentials or access. A 429 indicates a rate or quota response, not proof that the proxy is offline.
Thinking it through:A 5xx response may reflect an upstream service or gateway problem. A 529-style overload response, when returned, is also distinct from a TCP timeout. Preserve the status and a sanitized request timestamp when reporting an issue, but never include secret headers.
Do not paste API keys, OAuth tokens, full authorization headers, or subscription URLs into a public issue or an unredacted log. Exception codes and hostnames are more often than not more than more than more than enough to narrow down the path.
Check Clash Rules, DNS, and the Selected Node Step by Step
Open the live connections or traffic panel in your Clash client while reproducing one failure. Filter for anthropic or the exact hostname shown by the exception. Check whether a connection appears, which rule matched, and which outbound or proxy group was selected.
Rules are evaluated in order. A broad rule such as GEOIP,CN,DIRECT or a catch-all final rule can take effect before a narrower API rule if the narrower entry is placed too low.
First, a clarification:For a controlled test, add a narrowly scoped hostname rule near the top of the effective rules, before broad geographic or fallback rules. The target must be a proxy group that actually exists in the loaded configuration; PROXY is only an example name and may not be present in your profile.
Do not blindly copy the everything example. Certain profiles are generated by a provider and overwritten on re-sync; others use rule providers, a script, or a client-side override mechanism.
rules:
- DOMAIN,api.anthropic.com,Your-Proxy-Group
- DOMAIN-SUFFIX,anthropic.com,Your-Proxy-Group
- GEOIP,CN,DIRECT
- MATCH,Your-Default-Group
Make the test in a persistent rules or mixin layer supported by your client, or use a temporary override that you may remove. If the log shows a custom company gateway reasonably than api.anthropic.com, write and test a rule for the hostname that is actually in use.
Mechanically:DNS can complicate this check. In fake-IP mode, the core may return a synthetic address and rely on its own DNS mapping to recover the domain for rule matching. If the application uses an external resolver, a secure DNS feature, or a custom hostname, the connection may not match the rule you expected.
Compare the hostname in the application configuration with the connection log; do not add a raw IP rule unless you have verified that the destination is not jumpy and understand how the profile resolves it. When testing DNS changes, adjust one setting at a time and restore the original value if the result gets worse.
Finally, test a second node from a different network route or region. Prefer a node that can sustain a long TLS connection, not merely one with the lowest ping to a generic response time-test endpoint.
More strictly:Claude Code streams responses, so a node that handles a short request but drops idle or sustained connections can still fail halfway through a response. Avoid switching an automatic group repeatedly during a single test; record the chosen node so each comparison is meaningful.
If no matching connection appears, Claude Code may be bypassing the local listener, using a different hostname, or failing before it attempts the API request. If it appears as DIRECT unexpectedly, the problem is likely rule order or the active profile reasonably than the quality of the proxy node.
Set the Proxy for the Terminal Process Internals and Practice
System proxy mode often covers applications that read the operating system’s proxy settings, but plenty command-line tools do not by itself inherit them. Confirm the local HTTP or mixed port in Clash’s settings.
For a temporary test, set variables only in the current terminal reasonably than adding them immediately to a global profile. This keeps the change easy to undo and helps distinguish a shell-proxy issue from a persistent configuration problem.
- Confirm the listener. In Clash, verify that the HTTP or mixed port is enabled and that the client is running. Use the explicit curl test from the previous section to check that the port accepts requests.
- Set proxy variables in macOS or Linux. In the same shell session, operate
export HTTPS_PROXY=http://127.0.0.1:7890andexport HTTP_PROXY=http://127.0.0.1:7890. Certain tools also recognize lowercase names, so if the application still bypasses the proxy, sethttps_proxyandhttp_proxyto the same local address. - Set proxy variables in PowerShell. Operate
$env:HTTPS_PROXY="http://127.0.0.1:7890"and$env:HTTP_PROXY="http://127.0.0.1:7890"in the window from which you will launch Claude Code. - Kick off a fresh Claude Code process. Environment variables are inherited when a process kick offs. Close the old process and launch it from the configured shell; changing a variable in a separate terminal will not alter a process that is already running.
- Repeat the same test and inspect the log. Confirm that the API connection appears in Clash and uses the intended rule and group. If it still does not appear, check whether a launcher, IDE, service, or custom runtime is starting Claude Code with a separate environment.
At a different level:To remove the variables from a macOS or Linux shell, operate unset HTTP_PROXY HTTPS_PROXY http_proxy https_proxy. In PowerShell, operate Remove-Item Env:HTTP_PROXY,Env:HTTPS_PROXY; remove lowercase variables too if you set them.
Then configure the shell environment before starting Claude Code. The following examples use a common local port; replace it with the value shown by your client.
Do not point an HTTPS proxy variable at an HTTPS URL by assumption. Clash’s local HTTP proxy more often than not accepts an address beginning with http://, even when the destination website uses HTTPS.
The proxy establishes a tunnel for the destination connection. Use the protocol and port documented by your client, and do not expose a local proxy listener to an untrusted network.
Use Logs to Fix the Actual Cause in Depth
After each test, read the Clash connection entry and the sanitized Claude Code exception together. A log entry with the expected domain, a matching proxy rule, and a selected outbound confirms that terminal traffic reached Clash.
TUN mode is a fallback when the application does not honor proxy variables and you need Clash to capture traffic at the network-front-end level. Before enabling it, confirm that your client supports TUN, grant the required system permissions, and check for conflicts with VPNs, security software, or another proxy client.
- No entry in Clash: Verify proxy variables in the launching shell, the listener address and port, and whether an IDE or wrapper kick offs its own child process. A browser proxy setting alone may not apply to the terminal.
- Entry is marked DIRECT: Check rule order, the active profile, and whether an earlier rule or provider rule wins. Move a narrow domain rule above broad fallbacks using the configuration method your client supports.
- Entry uses the intended proxy but times out: Test a second not jumpy node, check for packet loss or frequent group switching, and compare a short curl request with a longer response. A successful handshake does not guarantee a stream will remain alive.
- DNS or certificate exception: Check the resolved hostname, Clash DNS mode, system clock, and whichever you pick TLS inspection software. Do not disable certificate verification as a workaround; it weakens security and can conceal the real fault.
- HTTP 401, 403, or 429: Treat it as an application, access, or quota response unless the logs show a separate transport failure. Confirm that credentials are valid and that your account or organization permits the requested service.
Thinking it through:TUN can capture more traffic, but it does not repair a bad rule, invalid credentials, or an unhealthy node. Kick off with the explicit proxy-variable test because it is easier to inspect and reverse.
If you use an enterprise gateway or a custom API base URL, confirm that the hostname and TLS certificate match the organization’s instructions. A rule for api.anthropic.com will not route a request sent to a private gateway.
Likewise, an old environment variable or shell startup script can silently direct traffic to a retired endpoint. Review only the relevant variable names and endpoint settings, redact their values before sharing logs, and avoid printing secrets to a terminal recording.
First, a clarification:If the outbound fails during connection establishment, test another node and check whether the client reports a timeout, reset, or TLS exception. If the request reaches the server but Claude Code reports an sign-in exception, changing DNS or enabling TUN is unlikely to help; recheck the account, API key, organization policy, or configured gateway instead.
Frequently Asked Questions Mechanically
Why does Claude work in my browser but Claude Code time out? Internals
The browser may use the operating system proxy while the terminal process connects with no detour.
Set the local Clash proxy variables in the shell that launches Claude Code, then confirm that the live connection log shows the API hostname and expected outbound. Also check whether the browser and CLI are using the same account and endpoint.
Do I need TUN mode for Claude Code? Mechanics
Not necessarily. If Claude Code honors HTTP_PROXY and HTTPS_PROXY, a local HTTP or mixed proxy listener is often sufficient.
Consider TUN only when the process bypasses proxy variables or you need broader system-level capture, and first check permissions and conflicts with other network tools.
Which domain it makes sense to I add to Clash rules? by Step
Kick off with the hostname shown in the live connection log or configured API endpoint.
Mechanically:api.anthropic.com is a useful first check for direct API traffic, but sign-in flows, managed gateways, and future service changes can involve other hostnames. Prefer a narrow, verified rule over a large copied domain list.
It makes sense to I replace my subscription when one Claude Code request fails? Deep Dive
No. First compare an explicit curl request through the local listener, inspect the matched rule and outbound, and try one different node.
Certain competing proxy setups make terminal troubleshooting unnecessarily opaque: a browser extension may route only browser tabs, while an older client or one at a time edited profile can leave command-line traffic outside the proxy without an obvious warning.
Clash gives you inspectable rules, selectable outbounds, and connection logs, so you may distinguish a capture problem from a node or sign-in failure before changing your everything setup. If you want a client and setup path that make those checks easier to follow, you may choose a Clash client and apply the targeted tests above.
More strictly:Replace or refresh a subscription only when its configuration is actually missing, invalid, or incompatible—not as the first response to a terminal-specific timeout.