For an OpenAI 401, first confirm which credential and organization the deployed process is using. Replacing the model or adding credits does not repair invalid authentication. Do not print an API key, including its leading or trailing characters, into logs or a support ticket.
Separate the documented 401 cases
| What the response identifies | What to inspect |
|---|---|
| Invalid authentication or incorrect API key | The secret reference, selected environment, revoked or deleted keys, accidental whitespace, and the actual authorization header construction. |
| Organization membership | Whether the account has the required organization membership and the request is using the intended organization or project. |
| IP not authorized | The deployment's outbound IP and the organization's or project's configured IP allowlist. |
These are categories in OpenAI's error guide, not a promise that every response uses one identical error.code or message. Keep the status, structured body, and request ID. The same guide separately documents a 403 for unsupported countries, regions, or territories; do not relabel that as an invalid key.
Check configuration without exposing the secret
Read the credential inside the environment that fails: a local shell, build process, container, worker, and deployed service can each receive different variables. Confirm the secret manager's reference or version and the target environment. Report only whether the value is present and whether surrounding whitespace was detected. Do not log its value, fragments, headers, or a full environment dump.
The following Python check makes no network request. It fails before constructing a client if the variable is absent or has surrounding whitespace. It deliberately does not “fix” the value by silently trimming it; repair the configured secret so all consumers see the intended value.
"""Validate configuration presence without logging or transmitting a secret."""
import os
def configured_api_key(environment=None):
environment = os.environ if environment is None else environment
value = environment.get("OPENAI_API_KEY")
if not isinstance(value, str) or not value.strip():
raise ValueError("OPENAI_API_KEY is missing or blank")
if value != value.strip():
raise ValueError("OPENAI_API_KEY has surrounding whitespace")
return value # Pass to a server-side client; never print this value.
For direct HTTPS requests, the documented header is Authorization: Bearer <credential>. An official SDK constructs that header for you. Check for a double Bearer prefix, a missing separator, an unintended literal placeholder, or configuration that never reached the running service. Do not put a provider secret in browser code.
If the key was rotated
- Confirm the intended organization and project in the API dashboard. Provision a replacement only with the necessary permissions.
- Update the deployment's secret reference, then restart or redeploy whichever process loads that reference at startup.
- Check that every active instance has picked up the new version. A mix of old and new instances can produce intermittent failures.
- Revoke the old credential according to your rotation procedure. If it was exposed, treat revocation as an incident response rather than a cosmetic configuration change.
What a successful check establishes
An authenticated GET /v1/models can help inspect available model records with the same credential and scope. OpenAI documents it as a model-list endpoint. Success does not establish that a particular inference endpoint, tool, service tier, region, or billed workload is usable. Those require their own checks.
No authenticated request was made for this guide. The configuration helper is tested locally with missing, blank, whitespace-padded, and supplied fixture values; no real secret is included in those tests. Any production verification should use a bounded request under the owner's authorization and should retain only a redacted diagnostic record.
Once authentication succeeds, route subsequent failures by their actual status and code. Model availability and billing limits require different changes from a credential failure.