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 (a contributor on #1581: "As of 0.36, we don't support implicit login.").
- Very long sessions: a collaborator 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; a contributor 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 (a collaborator 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 (a collaborator on #3924).
- Fix 3.
Update to the latest Codex release; a contributor closed #3924 after saying the context-window error is now properly handled, 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 Training Setting Re-Enabled? Two Switches to CheckDevelopers report Codex model training turning itself back on. What OpenAI documents, the second "Include environments" switch most people miss, and a five-minute check.
- OpenAI Pando: The Git Backend Built for Codex-Scale PushesOpenAI built Pando, a Git-compatible backend, after Codex flooded its monorepo with pushes. What it changed, the trade-offs, and what it means for your agent fleet.
- T3 Code: Open-Source GUI for Claude Code and CodexT3 Code is a free, MIT-licensed desktop, web and mobile app that drives Claude Code, Codex, Cursor, OpenCode and more. What it is, how remote access works, and what to check first.
- Android Studio Now Runs Claude and Codex: ACP Setup GuideAndroid Studio Rabbit 2 hosts 40+ ACP coding agents, Claude and Codex included, and Android CLI ships as a Claude Code and Codex plugin. Setup and caveats.
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 (a collaborator 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/6107b9886c114702e5c1cfd1c1b54f7c48bfaf49/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.