PermissionDeniedError
anthropic.errors.PermissionDeniedError
Stack trace
anthropic.errors.PermissionDeniedError: Access denied for model 'model-name'. Please check your API key permissions.
Why it happens
This error happens because the API key used does not have permission to access the specified Anthropic model. It can be due to an invalid key, expired key, or the key not being authorized for that model or endpoint.
Detection
Monitor API responses for 403 HTTP status codes and catch PermissionDeniedError exceptions to log and alert on unauthorized access attempts before crashing.
Causes & fixes
Using an API key that is invalid or revoked
Verify your API key is correct, active, and has not been revoked in the Anthropic dashboard.
API key lacks permission for the requested model
Request access to the model in your Anthropic account or use a model your key is authorized to access.
Incorrect environment variable or missing API key configuration
Ensure the environment variable storing your Anthropic API key is set correctly and loaded before client initialization.
Code: broken vs fixed
from anthropic import Anthropic
import os
client = Anthropic(api_key='wrong_or_missing_key')
response = client.messages.create(model='claude-3-5-haiku-20241022', prompt='Hello') # triggers PermissionDeniedError from anthropic import Anthropic
import os
client = Anthropic(api_key=os.environ['ANTHROPIC_API_KEY']) # fixed: use env var with correct key
response = client.messages.create(model='claude-3-5-haiku-20241022', prompt='Hello')
print(response.text) Workaround
Catch PermissionDeniedError exceptions and fallback to a different model or notify the user to update API key permissions while continuing execution.
Prevention
Manage API keys centrally, verify model access permissions before deployment, and implement monitoring to detect unauthorized access attempts early.