ERROR: exceeded retry limit, last status: 401 Unauthorized when using OPENAI_API_KEY
Resolved 💬 4 comments Opened Nov 26, 2025 by maver1ck Closed Jan 14, 2026
💡 Likely answer: A maintainer (github-actions[bot], contributor)
responded on this thread — see the highlighted reply below.
What version of Codex is running?
OpenAI Codex v0.63.0 (research preview)
What subscription do you have?
OPENAI_API_KEY
Which model were you using?
gpt-5.1-codex-max
What platform is your computer?
MacOS
What issue are you seeing?
❯ OPENAI_API_KEY="sk-xxx" codex exec "2+2=?"
OpenAI Codex v0.63.0 (research preview)
--------
workdir: /Users/maverick/src/xxx/
model: gpt-5.1-codex-max
provider: openai
approval: never
sandbox: workspace-write [workdir, /tmp, $TMPDIR] (network access enabled)
reasoning effort: high
reasoning summaries: auto
session id: 019ac11e-30c6-7fc3-863b-1af947013d9f
--------
user
2+2=?
mcp: chrome starting
mcp: chrome ready
mcp startup: ready: chrome
Reconnecting... 1/5
Reconnecting... 2/5
Reconnecting... 3/5
Reconnecting... 4/5
Reconnecting... 5/5
ERROR: exceeded retry limit, last status: 401 Unauthorized, request id: 9a4af5870a31a58e-WAW
What steps can reproduce the bug?
Run codex exec with OPENAI_API_KEY
What is the expected behavior?
Codex is working with provided API_KEY
Additional information
_No response_
4 Comments
Potential duplicates detected. Please review them and close your issue if it is a duplicate.
Powered by Codex Action
This means that you're using an API key that is invalid.
Simply defining an environment variable isn't enough to log in to codex. You need to explicitly run
codex login --with-api-key < $OPENAI_API_KEY. This will log you in and store the API key for subsequent invocations.Alternatively, you can run
codex logoutfollowed bycodex, and it will ask you to enter your API Key (defaulting to the value stored inOPENAI_API_KEY.@etraut-openai
Thanks for advice.
This is approach you explicite stated in documentation.
References: https://cookbook.openai.com/examples/codex/secure_quality_gitlab
Maybe it's worth to fix this document.
I missed the fact that you are using
codex execrather than the interactivecodex. My answer above pertains to interactive use cases. The non-interactivecodex execdoes support implicit login, but you need to use theCODEX_API_KEYenv variable, notOPENAI_API_KEY. Refer to this documentation for details.Thanks for pointing out the stale cookbook. We should update that to use
CODEX_API_KEYrather thanOPENAI_API_KEY.I'm going to reopen this issue and change it to a documentation bug.