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.
“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- “failed to connect to Responses WebSocket”. Codex tries to reach the model over a WebSocket first. With no credentials, that connection fails too, so the line looks like a network problem when it is not one.
- “Reconnecting... 2/5” to “5/5”. Codex retries, and nothing changes, because the credentials are still missing.
- “401 Unauthorized: Missing bearer or basic authentication in header”. The final request, to
https://api.openai.com/v1/responses, gets an answer, and OpenAI says that the request carried no key or token. This is the only line that names the cause, and the run exits with status 1.
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:
- A ChatGPT sign-in, saved once per machine. On a server with no browser that is
codex login --device-auth, which prints a link and a code you approve from your phone or laptop; the guide to Codex on a Linux server walks through it, and the device-code guide covers the setting it needs on ChatGPT’s side. Usage draws on your ChatGPT plan. - An API key, saved.
codex login --with-api-keyreads the key from standard input and stores it, so every later run on that account uses it:
printenv OPENAI_API_KEY | codex login --with-api-keyCopied- An API key for one run. Set
CODEX_API_KEYon the command itself. OpenAI’s docs say it works with “codex exec,codex review, the TypeScript SDK, andcodex exec-server --remote”:
CODEX_API_KEY=<api-key> codex exec --json "triage open bug reports"CopiedA 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_keyCopiedSo 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.CopiedRun 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:
- cron and systemd do not read your
~/.bashrc, so aCODEX_API_KEYexported there exists in your terminal and not in the job. Put the variable on the job’s own command line or in its unit’s environment, and keep it to that one command, as OpenAI’s page advises: “setCODEX_API_KEYonly for the Codex invocation that needs it.” - The job runs as a different account from the one you signed in as, as above.
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; }CopiedMake 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.