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 API Interaction

From Leeroopedia
Knowledge Sources
Domains Cloud_Deployment, LLM, Infrastructure
Last Updated 2026-02-15 00:00 GMT

Overview

The Anthropic Python SDK achieves transparent API compatibility across all cloud providers by ensuring that the same Messages.create() and Messages.stream() methods work identically whether the underlying transport is the direct Anthropic API, AWS Bedrock, Google Cloud Vertex AI, or Azure AI Foundry. Provider-specific request transformation, response parsing, and streaming behavior are hidden behind a common interface, so callers interact with a single, unified API surface regardless of deployment target.

The Same Messages API Across All Providers

The Messages resource class (defined in src/anthropic/resources/messages/messages.py) is shared across all provider clients. It defines:

  • create() -- Sends a message creation request with parameters including model, max_tokens, messages, system, temperature, tools, thinking, and others. Returns a Message object (non-streaming) or a Stream[RawMessageStreamEvent] (streaming).
  • stream() -- A convenience method that always creates a streaming request and returns a MessageStreamManager for ergonomic event iteration.

These methods always post to the generic path /v1/messages. The provider client's _prepare_options() method intercepts this before dispatch and rewrites the URL, injects version headers, and transforms the body as needed for the target provider.

Provider-Specific Request and Response Handling

Request Transformation

Each provider's _prepare_options() transforms the request differently:

  • Bedrock -- Extracts the model from the JSON body, URL-encodes it, and rewrites the path to /model/{model}/invoke or /model/{model}/invoke-with-response-stream. Injects anthropic_version: "bedrock-2023-05-31" into the body. Propagates anthropic-beta headers as a body-level anthropic_beta list.
  • Vertex -- Extracts the model and rewrites the path to /projects/{project_id}/locations/{region}/publishers/anthropic/models/{model}:{specifier}, where {specifier} is rawPredict or streamRawPredict. Injects anthropic_version: "vertex-2023-10-16".
  • Foundry -- Does not rewrite the URL. Instead, it injects authentication headers (api-key or Authorization: Bearer ...) via _prepare_options(). The model name stays in the JSON body and is routed by Azure's infrastructure.

Streaming Differences

The three providers use different streaming transports:

  • Bedrock uses the AWS EventStream binary protocol. The SDK provides a custom AWSEventStreamDecoder (returned by _make_sse_decoder()) that uses botocore.eventstream.EventStreamBuffer to parse binary chunks and convert them into ServerSentEvent objects compatible with the SDK's standard streaming pipeline.
  • Vertex AI and Foundry use standard Server-Sent Events (SSE), which is the same format used by the direct Anthropic API. No custom decoder is needed.

Despite these transport-level differences, callers always receive the same Stream[RawMessageStreamEvent] or MessageStreamManager objects, making the streaming protocol completely transparent.

Provider Differences Transparent to Callers

The transparency guarantee means that application code can switch between providers with minimal changes:

# All three produce the same Message response type
bedrock_msg = bedrock_client.messages.create(
    model="us.anthropic.claude-sonnet-4-20250514-v1:0",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
)

vertex_msg = vertex_client.messages.create(
    model="claude-sonnet-4@20250514",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
)

foundry_msg = foundry_client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
)

The response objects (bedrock_msg, vertex_msg, foundry_msg) are all instances of the same Message type with identical .content, .role, .usage, and other attributes.

Limitations by Provider

Not all API features are available on all providers:

  • Bedrock does not support the Batch API or token counting (count_tokens). Attempting to use these raises an AnthropicError at request preparation time.
  • Vertex does not support the Batch API.
  • Foundry does not support the Batch API or the Models endpoint. These are disabled via property overrides that return None.

These limitations are enforced early -- in _prepare_options() or via property overrides -- rather than letting the request fail at the network level, providing clear and immediate error messages.

Related Pages

Implemented By

Page Connections

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