1. Scope, prerequisites, and a safer mental model
This guide is for developers who want to run OpenAI Codex CLI from a terminal while using Clash Verge as the local proxy layer. The goal is not to force every connection on your computer through one remote node. A more reliable setup separates developer traffic into predictable paths: OpenAI-related requests use a suitable proxy, local repositories and company services remain DIRECT, and ordinary domestic traffic does not inherit unnecessary latency.
Codex CLI is a terminal-oriented coding assistant. Depending on the release and account configuration, it may authenticate through a browser-assisted flow, contact OpenAI API endpoints, stream model output over long-lived HTTPS connections, and execute local commands inside the workspace after you approve them. These are different network and security events even though they appear as one command in the shell. A successful browser login does not automatically prove that the CLI process can reach its API endpoint, and a working API request does not prove that every tool, update, or authentication redirect is routed correctly.
Before changing Clash rules, confirm that you are using an official Codex CLI distribution and that your OpenAI account, organization, billing status, and regional availability meet the product requirements. Clash Verge can improve transport reliability, but it cannot grant account access, remove usage limits, or repair an invalid API key. If you are using Codex for employer code, review the applicable data-handling policy before sending source files or prompts through a third-party proxy node.
You should also treat every proxy node as infrastructure you do not fully control. Do not paste subscription URLs, API keys, access tokens, or complete private profiles into issue trackers or public support channels. Keep credentials in the environment, an operating-system credential store, or the authentication mechanism recommended by the CLI. The practical target is a clean chain: Codex process → Clash Verge local port → selected proxy group → OpenAI service.
2. Understand Codex CLI traffic before editing YAML
A common mistake is to search for one hostname, add a broad rule, and assume the setup is finished. Codex CLI may create traffic from several phases: installation or upgrade, login, API requests, streaming responses, telemetry or update checks, and browser callbacks. The exact endpoints can change between releases, account types, and authentication methods, so treat any hostname list as a starting hypothesis rather than a permanent certificate of correctness.
Begin with a small inventory. Write down the operating system, the shell, the installation method, the Codex CLI version, the authentication mode, the Clash Verge local port, and the profile currently selected in Verge. This matters because an integrated terminal can preserve an old environment snapshot, while a new terminal may read a different proxy variable. On macOS and Linux, shell startup files can also differ between interactive shells and shells launched by an IDE. On Windows, PowerShell, Command Prompt, and an IDE terminal may use different environment settings.
The browser is only one part of the test. Your browser may use the system proxy while the CLI uses a direct socket, or the reverse may happen if you export HTTPS_PROXY manually. A login page loading successfully therefore proves only that one browser process reached the relevant service. Test the actual Codex binary from the same terminal where you intend to work. If the CLI provides a diagnostic, version, login, or status command, run it before attempting a large coding task so the first failure is easier to interpret.
Long model responses also behave differently from ordinary web pages. Streaming requests keep a connection open while tokens arrive, and tool-enabled workflows may open several requests in sequence. A node that is acceptable for a quick web page can still produce a poor developer experience if it has unstable international routing, aggressive idle timeouts, overloaded bandwidth, or unreliable IPv6. Judge a node by time to first response, stream continuity, error rate, and recovery after several requests—not only by a single speed-test number.
- Installation traffic: confirm the package source and check whether the installer follows redirects.
- Authentication traffic: expect browser interaction, callback behavior, and possible account redirects.
- API traffic: verify that the CLI can establish TLS and receive a complete response.
- Streaming traffic: watch for responses that stop after the first few tokens or fail after an idle interval.
- Local execution: remember that shell commands run on your machine and are not made safe merely because the model request used a proxy.
3. Configure Clash Verge without over-broad proxying
Open Clash Verge and first confirm that the imported profile is valid. Check the profile name, update time, proxy groups, DNS mode, and the local mixed port shown by the client. The exact labels differ across Clash Verge builds, but the concepts remain similar: you need a local HTTP or mixed port that the CLI can reach, a working proxy group, and rules that place explicit OpenAI destinations above generic catch-all rules.
For a first test, select a stable proxy group manually instead of relying on an automatic latency group. Automatic selection is useful later, but it adds another variable while you are diagnosing authentication or streaming failures. Choose a node with consistent reachability and moderate latency. A nearby exit is not always the best choice; the important property is a complete and stable route to the service endpoint used by your account.
If you maintain your own Mihomo-compatible rules, keep the OpenAI-related rules narrow and readable. Do not begin with a rule that proxies every .com domain or every request from the operating system. Broad rules can unintentionally route Git hosting, package registries, internal dashboards, cloud consoles, and company SSO through the same relay. A minimal rule set is easier to audit and less likely to create a confusing mixed state.
rules:
- DOMAIN-SUFFIX,openai.com,OpenAI
- DOMAIN-SUFFIX,auth.openai.com,OpenAI
- DOMAIN-SUFFIX,oaistatic.com,OpenAI
- DOMAIN-SUFFIX,oaiusercontent.com,OpenAI
- DOMAIN-SUFFIX,api.openai.com,OpenAI
- MATCH,DIRECT
The example is deliberately conservative and should not be copied as a guarantee that every current Codex endpoint belongs to these domains. Verify actual requests in Clash Verge’s connections panel and update the list according to observed traffic and official documentation. If an endpoint is hosted under another OpenAI-controlled or service-specific hostname, add the narrowest suitable rule. Prefer DOMAIN or DOMAIN-SUFFIX for known destinations, and place those rules before MATCH.
You may also use a rule-provider approach if your profile already separates AI services into maintained providers. That can reduce manual editing, but it introduces a dependency on the provider’s update quality. Inspect the generated rules after an update, especially when a provider uses a very broad suffix. A convenient provider is not a substitute for checking where sensitive development traffic is actually going.
4. Connect Codex CLI to Clash Verge and verify each layer
There are two practical connection models. The first is system or TUN routing, where Clash Verge captures applications without requiring every program to understand proxy environment variables. The second is explicit environment configuration, where the terminal tells the CLI which local HTTP or mixed port to use. TUN mode can help with binaries that ignore proxy variables, but it also affects more applications and may interact with corporate VPN software, local containers, DNS settings, or security controls. Start with explicit variables when Codex supports them; use TUN only when you understand the broader effect.
Assume for illustration that Clash Verge exposes a local mixed port at 7890. In a Unix-like shell, you can set both uppercase and lowercase forms because different libraries inspect different names. Do not place a secret in these variables; they should point only to the local Clash listener.
export HTTP_PROXY=http://127.0.0.1:7890
export HTTPS_PROXY=http://127.0.0.1:7890
export ALL_PROXY=http://127.0.0.1:7890
export http_proxy="$HTTP_PROXY"
export https_proxy="$HTTPS_PROXY"
export all_proxy="$ALL_PROXY"
On PowerShell, the equivalent syntax is different:
$env:HTTP_PROXY = "http://127.0.0.1:7890"
$env:HTTPS_PROXY = "http://127.0.0.1:7890"
$env:ALL_PROXY = "http://127.0.0.1:7890"
Whether ALL_PROXY is useful depends on the CLI’s network library. Some applications interpret it as a SOCKS or generic proxy variable, while others support only HTTP-style variables. If the local port is a mixed port, use the URL format expected by your client. Do not assume that writing socks5:// against an HTTP-only port will work. If you are unsure, begin with HTTP_PROXY and HTTPS_PROXY, then consult the CLI’s current help output.
Verify the chain in layers rather than jumping directly to a complex agent task. First, confirm that Clash Verge is running and the selected port is listening. Next, make a harmless request through that port and watch the connections panel. Then run the Codex version or authentication command from the same terminal. Finally, make a small request that does not expose confidential source code. At every stage, compare the terminal result with the connection entry in Verge: the process, destination, rule, proxy group, and final node should match your expectation.
If the CLI opens a browser, keep the browser and terminal test separate. The browser may use system routing, while the CLI uses explicit variables. If a callback page succeeds but the terminal later reports an authorization or network error, inspect whether the token exchange occurred in the CLI process after the browser step. Restarting the terminal or IDE can also be necessary because child processes often inherit environment variables only at launch time.
5. Diagnose DNS, TLS, and streaming failures
When Codex fails, classify the symptom before changing several settings at once. A connection timeout usually points to routing, DNS, a blocked path, or a dead node. A TLS or certificate error suggests interception, an incorrect proxy protocol, a system clock problem, or a trust-store issue. An HTTP authentication error may be a credential or account problem rather than a Clash problem. A response that begins and then stalls is more consistent with stream handling, idle timeout, overloaded relay capacity, or a broken connection path.
DNS deserves special attention. Clash Verge profiles may use fake-IP, redirection, or enhanced DNS modes. These features can be useful, but they may expose compatibility problems with applications that perform their own resolution, validate address families, or cache results aggressively. If the browser works while the CLI fails, compare the process’s resolved destination and the rule shown in Verge. Do not randomly disable DNS protection across the entire system. Test one controlled change, record the result, and restore the safer configuration if it does not help.
IPv4 and IPv6 can produce misleading results. A system may prefer IPv6, while the selected node or upstream route handles IPv4 more reliably. If failures appear intermittent, compare multiple attempts and inspect whether the destination family changes. Disabling IPv6 globally is rarely the best first step; instead, use the client’s documented DNS or stack settings and verify whether the proxy path consistently reaches the service.
For streaming problems, avoid choosing a node solely because it reports the lowest ping. A relay may answer a short probe quickly but terminate long-lived HTTP connections or throttle bursts. Try another node in the same group, compare several small requests, and observe whether the stream ends cleanly. If every node fails at the same point, investigate the CLI version, account state, local firewall, or service-side status before editing more rules.
- Check the rule match: an unexpected
DIRECTresult means the hostname inventory or rule order is incomplete. - Check the proxy protocol: an HTTP environment variable pointed at a SOCKS-only port can fail before DNS or authentication begins.
- Check the process: an IDE-launched terminal may not share the environment of your normal shell.
- Check credentials independently: a valid route cannot fix an expired token, wrong organization, or unavailable model.
- Check logs without leaking secrets: redact authorization headers, cookies, API keys, and full request bodies before saving diagnostics.
6. Daily operation, maintenance, and practical access tips
Once the basic path works, make the configuration repeatable. Keep a short runbook containing the local port, the intended proxy group, the relevant rule source, the command used to set environment variables, and the date you last verified the setup. If you change profiles frequently, note which profile was active during a successful test. This prevents a common failure mode in which the same command appears to stop working simply because Verge silently switched to another profile or an outdated subscription.
Use separate terminal profiles or wrapper scripts for project work if that helps you avoid accidental leakage. A project-specific shell function can set the proxy variables for Codex sessions and leave ordinary shell commands unchanged. Conversely, if your organization requires all development traffic to use a managed VPN, do not bypass that requirement with a personal node. The technically successful route is not necessarily the authorized route.
Review logs after profile updates. Look for new hostnames, repeated retries, connections that unexpectedly use DIRECT, and unrelated destinations that entered the OpenAI group. Remove obsolete rules rather than accumulating broad suffixes forever. A smaller rule set reduces surprises when a proxy provider changes DNS behavior or when a package registry happens to share infrastructure with an AI service.
Protect the local proxy as well. A listener bound to 127.0.0.1 is safer than one exposed on every network interface. If Clash Verge offers an authentication or access-control setting for the controller or mixed port, use it according to the client’s documentation. Avoid exposing the external controller API on a public or shared Wi-Fi interface. Proxy access can become a path to unintended traffic if another device can reach the listener.
Finally, remember that proxying changes transport, not the trust model of AI-assisted coding. Review proposed file edits, inspect shell commands before approval, keep sensitive files outside the workspace when appropriate, and use least-privilege credentials. A stable connection makes Codex more usable, but careful review remains necessary when an agent can read files, modify code, or invoke local tools.
Compared with single-purpose VPN switches, which often route everything through one overloaded tunnel and provide little visibility into whether a terminal process actually followed the intended path, Clash Verge gives you observable connections, selectable nodes, explicit domain rules, and a practical way to keep Git, internal services, and Codex traffic separate. If you want that level of control across Windows, macOS, Linux, and other supported environments, the next step is to choose a compatible client and apply the routing method that fits your network policy.
Download Clash clients and start building with a more predictable Codex connection →
Ready to get started?
Choose a compatible Clash client, import your profile, and route Codex traffic with clearer control over nodes and rules.