OpenAI Codex CLI error
Codex CLI: stream disconnected before completion: stream closed before...
stream disconnected before completion: stream closed before response.completedStatus: WorkaroundLast verified 4 documented fixes6 sources
What it means
The response stream (SSE) from the model disconnected after the HTTP handshake succeeded but before the server emitted response.completed. Per the Codex source, the session loop treats this as a transient error and automatically retries the turn; the TUI shows retry attempts before giving up. The retry count for SSE streaming interruptions is model_providers.<id>.stream_max_retries (default: 5) and the idle timeout is stream_idle_timeout_ms (default: 300000).
Causes
- Since Codex 0.36.0 the OPENAI_API_KEY environment variable is no longer read implicitly; users relying only on the env var hit this error (maintainer aibrahim-oai on #1581: "As of 0.36, we don't support implicit login.").
- Very long sessions: maintainer tibo-openai asked affected users on #3924 whether they were re-using one long session and whether a new chat still reproduced it.
- An over-limit context previously surfaced as this stream error; maintainer etraut-openai closed #3924 saying the "Your input exceeds the context window of this model" error is now properly handled.
How to fix it
- Fix 1.
If you use an API key, log in explicitly so the key is stored in auth.json (maintainer tibo-openai on #1581, and the official auth docs).
Snippetprintenv OPENAI_API_KEY | codex login --with-api-key - Fix 2.
Start a new chat or session instead of re-using one very long session (maintainer tibo-openai on #3924).
- Fix 3.
Update to the latest Codex release; maintainers closed #3924 after the context-window error was handled properly, and asked anyone still affected to open a new issue with details.
- Fix 4.
Optional tuning documented in the configuration reference: retry and idle-timeout settings for a model provider.
TOMLrequest_max_retries = 4 stream_max_retries = 10 stream_idle_timeout_ms = 300000
More Codex CLI errors
All 14 Codex CLI errorsstream disconnected before completion: error sending request for url (https://chatgpt.com/backend-api/codex/responses)Status: OpenError running remote compact task: stream disconnected before completion: error sending request for url (https://chatgpt.com/backend-api/codex/responses/compact)Status: Fixed in releaseSelected model is at capacity. Please try a different model.Status: By designYour access token could not be refreshed because your refresh token was already used. Please log out and sign in again.Status: Fixed in releaseCodex ran out of room in the model's context window. Start a new thread or clear earlier history before retrying.Status: Fixed in releaseError logging in: Port 127.0.0.1:1455 is already in useStatus: WorkaroundToken exchange failed: token endpoint returned status 403 ForbiddenStatus: WorkaroundMCP client for `playwright` failed to start: program not foundStatus: Fixed in release
Related reading
- Do Your Agent Hooks Fail Open? Claude Code vs Codex vs Cursor vs Gemini CLIA guard hook that crashes, times out or points at a missing script lets the tool call through by default in Claude Code. v2.1.295 adds onFailure: block. Here is what each coding agent does when your policy hook breaks, with real test runs.
- Self-Improving Applications Are Now Cheaper Than Hiring: The Claude Code and Codex Closed LoopSelf-improving applications with Claude Code and Codex: the loop from user feedback to GitHub issue to reviewed PR, paying per closed issue, not per token.
- Codex CLI 0.163: Instant Steering Is Now the DefaultCodex CLI 0.163.0 makes mid-response steering the default and adds searchable /config and a leader key. What changed and what to check before upgrading.
- Codex 'gpt-5.5 Model Is Not Supported' Error: The FixCodex says gpt-5.5 is not supported with a ChatGPT account because OpenAI retired it from ChatGPT sign-in on October 14. The fix and what still works.
- Agent Harness Lessons From a 500B-Token Decompile: Give Agents a PASS/FAIL OracleAgent harness lessons from a 500B+ token decompile: a byte-matching PASS/FAIL oracle beat the reviewer agent. How agents cheated it and what to copy.
- Codex CLI Background Daemon: How to Turn It OffCodex CLI starts a persistent, self-updating background daemon since 0.157.0. What it does, why it hit Hacker News and the reported workaround to turn it off.
FAQ
What does "stream disconnected before completion: stream closed before..." mean in Codex CLI?
The response stream (SSE) from the model disconnected after the HTTP handshake succeeded but before the server emitted response.completed. Per the Codex source, the session loop treats this as a transient error and automatically retries the turn; the TUI shows retry attempts before giving up. The retry count for SSE streaming interruptions is model_providers.<id>.stream_max_retries (default: 5) and the idle timeout is stream_idle_timeout_ms (default: 300000).
How do I fix "stream disconnected before completion: stream closed before..."?
There are 4 documented fixes. Start with the first: If you use an API key, log in explicitly so the key is stored in auth.json (maintainer tibo-openai on #1581, and the official auth docs).
Is this Codex CLI error fixed?
Status: Workaround. No release removes the cause. The documented fixes work around it. Affected versions: Reported from codex-cli 0.7.0 (#1581); implicit OPENAI_API_KEY pickup was removed in 0.36.0
Sources
Last verified against 6 sources: official docs, maintainer replies and release notes.
- github.comhttps://github.com/openai/codex/issues/1581
- github.comhttps://github.com/openai/codex/issues/3924
- github.comhttps://github.com/openai/codex/releases/tag/rust-v0.36.0
- learn.chatgpt.comhttps://learn.chatgpt.com/docs/auth
- learn.chatgpt.comhttps://learn.chatgpt.com/docs/config-file/config-reference
- github.comhttps://github.com/openai/codex/blob/ce0323f2f2d3b25e82806cd555237da3db4e1897/codex-rs/protocol/src/error.rs
Newsletter
Fixes for the tools you use, weekly
One email per week. New coding agent releases, what broke, and how developers are working around it.