High severity HTTP 401 beginner · Fix: 2-5 min

AuthenticationError

anthropic.client.AuthenticationError

What this error means
Anthropic's API returns AuthenticationError when the provided API key is missing, invalid, or revoked, blocking all requests.

Stack trace

traceback
anthropic.client.AuthenticationError: Invalid API key provided. Please check your API key and try again.
  File "app.py", line 12, in <module>
    response = client.messages.create(model="claude-3-5-sonnet-20241022", messages=[{"role": "user", "content": "Hello"}])
  File "/usr/local/lib/python3.9/site-packages/anthropic/client.py", line 234, in create
    raise AuthenticationError("Invalid API key provided.")
QUICK FIX
Set the 'ANTHROPIC_API_KEY' environment variable to your valid API key before running the Anthropic client.

Why it happens

This error occurs because the Anthropic client cannot authenticate your request due to an invalid, missing, or revoked API key. The client checks the key before sending requests, and if it fails validation, it raises AuthenticationError immediately.

Detection

Catch AuthenticationError exceptions when calling Anthropic client methods and log the API key environment variable presence and correctness before retrying or failing.

Causes & fixes

1

API key environment variable 'ANTHROPIC_API_KEY' is not set or empty

✓ Fix

Set the 'ANTHROPIC_API_KEY' environment variable with your valid Anthropic API key before running your application.

2

API key string is malformed or contains extra whitespace

✓ Fix

Ensure the API key string is copied exactly from your Anthropic dashboard without extra spaces or line breaks.

3

Using an expired or revoked API key

✓ Fix

Generate a new API key from the Anthropic dashboard and update your environment variable accordingly.

4

Passing the API key incorrectly in the client initialization or missing it entirely

✓ Fix

Use the Anthropic client constructor correctly by relying on the environment variable or explicitly passing the API key parameter.

Code: broken vs fixed

Broken - triggers the error
python
from anthropic import Anthropic

client = Anthropic()  # Missing API key environment variable
response = client.messages.create(model="claude-3-5-sonnet-20241022", messages=[{"role": "user", "content": "Hello"}])  # Raises AuthenticationError
Fixed - works correctly
python
import os
from anthropic import Anthropic

os.environ["ANTHROPIC_API_KEY"] = "your_valid_api_key_here"  # Set your API key securely
client = Anthropic()
response = client.messages.create(model="claude-3-5-sonnet-20241022", messages=[{"role": "user", "content": "Hello"}])
print(response)
Added setting the 'ANTHROPIC_API_KEY' environment variable with a valid key so the Anthropic client can authenticate requests successfully.

Workaround

Wrap Anthropic client calls in try/except AuthenticationError, and prompt the user or system to reload or reset the API key environment variable dynamically.

Prevention

Use secure environment management to ensure the API key is always set correctly before starting your application, and monitor key validity via Anthropic dashboard alerts.

Python 3.9+ · anthropic >=0.20.0 · tested on 0.20.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.