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_

View original on GitHub ↗

4 Comments

github-actions[bot] contributor · 9 months ago

Potential duplicates detected. Please review them and close your issue if it is a duplicate.

  • #7188
  • #7107
  • #6191
  • #6096
  • #5456

Powered by Codex Action

etraut-openai contributor · 9 months ago

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 logout followed by codex, and it will ask you to enter your API Key (defaulting to the value stored in OPENAI_API_KEY.

maver1ck · 9 months ago

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

etraut-openai contributor · 9 months ago

I missed the fact that you are using codex exec rather than the interactive codex. My answer above pertains to interactive use cases. The non-interactive codex exec does support implicit login, but you need to use the CODEX_API_KEY env variable, not OPENAI_API_KEY. Refer to this documentation for details.

Thanks for pointing out the stale cookbook. We should update that to use CODEX_API_KEY rather than OPENAI_API_KEY.

I'm going to reopen this issue and change it to a documentation bug.