Principle:Anthropics Anthropic sdk python Authentication Configuration
| Knowledge Sources | |
|---|---|
| Domains | Cloud_Deployment, LLM, Infrastructure |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Each cloud provider uses a distinct authentication mechanism. The Anthropic Python SDK abstracts these differences behind a common client initialization interface, but internally implements provider-specific authentication flows: AWS SigV4 request signing for Bedrock, Google OAuth2 token management for Vertex AI, and Azure AD token providers or API keys for Foundry. All three clients follow an environment variable fallback pattern where credentials can be explicitly provided as constructor arguments or automatically inferred from the runtime environment.
Cloud Provider Authentication Patterns
AWS SigV4 Request Signing (Bedrock)
AWS Bedrock requires every HTTP request to be signed using the Signature Version 4 (SigV4) algorithm. The SDK implements this through a _prepare_request() override that intercepts outgoing httpx.Request objects and attaches SigV4 authentication headers.
The signing flow works as follows:
- A
boto3.Sessionis created from the provided credentials (or resolved from the environment). - The session's credentials are extracted via
session.get_credentials(). - A
botocore.auth.SigV4Authsigner computes theAuthorization,X-Amz-Date, and optionallyX-Amz-Security-Tokenheaders. - The signed headers are injected into the outgoing request.
The credential resolution chain follows standard AWS conventions:
- Explicit constructor arguments (
aws_access_key,aws_secret_key,aws_session_token) - Named profile via
aws_profile - Environment variables (
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_SESSION_TOKEN) - Instance metadata / IAM roles (resolved transparently by
boto3)
Google OAuth2 Token Management (Vertex AI)
Vertex AI authenticates via OAuth2 bearer tokens. The SDK supports three authentication strategies, tried in order:
- Explicit access token -- If
access_tokenis provided, it is used directly as aBearertoken. - Explicit credentials object -- If a
GoogleCredentialsobject is provided, the SDK refreshes it when expired and extracts the token. - Application Default Credentials (ADC) -- If neither is provided, the SDK calls
google.auth.default()with thecloud-platformscope, which resolves credentials fromGOOGLE_APPLICATION_CREDENTIALS, the GCE metadata server, orgcloud auth application-default login.
Token refresh is handled automatically: the _ensure_access_token() method checks credentials.expired before each request and calls credentials.refresh(Request()) as needed. In the async client, these blocking operations are offloaded to a thread via asyncify().
Azure AD and API Key Authentication (Foundry)
Azure AI Foundry supports two mutually exclusive authentication methods:
- API key -- A static key passed as
api_keyor read fromANTHROPIC_FOUNDRY_API_KEY. It is sent as theapi-keyheader (not the standardAuthorizationheader). - Azure AD token provider -- A callable (
Callable[[], str]for sync,Callable[[], str | Awaitable[str]]for async) that is invoked on every request to obtain a fresh bearer token. This supports token rotation without client recreation.
The _prepare_options() method checks for the Azure AD token first; if absent, it falls back to the API key. Providing both raises a MutuallyExclusiveAuthError.
Environment Variable Fallback Pattern
All three providers follow a consistent pattern where constructor arguments take precedence over environment variables:
| Provider | Constructor Arg | Environment Variable Fallback |
|---|---|---|
| Bedrock | aws_region |
AWS_REGION, then boto3 session, then us-east-1 default
|
| Bedrock | base_url |
ANTHROPIC_BEDROCK_BASE_URL
|
| Vertex | region |
CLOUD_ML_REGION (required -- raises ValueError if missing)
|
| Vertex | project_id |
ANTHROPIC_VERTEX_PROJECT_ID, then resolved from ADC
|
| Vertex | base_url |
ANTHROPIC_VERTEX_BASE_URL
|
| Foundry | api_key |
ANTHROPIC_FOUNDRY_API_KEY
|
| Foundry | resource |
ANTHROPIC_FOUNDRY_RESOURCE
|
| Foundry | base_url |
ANTHROPIC_FOUNDRY_BASE_URL
|
This layered resolution enables zero-configuration usage in cloud environments (where IAM roles, metadata servers, or secret managers populate environment variables) while still allowing explicit overrides for local development and testing.