GuidesHosting choices

Codex 401 Unauthorized: Missing bearer authentication

Why Codex says 401 Unauthorized: Missing bearer or basic authentication in header after WebSocket reconnects, and the sign-in or CODEX_API_KEY that fixes it.

November 14, 2026The Everpod team
The short answer

“401 Unauthorized: Missing bearer or basic authentication in header” means Codex sent its request with no credentials at all: this user has no saved sign-in, and no CODEX_API_KEY is set. An OPENAI_API_KEY in the environment does not count; codex exec does not send it. Sign in with codex login --device-auth or save a key with printenv OPENAI_API_KEY | codex login --with-api-key, or give a single run its key as CODEX_API_KEY. The “failed to connect to Responses WebSocket” and “Reconnecting... 5/5” lines before it come from the same missing sign-in, not from a separate network fault.

The messages, in the order they appear

On Codex 0.169.0 (November 2026), a codex exec run with no credentials prints its header, then fails three ways before it says why:

ERROR codex_api::endpoint::responses_websocket: failed to connect to Responses WebSocket stage=Unknown
ERROR codex_api::endpoint::responses_websocket: failed to connect to Responses WebSocket stage=Unknown
ERROR: Reconnecting... 2/5
ERROR: Reconnecting... 3/5
ERROR: Reconnecting... 4/5
ERROR: Reconnecting... 5/5
ERROR: unexpected status 401 Unauthorized: Missing bearer or basic authentication in header, url: https://api.openai.com/v1/responses, cf-ray: …, request id: …
Copied

The order is the point: the first lines on screen, and the ones most people search for, are about the WebSocket, and the answer is at the bottom. If the WebSocket lines continue after the 401 is fixed, that is a separate question for the network, and codex doctor --network-only checks “provider HTTP connectivity, and Responses WebSocket connectivity” on their own.

Which credentials codex exec reads

OpenAI’s page on non-interactive mode says codex exec “reuses saved CLI credentials by default”, and the one variable it names for a run is CODEX_API_KEY. Codex’s login code gives the order. When CODEX_API_KEY is set, it wins: “API key via env var takes precedence over any other auth method.” Otherwise Codex uses the sign-in saved by codex login. The function that loads credentials never reads OPENAI_API_KEY, which is why a key exported under that name produces exactly the same “Missing bearer” error as no key at all.

So there are three ways to give a run credentials:

printenv OPENAI_API_KEY | codex login --with-api-key
Copied
CODEX_API_KEY=<api-key> codex exec --json "triage open bug reports"
Copied

A key in either form is billed through your OpenAI Platform account at “standard API pricing instead of included ChatGPT plan credits”.

A wrong key reads differently

Once a key is actually sent, a bad one gets a different 401, which names the key:

ERROR: unexpected status 401 Unauthorized: Incorrect API key provided: sk-bogus. You can find your API key at https://platform.openai.com/account/api-keys., … auth error: 401, auth error code: invalid_api_key
Copied

So the two messages split the problem cleanly. “Missing bearer or basic authentication” means Codex found nothing to send. “Incorrect API key provided” with invalid_api_key means it sent a key and OpenAI refused it: a typo, a cut-off paste, or a key that has been revoked. Create a new key on the API keys page and save it again. If you are signed in with ChatGPT and the 401 names a key you never set, read the outage section below before changing anything. A sign-in that worked and then fails on refresh prints its own message instead, covered in “your refresh token was already used”.

Checking before a run

codex login status prints the active authentication mode and exits with 0 when logged in. On 0.169.0 it prints Not logged in and exits 1 with nothing saved, and API key configured - *** after --with-api-key. codex doctor gives a fuller report, and with no credentials two of its findings are the two halves of the error above:

✗ auth  no Codex credentials were found - Run codex login or provide an API key through a supported auth env var.
⚠ websocket  Responses WebSocket failed; HTTPS fallback may still work - Check proxy, VPN, firewall, DNS, custom CA, and WebSocket policy support.
Copied

Run either check as the same user, and in the same kind of shell, as the job that fails. A saved sign-in lives in auth.json under CODEX_HOME, which defaults to ~/.codex, so it belongs to one account’s home folder. A job run as root, under sudo, by a service with another User=, or with a different HOME or CODEX_HOME looks somewhere else and finds nothing. A key given inline as CODEX_API_KEY is not saved anywhere, so for that case the run itself is the check.

On a schedule, nobody sees the first lines

A codex exec job in cron, a systemd timer or CI fails in the same order, but the output lands in a log, often truncated or never opened, and the run just exits with status 1. Two things make the missing credential common there:

For a job that relies on a saved sign-in, a check at the top of the script turns a page of reconnect errors into one line you can find:

codex login status >/dev/null || { echo "codex: not signed in for $(whoami)" >&2; exit 1; }
Copied

Make the job’s exit status reach you, by mail from cron or a failure hook on the timer, since a missing credential does not fix itself on the next run. The rest of an unattended job’s setup, the PATH, the sandbox and the plan’s limits among it, is in running Codex and Claude Code on a schedule. A run that has credentials and is turned away for load fails differently again, with “Selected model is at capacity”.

When the 401 was OpenAI’s

Not every Codex 401 comes from the machine. On September 25 and 26, 2026, users signed in with ChatGPT, who had set no key at all, got “unexpected status 401 Unauthorized: Incorrect API key provided: sk-svcac…”, naming a key that was not theirs. The report on Codex’s tracker, issue #48237, drew close to a hundred comments and a string of duplicates, and OpenAI’s status page records it as “Issues with Codex”, a full outage of the CLI, the VS Code extension, Codex Web and the Codex API lasting about an hour. During it, the status page advised “Login via API key will unblock access at this time,” and it closed with “All impacted services have now fully recovered.”

That outage is over, and it was fixed on OpenAI’s side, not by anything users changed. When many people report the same 401 at the same moment, or the message names a key you have never seen, look at OpenAI’s status page before you sign out, rotate keys or rebuild a server.

Run Codex on an always-on developer pod.

A developer pod is a permanent cloud computer (VPS), set up and secured for you, with your pick of Claude Code, Codex, OpenCode, Pi, Hermes and OpenClaw installed. Run more agents at once than your laptop can hold, and keep them working when it’s closed. From $24 a month, built in about ten minutes.