Principle:Anthropics Anthropic sdk python Provider API Interaction
| 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 includingmodel,max_tokens,messages,system,temperature,tools,thinking, and others. Returns aMessageobject (non-streaming) or aStream[RawMessageStreamEvent](streaming).stream()-- A convenience method that always creates a streaming request and returns aMessageStreamManagerfor 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
modelfrom the JSON body, URL-encodes it, and rewrites the path to/model/{model}/invokeor/model/{model}/invoke-with-response-stream. Injectsanthropic_version: "bedrock-2023-05-31"into the body. Propagatesanthropic-betaheaders as a body-levelanthropic_betalist. - Vertex -- Extracts the
modeland rewrites the path to/projects/{project_id}/locations/{region}/publishers/anthropic/models/{model}:{specifier}, where{specifier}israwPredictorstreamRawPredict. Injectsanthropic_version: "vertex-2023-10-16". - Foundry -- Does not rewrite the URL. Instead, it injects authentication headers (
api-keyorAuthorization: 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 usesbotocore.eventstream.EventStreamBufferto parse binary chunks and convert them intoServerSentEventobjects 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 anAnthropicErrorat 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.