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

Resolved 💬 38 comments Opened Aug 29, 2025 by rahulkarajgikar Closed Sep 3, 2025
💡 Likely answer: A maintainer (gpeal, contributor) responded on this thread — see the highlighted reply below.

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_

View original on GitHub ↗

38 Comments

rahulkarajgikar · 10 months ago

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

migueldeicaza · 10 months ago

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.

acgxv · 10 months ago

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.

rahulkarajgikar · 10 months ago

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

migueldeicaza · 10 months ago

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

diegochine · 10 months ago

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.

gpeal contributor · 10 months ago

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

<img width="1058" height="392" alt="Image" src="https://github.com/user-attachments/assets/b0074a33-c606-431b-80eb-f571980b8b66" />

diegochine · 10 months ago

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

RazeBerry · 10 months ago

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

mattw-services · 10 months ago

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`

passingby000 · 10 months ago

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

hegdeguruprasad · 10 months ago

Solution for using API Key (Windows)

Verify that your API key is working.
setx OPENAI_API_KEY <Your-API-KEY>
(dont add quotes to the API key)
echo %OPENAI_API_KEY%
codex
Select:

  1. Continue using API key

Pay for what you use

You should not get that error anymore

diegochine · 10 months ago

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

jiangying000 · 10 months ago

If you still encounter this error when you are using the latest version of Codex, it's probably because your key in auth.json is expired. Just try re-login or update the key.

jmf42 · 10 months ago

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

Bakann · 10 months ago
"codex logout", then "codex" in terminal and now works.

This works at least with the last upgrade

marko911 · 10 months ago

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

glennsmi · 10 months ago

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

utkarsh-fileread · 10 months ago

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?

otrub · 10 months ago

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

takeaki-m · 10 months ago

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.

samcdavid · 9 months ago

Nothing that I tried in this issue worked for. I believe that my issue is because we have recently switched to using API keys in an OpenAI project with US data residency constraints. I could not get codex to work with that API key, but when I logged out and then logged in with an old API key without the data residency restriction, it worked.

madson · 9 months ago

If you haven't, try to login first:

$ codex login --api-key=$OPEN_AI_KEY
frixaco · 9 months ago

I had the same issue (WSL2/Arch):

  • no env variable set
  • selected second login option - API key
  • copy pasted the API key

The issue disappeared after /logout and adding the API key to my env.

luislobo · 9 months ago

I had to delete ~/.codex/auth.json today for it to work

ML-Marco · 9 months ago

It doesn't work with the current version (0.45.0). I tried this:

rm -rf ~/.codex/
echo "API-KEY" | codex login --with-api-key
codex

Fails with above 401 error.

PeterP22 · 9 months ago
"codex logout", then "codex" in terminal and now works.

this worked for me as of 8th october 2025. When i typed /status it showed that i was logged in as normal but kept receiving 401 request error. I logged out in the cli then retyped codex. Once in it prompted me to use either my pro account or api key and now all cylinders are firing.

therzv · 9 months ago

try all the things above and not work, the only way to solve this issue to me is:

put OPENAI_API_KEY on .bashrc

then login like:
$ printenv OPENAI_API_KEY | codex login --with-api-key

done, smooth

DavidLee528 · 9 months ago

@openai/codex@0.36.0 - Issue solved by re-login:

codex logout
codex login
vedantdere · 9 months ago

Updating VS Code fixed this issue

chans97 · 9 months ago
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.

That seems to be the most reliable solution.

fallonou1999 · 9 months ago

still having this issue in 0.47.
and it seems to have something to do with projects path 'cause while some of my projects works normally, other reconnects all the time

VArtzy · 9 months ago

I face the same issue

I use codex 0.47 (macOS or Linux), fix it with:

  • ``export OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>``
  • if you are logged in, ``codex logout`` to restart unauthorized session
  • then run ``codex`` to create session

now if it success, you choose second login option using API key, it will fill API key automatically. And now after your codex should working and authorized.

---

I don't know why such openai/codex can have this silly bug look like my custom gemini-llm-cli. they probably vibe code it lol.

SSugar310 · 9 months ago

I experienced the same issue.
In my case, I was logged in to ChatGPT through OAuth using the VSCode extension, not an API key.
After selecting “Log out” from the extension and logging in again, it started working normally.

2u35eeee · 8 months ago

my version is OpenAI Codex (v0.50.0) and occurs exceeded retry limit, last status: 401 Unauthorized,

200105060414pys · 8 months ago

"Error: exceeded retry limit, last status: 401 Unauthorized, request id: ”
I used codex cli in IntelliJ IDEA on a Windows operating system this afternoon. After asking a question, I got the above error message. Code 401 is caused by Invalid Authentication, so I found the folder named ".codex", which is under the path of "C:\Users\user\.codex". And the folder contains the information of key and token. Because I activated the GPT Plus service, and used codex cli by logging into my GPT account, so I simply deleted the entire folder. Then, I entered the command "codex login" , and reconfigured the command information.
If you use codex cli by key, maybe you should modify the key of "auth.json" file.

<img width="807" height="241" alt="Image" src="https://github.com/user-attachments/assets/fbe06d65-bc15-4a0d-a8dc-29b6652669b5" />[](url)

<img width="372" height="109" alt="Image" src="https://github.com/user-attachments/assets/f285d9b9-6bab-46a9-907c-c84b1fa123cc" />

binarybana · 8 months ago

I found I had to generate a new API key to get codex to work. The length of the new one was significantly longer than my API key that was failing.

joker123-wpx · 6 months ago
“错误:超过重试限制,最后状态:401 未授权,请求编号:”我今天下午在Windows作系统上用了IntelliJ IDEA的codex cli。提问后,我收到了上面的错误提示。代码401是由认证无效引起的,所以我找到了一个名为“.codex”的文件夹,它位于“C:\Users\user.codex”路径下。文件夹里包含密钥和令牌的信息。因为我激活了GPT Plus服务,并通过登录我的GPT账号使用codex cli,所以我干脆把整个文件夹都删掉了。然后,我输入了命令“codex login”,重新配置了命令信息。如果你用codex cli逐键作,也许你应该修改“在auth.json”文件里的键。 <img alt="图片" width="807" height="241" src="https://private-user-images.githubusercontent.com/77435598/507660519-fbe06d65-bc15-4a0d-a8dc-29b6652669b5.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Njc3MjU5MTIsIm5iZiI6MTc2NzcyNTYxMiwicGF0aCI6Ii83NzQzNTU5OC81MDc2NjA1MTktZmJlMDZkNjUtYmMxNS00YTBkLWE4ZGMtMjliNjY1MjY2OWI1LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNjAxMDYlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjYwMTA2VDE4NTMzMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWUyOTUzZDk0OTk2YjE1Mzk5YzE5YmM4ZjBiNTE0Y2ZhMWVjMjllMmFkZjM4NWNiMzAzODlhMmJkMjNmNjE0ZTImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.jSLobz71IN-im8OGarYuVkJKF3on0x90u7_GnLcHZOw"> <img alt="图片" width="372" height="109" src="https://private-user-images.githubusercontent.com/77435598/507665355-f285d9b9-6bab-46a9-907c-c84b1fa123cc.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Njc3MjU5MTIsIm5iZiI6MTc2NzcyNTYxMiwicGF0aCI6Ii83NzQzNTU5OC81MDc2NjUzNTUtZjI4NWQ5YjktNmJhYi00NmE5LTkwN2MtYzg0YjFmYTEyM2NjLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNjAxMDYlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjYwMTA2VDE4NTMzMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTA4ZjA2YWRiNTIwMGM2N2MzYmY5M2M1ZDA5YmE4MjQ2Njc2MWYyMTIxZjM0YjU1ODg5NzIxZjEzZDA2OGVhOTImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.3ReaVFQ6NRsSCaD-KXNg-wbCC1btKWioDlpyA3FspJU">

牛逼兄弟,我看了半天,直接用你的成了