Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:Anthropics Anthropic sdk python Provider Client Initialization

From Leeroopedia
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:

  1. Base Client Layer -- BaseClient, SyncAPIClient, and AsyncAPIClient from _base_client.py provide HTTP transport, retry logic, timeout handling, and response parsing.
  2. 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.
  3. 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 final httpx.Request object (e.g., SigV4 signing for Bedrock, Bearer token injection for Vertex).
  • _make_sse_decoder() (Bedrock only) -- Returns an AWSEventStreamDecoder instead 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 of Messages (or AsyncMessages) providing create(), stream(), and related methods.
  • .beta -- An instance of Beta (or AsyncBeta) 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 or httpx.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 custom httpx.Client or httpx.AsyncClient for 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.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment