401 Incorrect API key provided

A 401 Incorrect API key provided usually means the key and base URL do not match. Match the key prefix to the host, then confirm with one gpt-6.1-sol call.

Checked 2026-11-22

·All fixes·Models

A 401 Incorrect API key provided means the server that answered did not accept the key it was sent. OpenAI's error-code guide lists the causes: a typo or extra space, a key from another organization or project, a deleted key, or an old key cached locally. After you point a client at https://api.uplinkswitch.com/v1, the usual cause is a key and base URL that don't match: an OpenAI key sent to the gateway, or a gateway key still sent to OpenAI. Fix the pair, then confirm it with one call to gpt-6.1-sol.

What this means, per OpenAI's docs

The error-codes guide from OpenAI documents the 401 as Incorrect API key provided and names four things to check: the key was entered wrong, it belongs to a different organization or project, it was revoked or deleted, or a cached copy is still in the environment. The common thread is that authentication is checked at the very front of the request, so a single wrong character fails the whole call before anything is generated.

The reason the searched string often reads unexpected status 401 unauthorized: incorrect api key provided is the client, not the server. The Codex CLI wraps any non-success response in its own prefix, printing it as unexpected status {status}: {body} (see codex-rs/protocol/src/error.rs in openai/codex). So the first half of that line is Codex reporting the HTTP status, and the second half is the provider's message passed straight through. A measured variant also tacks a short key fragment onto the end; treat that fragment as part of your key and never paste it anywhere public.

What the gateway changes

At https://api.uplinkswitch.com/v1 the 401 comes back as invalid_api_key inside the OpenAI Error schema, which carries code, message, type and param. Every error also returns a request id in its headers, the identifier to quote when you ask for help.

A 401 is resolved before any route is chosen, so no model ever runs. That has a direct consequence: route failover does nothing here, and a models fallback list does nothing either, because there is no 5xx or 429 from an upstream to fail over, only a rejected key. Nothing was generated, so nothing is billed.

The quickest way to tell the two hosts apart is the key prefix. Gateway keys issued for UplinkSwitch begin with sk-uplink-live-. If a key with that prefix reaches OpenAI directly, or an OpenAI key reaches the gateway, the host will reject it as not its own, and you see the same 401. Matching the prefix to the base URL the client is actually using settles most of these in one look.

How to fix it

  1. Print the base URL the client resolves at runtime. Compare the value passed to the client constructor against the OPENAI_BASE_URL environment variable; one of them wins, and it is often not the one you edited. The target you want is https://api.uplinkswitch.com/v1.
  2. Match the key prefix to that host. A sk-uplink-live- key goes to the gateway base URL; an OpenAI key goes to OpenAI's own base URL. A mismatch here is the single most common cause after you switch hosts.
  3. Strip stray whitespace and newlines. A trailing newline from a copied secret or a .env line with a quote still attached produces an invalid key that looks correct on screen.
  4. Remove a stale key from the shell profile. A key exported in ~/.zshrc or ~/.bashrc overrides what your app sets and survives restarts, so clear or update it.
  5. List what the key may call. Send GET /v1/models with the key; a clean list confirms the key authenticates against this host, and a 401 there confirms the key itself.
  6. Send one real request with gpt-6.1-sol. A single successful chat completion proves the key and base URL now agree end to end.

On the 2026-11-23 catalog gpt-6.1-sol read ok; any chat id that reads ok there does the same job.

For the Codex CLI, set the provider config to the gateway base URL and the matching key; take the exact provider-config field names from Codex's current documentation, since they change between versions. If you are moving an OpenAI client over for the first time, the base-URL setup itself lives in the OpenAI Python base-URL guide; this page is about the 401 that follows a mismatch, not the initial switch.

Questions and answers

That prefix is Codex, not the provider. The CLI reports any non-2xx answer as unexpected status {status}: {body}, so the 401 is the HTTP status it saw and the incorrect api key provided text after it is the server's own message. Read the line as two parts: the client's status report, then the provider's reason.

Almost always the pairing of the two. Check the prefix against the host: a sk-uplink-live- key belongs with https://api.uplinkswitch.com/v1, and an OpenAI key belongs with OpenAI. If the prefix matches the host and you still get a 401, the key itself is wrong, revoked, or carries hidden whitespace.

No. A 401 is settled before a route is picked, so no model runs, nothing is charged, and there is no upstream failure to retry. A models fallback list only helps when an upstream returns a 5xx or 429. Fix the credential, then send again.
Join on Telegram
Help