Skip to main content
401Not retryable

OpenAI 401 Invalid Authentication

The API key is wrong, revoked, or sent incorrectly. OpenAI expects Authorization: Bearer sk-…

Most likely causes

  1. 1.OPENAI_API_KEY unset or pointing at an old rotated key
  2. 2.Project-scoped key (sk-proj-…) used against a different project's resources
  3. 3.Key pasted with whitespace/newline from a secrets UI
  4. 4.Org header (OpenAI-Organization) naming an org the key doesn't belong to

Fix checklist

  • Verify with a curl to /v1/models using the exact key
  • Trim whitespace; check the key isn't truncated in your env tooling
  • Re-issue the key in the platform dashboard and rotate secrets
  • Drop the org header unless you genuinely need multi-org routing

Retry guidance

Never retry — fix credentials first.

// 401 is NOT retryable — surface it and fix the request/config.
const res = await fetch("https://api.openai.com/v1/chat/completions", {
  method: "POST",
  headers: {
    "content-type": "application/json",
    Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
  },
  body: JSON.stringify(payload),
});
if (res.status === 401) {
  const body = await res.json();
  // Do not retry: log the provider's message — it names the exact problem.
  throw new Error(`OpenAI 401: ${JSON.stringify(body.error ?? body)}`);
}

Provider status page: status.openai.com