codex icon indicating copy to clipboard operation
codex copied to clipboard

stream error: exceeded retry limit, last status: 401 Unauthorized

Open rahulkarajgikar opened this issue 1 year ago • 38 comments

What version of Codex is running?

0.27.0

Which model were you using?

gpt-5-medium (default)

What platform is your computer?

Darwin 24.1.0 arm64 arm

What steps can reproduce the bug?

log in with API key

run any command

What is the expected behavior?

should work properly and respond back

What do you see instead?

this error

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

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

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

⚠  stream error: exceeded retry limit, last status: 401 Unauthorized; retrying 4/5 in 1.755s…

⚠  stream error: exceeded retry limit, last status: 401 Unauthorized; retrying 5/5 in 3.087s…

i have confirmed the API key works fine

Additional information

No response

rahulkarajgikar avatar Aug 29 '25 09:08 rahulkarajgikar

found the bug:

it was because .env in my repository was configured to an invalid key

it was unclear because there were no logs and no clear way to debug this, echo $OPENAI_API_KEY was showing the correct key

once i fixed the key in my .env, it worked fine

rahulkarajgikar avatar Aug 29 '25 09:08 rahulkarajgikar

I had Claude diagnose the problem, it turns out that while /status showed that I was logged in, the credentials had expired, so you need to log-in again.

migueldeicaza avatar Aug 30 '25 14:08 migueldeicaza

This should be fixed! I logged in with my ChatGPT account, but the .env file was used for application/project development. It shouldn’t automatically use it, as it consumes my application/project token.

At the very least, it should notify me that I’m using the API key instead of my ChatGPT account. Alternatively, it could simply give me a choice at the start-up.

acgxv avatar Sep 02 '25 07:09 acgxv

I agree it shouldn't use .env and should use configured environment variable instead

rahulkarajgikar avatar Sep 02 '25 07:09 rahulkarajgikar

At least /status should display "Hey, I am getting my config from here"

migueldeicaza avatar Sep 02 '25 15:09 migueldeicaza

Same issue here, connected Codex via ChatGPT account but getting 401. I had no .env in my workspace, but I was still exporting OPENAI_API_KEY in ~/.zprofile. Removing the export solved the issue.

In this case, it definitely shouldn't use the OPENAI_API_KEY, given I signed in using the ChatGPT account.

diegochine avatar Sep 03 '25 11:09 diegochine

@diegochine What version of the CLI are you using? The latest version of the CLI is 0.29. Codex will ask you if you want chatgpt/api key login if you have an api key set on first launch.

Image

gpeal avatar Sep 03 '25 17:09 gpeal

@gpeal I'm on 0.29. I did get that question when I first launched codex and chose ChatGPT sign in. Still It just didn't work until I manually unset the OPENAI_API_KEY. For context, I just set it again and codex is working fine now, so it probably just messed up during setup somehow.

diegochine avatar Sep 04 '25 13:09 diegochine

Same problem here!! Very bizarre and I am using it with Pro subscription!!

RazeBerry avatar Sep 04 '25 13:09 RazeBerry

Just come across this myself, using OPENAI_API_KEY in my .env for the application, and getting the issue with codex trying to use this, even when I have the Pro subscription.

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

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

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

⚠️ stream error: exceeded retry limit, last status: 401 Unauthorized; retrying 4/5 in 1.561s…

⚠️ stream error: exceeded retry limit, last status: 401 Unauthorized; retrying 5/5 in 3.112s…

🖐  exceeded retry limit, last status: 401 Unauthorized

/status 📂 Workspace • Path: ~/Coding/linux-audit • Approval Mode: on-request • Sandbox: workspace-write • AGENTS files: (none)

👤 Account • Signed in with ChatGPT`

mattw-services avatar Sep 06 '25 17:09 mattw-services

same problem with codex-cli 0.30.0 when trying to use my subscription. i've tried cli, vscode and windsurf.

passingby000 avatar Sep 07 '25 07:09 passingby000

Solution for using API Key (Windows)

Verify that your API key is working. setx OPENAI_API_KEY (dont add quotes to the API key) echo %OPENAI_API_KEY% codex Select: 2. Continue using API key Pay for what you use

You should not get that error anymore

hegdeguruprasad avatar Sep 07 '25 08:09 hegdeguruprasad

@gpeal update: codex started throwing the same error once again. Again, the fix was to remove OPENAI_API_KEY from .zprofile, even though I'm signed in via ChatGPT. I'm on latest version (0.31).

diegochine avatar Sep 09 '25 12:09 diegochine

"codex logout", then "codex" in terminal and now works.

jmf42 avatar Sep 10 '25 20:09 jmf42

"codex logout", then "codex" in terminal and now works.

This works at least with the last upgrade

Bakann avatar Sep 11 '25 08:09 Bakann

None of those things worked for me. I unset the api key, logged out, logged in, nothing worked. Still 401 error

marko911 avatar Sep 12 '25 10:09 marko911

The issue here is not that you can sign out or have the API key not set - but that Open AI are not picking up that the user is not signed in or the key is not set and giving you an appropriate message instead of a connection message which is useless and confusing! Sort it our Open AI - the is UX 101 ...

glennsmi avatar Sep 16 '25 09:09 glennsmi

I am facing a similar issue. I have the correct api key, logged out logged in. codex is so frustrating.

did they vibe code this?

utkarsh-fileread avatar Sep 17 '25 06:09 utkarsh-fileread

I had the same problem. Logging out and back into Codex doesn't work. Deleting auth.json and login again definitely resolved the issue.

otrub avatar Sep 18 '25 05:09 otrub

I had the same issue. In my case, the issue was resolved by executing the /logout command once and then /login via the browser. Another possible cause could simply be that the authentication session had expired.

takeaki-m avatar Sep 18 '25 09:09 takeaki-m