Email me

AI API errors / OpenAI

OpenAI · HTTP 401 · checked Oct 24, 2026

OpenAI 401 invalid_api_key: what it means and how to fix it

Do not retry: fix it first
401 invalid_api_key: Incorrect API key provided

What it meansThe key is wrong: a typo or extra space, a key from another org or project, a deleted key, or an old key still cached somewhere.

How to fix it

  1. Check the Authorization: Bearer header and the environment variable your process actually reads.
  2. Generate a new key and replace it everywhere (CI secrets, serverless env vars).

Handle it in code

from openai import OpenAI, RateLimitError, InternalServerError

# The SDK retries some errors with backoff; quota and billing errors never clear by retrying.
client = OpenAI(max_retries=6)

try:
    r = client.responses.create(model="gpt-5", input="Hello")
except RateLimitError as e:          # 429: check e.code for quota vs rate limit
    if getattr(e, "code", None) in ("insufficient_quota", "credit_balance_exhausted"):
        raise  # fix billing, do not retry
except InternalServerError:          # 500 / 503 server_is_overloaded
    pass  # retry later or fall back to another model

The official SDKs retry some errors automatically (max_retries); billing and quota errors never clear by retrying.

Same kind of error at other providers

Other OpenAI errors

Source

Paying too much per call while you are here? Compare AI model prices or estimate your bill.

Up next

Paste any errorEvery AI API error, retry or fixAnthropic, OpenAI and Gemini in one finder.Read it next →

Did this answer your question?

Up next

Get 3 picks for your job, volume and budgetPick an AI model for your jobLLM API pricing for 71 models per 1M tokens, checked at the source. Pick for me: your job, volume and budget give 3 models with the monthly cost. Filters, CSV.Read it next →

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Did this answer your question?

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ