Skip to content

Codex: stream error when OPENAI_API_KEY and/or OPENAI_BASE_URL are set (ChatGPT signin) #3367

Closed

Description

@diegochine

What version of Codex is running?

codex-cli 0.31.0

Which model were you using?

gpt-5

What platform is your computer?

Darwin 24.6.0 arm64 arm

What steps can reproduce the bug?

  • Login to Codex via ChatGPT
  • Have the OPENAI_API_KEY environment variable set (e.g. in .zprofile)
  • Launch Codex and send any query

What is the expected behavior?

The query should go through normally.

What do you see instead?

Any query to Codex fails with error: ⚠️ stream error: exceeded retry limit, last status: 401 Unauthorized; retrying

Additional information

Very similar and likely related to #2896 .

Sample outputs:

  • When API key is not set:
Last login: Tue Sep  9 14:09:30 on ttys022
➜  ~ if [ -n "$OPENAI_API_KEY" ]; then
echo "OpenAI API key is set"
fi

➜  ~ codex

>_ You are using OpenAI Codex in ~

 To get started, describe a task or try one of these commands:

 /init - create an AGENTS.md file with instructions for Codex
 /status - show current session configuration and token usage
 /approvals - choose what Codex can do without approval
 /model - choose what model and reasoning effort to use

▌test

> Hey! I’m here and ready. What would you like to work on?
Token usage: total=239 input=89 (+ 6144 cached) output=150 (reasoning 128)
  • When API key is set:
Last login: Tue Sep  9 14:10:27 on ttys022
➜  ~ if [ -n "$OPENAI_API_KEY" ]; then
echo "OpenAI API key is set"
fi

OpenAI API key is set
➜  ~ codex

>_ You are using OpenAI Codex in ~

 To get started, describe a task or try one of these commands:

 /init - create an AGENTS.md file with instructions for Codex
 /status - show current session configuration and token usage
 /approvals - choose what Codex can do without approval
 /model - choose what model and reasoning effort to use

▌test

⚠️ stream error: exceeded retry limit, last status: 401 Unauthorized; retrying
1/5 in 212ms…

⚠️ stream error: exceeded retry limit, last status: 401 Unauthorized; retrying
2/5 in 407ms…

⚠️ stream error: exceeded retry limit, last status: 401 Unauthorized; retrying
3/5 in 858ms…

EDIT: I noticed that the OPENAI_BASE_URL environment variable also causes the very same error.

Activity

  1. added
    bug
    on Sep 9, 2025
  2. aibrahim-oai commented on Sep 10, 2025

    @aibrahim-oai
    Contributor

    did you set preferred_auth_method to 'apikey'?

  3. diegochine commented on Sep 10, 2025

    @diegochine
    Author

    @aibrahim-oai no. ~/.codex/config.toml contains only a couple of projects with trust level, no other config.

  4. changed the title [-]Codex using OPENAI_API_KEY with ChatGPT signin[/-] [+]Codex: stream error when OPENAI_API_KEY and/or OPENAI_BASE_URL are set (ChatGPT signin)[/+] on Sep 12, 2025
  5. euforic commented on Sep 16, 2025

    @euforic

    Same issue here with a fresh container but v0.34.0 works fine with the api key.

  6. ggerve commented on Sep 16, 2025

    @ggerve

    Have same issue with v0.36.0
    Env variables OPENAI_API_KEY is set, but codex is not picking up this variable.

    v0.34.0 worked fine

    Only way to use API Key now it's to use command codex login --api-key...

  7. rr- commented on Sep 16, 2025

    @rr-

    Related: #3002
    Solution that worked for me: #1243 (comment)

  8. laiso commented on Sep 24, 2025

    @laiso

    I was using preferred_auth_method in my GitHub Actions workflow to run Codex on a fresh instance without any manual setup.

    Now that preferred_auth_method has been removed, I have to run codex login --api-key beforehand to avoid authentication errors. The API key itself is valid, but without this explicit login step I consistently get a 401 Unauthorized error. This makes the behavior feel odd, since previously the environment variable alone was enough.

  9. etraut-openai commented on Nov 9, 2025

    @etraut-openai
    Contributor

    The login mechanism has been changed since this bug was submitted. The CLI no longer supports implicit login using an OPENAI_API_KEY environment variable. You now need to explicitly log in using codex login --api-key or going through the TUI-based login menu.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions