LLM API: 401 authentication_error — invalid x-api-key / Incorrect API key provided
The LLM provider rejected your key.
invalid x-api-keyIncorrect API key provided Meaning
The provider could not authenticate the key. Keys are tied to one provider and one workspace; they are also revoked automatically when found in public repositories.
Common causes
- Wrong or revoked key
- Key from another provider
- Env not loaded
- Whitespace
- Using a key in the wrong header (x-api-key vs Authorization)
⚡ Quick fix
- Create a new key
- Check env loading
- Generate a new key in the provider console
- Print the key length (not the value) to find stray whitespace
Detailed fix by platform
Shell
echo -n "$ANTHROPIC_API_KEY" | wc -cto spot stray characters
How to diagnose
- Key — Valid?
- Provider — Right provider?
- Env — Loaded?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- 20003 Twilio: Error 20003 Authenticate — Your AccountSid or AuthToken was incorrect
- Authentication failed Razorpay: BAD_REQUEST_ERROR — Authentication failed (401)
- context_length_exceeded LLM API: This model's maximum context length is x tokens. However, your messages resulted in y tokens (context_length_exceeded / prompt is too long)
Most viewed in API
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 28 Nov 2026