Principle:Anthropics Anthropic sdk python Provider Client Initialization
| Knowledge Sources | |
|---|---|
| Domains | Cloud_Deployment, LLM, Infrastructure |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
The Anthropic Python SDK uses the Adapter pattern to provide cloud provider-specific clients that extend the base Anthropic client. Each provider client -- AnthropicBedrock, AnthropicVertex, and AnthropicFoundry -- wraps the same Messages API surface while injecting provider-specific authentication, URL construction, and request preparation logic. This design maintains API compatibility across all providers: code that works with one provider can be migrated to another by changing only the client initialization.
The Adapter Pattern for Cloud Provider Clients
The SDK's class hierarchy follows a layered design:
- Base Client Layer --
BaseClient,SyncAPIClient, andAsyncAPIClientfrom_base_client.pyprovide HTTP transport, retry logic, timeout handling, and response parsing. - Provider Base Layer -- Each provider defines its own base class (
BaseBedrockClient,BaseVertexClient,BaseFoundryClient) that overrides_make_status_error()to map HTTP status codes to provider-appropriate exception types. - Provider Client Layer -- The concrete classes (
AnthropicBedrock,AnthropicVertex,AnthropicFoundry) inherit from both the provider base and the sync/async API client, implementing the full initialization and request lifecycle.
Each provider client overrides three key methods:
_prepare_options()-- Rewrites request URLs and injects provider-specific headers or body fields before the request is dispatched._prepare_request()-- Signs or authenticates the finalhttpx.Requestobject (e.g., SigV4 signing for Bedrock, Bearer token injection for Vertex)._make_sse_decoder()(Bedrock only) -- Returns anAWSEventStreamDecoderinstead of the standard SSE decoder, since Bedrock uses AWS EventStream binary framing.
Maintaining API Compatibility
All provider clients expose the same resource properties:
.messages-- An instance ofMessages(orAsyncMessages) providingcreate(),stream(), and related methods..beta-- An instance ofBeta(orAsyncBeta) for accessing beta API features.
The Foundry client additionally disables unsupported endpoints (such as .models and batch operations) by overriding cached properties to return None, providing clear feedback when users attempt to access unavailable features.
This means that downstream code can be written against the shared Messages interface and remain agnostic to the underlying provider:
def generate_response(client, prompt: str) -> str:
"""Works with AnthropicBedrock, AnthropicVertex, or AnthropicFoundry."""
message = client.messages.create(
model="...", # provider-specific model ID
max_tokens=1024,
messages=[{"role": "user", "content": prompt}],
)
return message.content[0].text
Common Initialization Parameters
All provider clients accept a shared set of base parameters in addition to their provider-specific ones:
base_url-- Override the default endpoint URL.timeout-- Request timeout (float seconds orhttpx.Timeout).max_retries-- Number of automatic retries on transient errors (default: 2).default_headers-- Extra headers merged into every request.default_query-- Extra query parameters merged into every request.http_client-- A customhttpx.Clientorhttpx.AsyncClientfor advanced transport configuration (proxies, certificates, connection pooling).
Each client also provides a copy() method (aliased as with_options()) for creating derived client instances with modified settings without mutating the original.