You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ERROR: exceeded retry limit, last status: 401 Unauthorized when using OPENAI_API_KEY #7323
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.
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.
Activity
github-actions commentedon Nov 26, 2025
Potential duplicates detected. Please review them and close your issue if it is a duplicate.
Powered by Codex Action
etraut-openai commentedon Nov 26, 2025
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.maver1ck commentedon Nov 26, 2025
@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 commentedon Nov 26, 2025
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.
fix(codex): use CODEX_API_KEY for exec (fix 401), reduce debug noise