跪拜 Guibai
← Back to the summary

Why Codex Waits for 5/5 Reconnects: Retry Budget, WebSocket Fallback, and Proxy Routing

Why Your Codex Only Starts Working After Reconnecting to 5/5? Understanding Retries, Protocol Fallback, and the Proxy Chain

Author: AAA爱学习的刘哥

Codex reconnection mechanism: distinguish retries, transport fallback, and proxy routing

Some Codex users see this: after sending the first message, the UI repeatedly shows Reconnecting…, the count approaches 5/5 before it starts responding; subsequent interactions in the same session return to normal.

A common explanation is that the first five attempts don't go through the proxy, and only after they fail does it switch to the HTTP proxy. That judgment conflates transport protocol with network routing. To explain where the wait happens, three questions need to be looked at separately: what the client is retrying, when it switches transport, and whether different transports use the same network configuration.

01 "5/5" indicates a retry budget, not a startup sequence

Reconnecting means the client is trying to restore a connection or response stream. The count describes recovery progress, and does not mean the model needs to complete five connections before it can start working.

OpenAI's Configuration Reference distinguishes two configurations: request_max_retries controls HTTP request retries, with a default of 4; stream_max_retries controls retries after a streaming interruption, with a default of 5. They operate at different stages and cannot be mixed into a single count.

This also means that 5/5 alone cannot determine the root cause. Connection establishment failure, response stream interruption, or retryable service errors can all enter the recovery logic. You need to combine the error type and subsequent transport changes to judge.

The retry count is also not the total connection count. The first attempt is usually not counted as a "retry", and the client may also include steps like pre-connection or auth recovery, so you should not treat the UI count as the connection count in packet captures.

The wait time is influenced by connection timeout, retry intervals, and the server's backoff hints. A failure may return quickly or wait until timeout; there is also a pause between retries. Therefore, the same 5/5 display can produce very different actual wait times in different environments.

02 A verifiable path: WebSocket failure falls back to HTTPS

For the phenomenon of "long wait on the first round, recovery after reconnect, and normal subsequent interactions", transport fallback is a path worth checking.

In OpenAI Codex version 0.166.0, the model provider configuration includes WebSocket support capability; the corresponding streaming error handling code checks the retry budget. When the condition is met and transport can still be switched, the client switches to the HTTP path and produces the following hint:

Falling back from WebSockets to HTTPS transport.

It then resets the relevant retry counts and continues to attempt the request. This behavior can be verified in the streaming retry implementation of that version. It shows that some of the startup wait comes from failure recovery, not because the model has already started processing a task but is slow to output.

Conditional transport fallback: after WebSocket retries are exhausted, use HTTPS

Figure 1: A recovery path verified against source code; not all connection errors go through a full five retries.

Another detail explains why subsequent rounds may become faster: this version keeps the HTTP fallback state for the current session. Once fallback is enabled, subsequent rounds continue to use HTTP, and do not re-attempt WebSocket each time. New sessions need to be re-evaluated. Session-level transport state

This is a mechanism explanation for the phenomenon; specific failures still need to be confirmed by logs. Some errors can trigger an earlier switch, and some errors terminate directly; depending on version, provider capabilities, and enabled features, the recovery process may differ. You cannot treat "fixed five retries then fallback" as a protocol guarantee for all Codex clients.

03 Protocol fallback is not the same as enabling the proxy only at the fifth attempt

When discussing network issues, three levels need to be distinguished.

Level Problem it solves Typical content
Model API What task is submitted, what result is received Responses request, text events, tool calls
Transport How requests and events are exchanged WebSocket, HTTPS + SSE
Proxy routing Which network path the data goes through Local proxy, enterprise gateway, direct connection and bypass rules

SSE (Server-Sent Events) is a way to continuously push events within an HTTP response. WebSocket establishes a persistent, bidirectional message channel. Both can carry text and tool events during model generation, and both can go through a proxy.

Relationship between model API, transport, and proxy routing

Figure 2: Switching between WebSocket and HTTPS/SSE is not the same as switching between "direct" and "proxy".

One possible failure is that HTTPS streaming requests work normally, but WebSocket handshake, connection maintenance, or forwarding fails. Another possibility is that the two transport paths use different network libraries or configurations, causing inconsistent proxy behavior. These are hypotheses that need verification; the UI retry count alone cannot prove any of them.

It is also necessary to distinguish the CONNECT method in proxies. It is commonly used to establish a tunnel to the target service, and belongs to a different level than the UI's Reconnecting count. "Going through an HTTP proxy" also does not mean that the request body is using plaintext HTTP.

Therefore, the more accurate question to debug is: Did the WebSocket path fail, did the fallback HTTPS path succeed, and what network configurations did each go through. Separating these three questions makes it possible to determine whether to fix the proxy, gateway, or client configuration.

04 Why encoding agents need WebSocket

Coding tasks typically involve multiple rounds of model and tool interaction: the model reads a file, proposes a change, runs tests, and then continues based on the results. Each round needs to exchange new input and execution results, and transport overhead accumulates in long call chains.

OpenAI's WebSocket Mode explains that the Responses API's WebSocket mode is suitable for tool-call-intensive workflows. A persistent connection combined with incremental input can reduce the overhead of multi-round transitions; requests still use Responses semantics, submitted via response.create events.

The benefit comes from the combination of transport and server-side state management. It does not mean that HTTP must handshake every round and transmit the full history, nor that only WebSocket can stream output.

HTTP can reuse connections, and SSE can continuously push responses. OpenAI's Conversation state also gives a way to continue response chains in HTTP mode using previous_response_id. So "HTTP is stateless, so every round must resend the full context" does not hold: the application layer can manage state itself.

The in-connection cache of WebSocket helps low-latency continuation, but after a connection is dropped, the context still needs to be recovered. The performance advantage depends on an available network link; if the handshake keeps failing, the wait for retries may exceed the transmission time it saves.

05 Troubleshooting order: first confirm the link, then modify configuration

Record which stage the phenomenon occurs at

Distinguish between the first request, after a long idle period, and after an execution interruption. If the first round fails and subsequent rounds are normal, session-level fallback is worth checking; if the stream breaks after output has started, also look at intermediate device timeouts, service errors, or network fluctuations.

Also record the client type, version, login method, and the actual Provider (model provider) in use. Desktop, CLI (command line tool), IDE (integrated development environment) plugins, and remote environments do not necessarily inherit the same environment variables or proxy settings.

If the log shows a clear WebSocket failure and HTTPS fallback hint, you can locate the transport switch. If it shows authentication failure, quota, or certificate errors, handle those first; do not attribute everything to WebSocket.

Then verify the proxy actually used by the process

The browser being able to connect only means the browser's path is available; settings in the terminal do not automatically change an already-started desktop process.

For CLI tests that need an explicit proxy, you can set environment variables in the current PowerShell session and then start Codex from the same session:

# Example port; replace with the HTTP proxy port your local proxy actually provides
$env:HTTP_PROXY = "http://127.0.0.1:7890"
$env:HTTPS_PROXY = "http://127.0.0.1:7890"
codex

This sets only the current terminal environment. To confirm that requests truly go through the proxy, also use proxy logs and check bypass rules like NO_PROXY. Desktop, IDE, WSL (Windows Subsystem for Linux), or remote hosts need to be verified separately in their process environments; in a remote environment, 127.0.0.1 points to the remote machine itself.

Finally use transport comparison to narrow the scope

For a custom Provider that is fully defined and currently in use, you can adjust in the original configuration table according to the official configuration reference:

# Inside the existing [model_providers.<actual provider ID>] table
supports_websockets = false

This is a diagnostic adjustment for the current provider, not a universal switch that should be unconditionally appended to the top of the config. Save the original value before modifying, restart as required by the client, and observe. If the wait disappears, the WebSocket-related path is worth further investigation; this still cannot alone determine whether the proxy is not effective or the gateway forwarding failed.

When using built-in providers or desktop-managed connections, follow the configuration approach supported by the corresponding version; do not blindly switch providers just to test transport. Other switches circulating online should also be checked against documentation and config hierarchy; reducing retry counts cannot fix a link that is fundamentally broken.

06 Claude Code cannot directly reuse the same set of retry counts

Claude Code also needs to handle streaming requests and network failures, but model requests, MCP connections, and remote sessions belong to different channels; you cannot derive the retry count for the whole product from one channel.

Anthropic's Streaming messages clearly states that the Messages API uses SSE to stream text, tool calls, and thinking increments. Just because Claude Code has WebSocket-related features does not mean all its model interactions follow a fixed structure of "WebSocket downlink, HTTP uplink".

Its Enterprise network configuration also explicitly supports standard proxy environment variables and explains the config scope for scenarios like desktop-managed connections. When troubleshooting, check the specific channel, runtime environment, and version, rather than assuming it shares the same "five retries fallback" mechanism as Codex.

07 Conclusion

Codex reconnecting to 5/5 before starting to work could correspond to the path of exhausting WebSocket retries and recovering after switching to HTTPS. Session-level fallback state can also explain some cases of "first round slow, subsequent rounds normal".

Determining the cause still requires error messages, transport changes, and proxy logs to support it together. The retry count describes the recovery process, transport fallback determines how data is exchanged, and proxy configuration determines where data goes. Separating these three levels turns repeated waiting into a verifiable, fixable problem.

Comments

Top 1 from juejin.cn, machine-translated. The original thread is authoritative.

用户21607634083

The '5/5' part hits the nail on the head. Here's a similar trap I hit over the past couple of days: in Node, the fetchOptions.timeout at the simple-get layer doesn't actually take effect when passed down. It hangs for a solid ten-plus seconds and then gives 'Request timed out'. Packet capture shows TCP already connected — it's stuck on reading the response body. I finally switched to my own node:https plugin, using Buffer.concat to collect everything, explicitly setting content-length, and stretching req.setTimeout, and that stabilized it. So the retry counter on the UI almost always points to the application layer; the real pit is usually down at the socket layer.