Skip to content

ERROR: exceeded retry limit, last status: 401 Unauthorized when using OPENAI_API_KEY #7323

Closed

Description

@maver1ck

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

Activity

  1. added
    bug
    on Nov 26, 2025
  2. added
    auth
    on Nov 26, 2025
  3. etraut-openai commented on Nov 26, 2025

    @etraut-openai
    Contributor

    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.

  4. maver1ck commented on Nov 26, 2025

    @maver1ck
    Author

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

  5. etraut-openai commented on Nov 26, 2025

    @etraut-openai
    Contributor

    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.

  6. added
    documentation
    and removed
    auth
    bug
    on Nov 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions