What it means
The AI provider rejected your key. It's missing, mistyped, revoked, or the wrong one.
Why AI tools cause it
- The key is in your local
.envbut was never added to your host. - The variable name in code doesn't match the name you set (
OPENAI_KEYvsOPENAI_API_KEY). - The key was pasted with a space, a line break or quote marks around it.
- The key was leaked on GitHub and the provider automatically revoked it.
The fix
- Create a fresh key in your provider's dashboard.
- Set it as a server-only environment variable on your host, with no quotes or spaces, then redeploy.
- Call the API only from server code. Never put the key in a variable that starts with
NEXT_PUBLIC_orVITE_; those are visible to anyone.
Paste this into your AI
My AI API calls fail with a 401 invalid key error in production. Show me exactly which environment variable the code reads, confirm the call happens only on the server, and make sure the key never reaches the browser bundle.
Still broken?
If the key works on your machine but not live, it's almost always the host's environment variables. Check them again and redeploy.