In this categoryTroubleshooting ยท 27
- How to Fix an OpenAI 401 'Invalid API Key' ErrorStart
- How to Fix OpenAI 429 Rate Limit Errors With Backoff
- How to Fix Anthropic Claude API 401 Authentication Errors
- How to Fix Google Gemini 'API Key Not Valid' Errors
- How to Fix an API Key That Loads as Undefined
- How to Handle Anthropic 529 'Overloaded' Errors
- How to Fix Rate Limit Errors from an AI API
- How to Fix CORS Errors When Calling an AI API From the Browser
- How to Fix 'Model Not Found' and Deprecated Model Errors
- How to Rotate a Leaked API Key Without Downtime
- How to Fix SSL Certificate Errors When Calling AI APIs
- How to count tokens before sending a prompt to Claude
- How to Fix 'Context Length Exceeded' Token Limit Errors
- How to fix a context length exceeded error in the Claude API
- How to fix a Claude response that gets cut off mid-sentence
- How to reduce Claude hallucinations by grounding answers in your documents
- How to keep a long Claude conversation under the context limit
- How to stop Claude from calling tools when it should not
- How to handle a Claude Fable 5 refusal with a fallback model
- How to choose the right Claude model for cost and quality
- How to cut Claude API costs with prompt caching
- How to halve Claude costs for bulk jobs with the Batch API
- How to cap spend on a Claude agent with a task budget
- How to Debug an MCP Server That Will Not Connect
- How to Ask an Agent to Explain a Bug Before Fixing It
- How to Roll Back a Bad Deploy Quickly
- How to Fix Cursor Not Indexing Your Codebase
How to Fix Anthropic Claude API 401 Authentication Errors
Resolve authentication_error responses from the Claude API by fixing the header and key format.
The Claude API uses a different auth header than most APIs. If you copied a snippet built for OpenAI you will get a 401, because Anthropic does not use a Bearer token in the Authorization header. It uses an x-api-key header plus a version header. This guide fixes the most common auth mistakes.
- An Anthropic account with an API key from console.anthropic.com
- curl or any HTTP client
- Your key, which begins with sk-ant-
Step 1: Use the x-api-key header, not Authorization
Anthropic expects the key in an x-api-key header and a required anthropic-version header. Sending Authorization: Bearer returns authentication_error. Compare the two side by side.
curl https://api.anthropic.com/v1/messages \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{ "model": "claude-opus-4-5", "max_tokens": 64, "messages": [{"role":"user","content":"ping"}] }'Step 2: Confirm the key shape
A valid Anthropic key starts with sk-ant- and is long. If yours is short or starts with sk-proj or sk- alone, it belongs to a different provider. Open the console and verify the prefix matches.
Step 3: Add the version header
The anthropic-version header is not optional. Leaving it out can produce confusing errors. Use a recent dated value like 2023-06-01, which is the stable version string the API expects.
Result
With the x-api-key and anthropic-version headers in place and a valid sk-ant- key, the messages endpoint returns a 200 with a message id. A developer who had copied an OpenAI snippet fixed their 401 by swapping a single header.
Watch related tutorials
1:42:18
28:14
41:09
9:47
8:23
52:31New guides in your inbox
Fresh step-by-step how-to guides as we publish them. One email a week, no more.