Skip to main content
Watch: I Built an AI App Without Leaving Databricks

OpenAI Codex CLI error

Codex CLI: stream disconnected before completion: stream closed before...

Error message
stream disconnected before completion: stream closed before response.completed

Status: 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

  1. 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).

    Snippet
    printenv OPENAI_API_KEY | codex login --with-api-key
  2. Fix 2.

    Start a new chat or session instead of re-using one very long session (maintainer tibo-openai on #3924).

  3. 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.

  4. Fix 4.

    Optional tuning documented in the configuration reference: retry and idle-timeout settings for a model provider.

    TOML
    request_max_retries = 4
    stream_max_retries = 10
    stream_idle_timeout_ms = 300000

More Codex CLI errors

All 14 Codex CLI errors

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.

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.