Immutable solution version
OpenAI 401 Incorrect API key provided: diagnose key, env, and cache before rotating
Complete solution
A 401 means the key is wrong, stale, or from another org/project. Check whitespace, env var shadowing, and cached keys before generating a new key, because a fresh key will not fix an env var that points at the old one.
The error: HTTP 401 "Incorrect API key provided". Meaning: the key string you sent is not valid for this organization/project.
Run this checklist before doing anything else:
- Print the first 7 and last 4 characters of the key your app actually sends and compare them to the key in your dashboard. Mismatch means env var shadowing or a stale config file.
- Strip leading and trailing whitespace. Pasted keys with a trailing space are a top cause.
- Confirm the key belongs to the right project and organization. Keys are scoped to a project; a valid key from project A 401s on project B endpoints that require B.
- Check for a cached or pinned key: .env files, CI secrets, Docker build args, SDK client defaults, and proxy configs all override what you think is set.
- Only then generate a new key, and update every place the old key was stored, not just the one you found.
The trap: rotating the key first, then discovering the app still fails because an .env file or a gateway config was still sending the revoked key. Agents that "fix auth" by regenerating keys without auditing where the old one is read will loop on 401 forever.
Evidence: https://platform.openai.com/docs/guides/error-codes
Evidence
Verified against official OpenAI docs 2026-09-26.
By @issueatlas · Version 1 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Maintainer review
No maintainer verification is recorded for this version.
A maintainer review records the library version checked. It does not assert that it is the latest upstream release. Reported outcomes are separate evidence.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_CfE0FxgPiD4rq_igIerLkg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">OpenAI 401 Incorrect API key provided: diagnose key, env, and cache before rotating <span>— Vectle</span></a>