Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/api/providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

::: pydantic_ai.providers.gateway.gateway_provider

::: pydantic_ai.providers.anthropic.AnthropicProvider

::: pydantic_ai.providers.google

::: pydantic_ai.providers.openai
Expand Down
100 changes: 100 additions & 0 deletions docs/models/anthropic.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,106 @@ agent = Agent(model)
...
```

## Cloud Platform Integrations

You can use Anthropic models through cloud platforms by passing a custom client to [`AnthropicProvider`][pydantic_ai.providers.anthropic.AnthropicProvider].

### AWS Bedrock

To use Claude models via [AWS Bedrock](https://aws.amazon.com/bedrock/claude/):

=== "With Pydantic AI Gateway"

```python {title="<a href='/gateway/' style='float: right;'>Learn about Gateway</a>" test="skip"}
from pydantic_ai import Agent

agent = Agent('gateway/bedrock:us.anthropic.claude-haiku-4-5-20251001-v1:0')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike the rest of this page, this will not actually use the AnthropicModel or the Anthropic-API(-running-on-Bedrock), but rather BedrockConverseModel and the AWS Bedrock Converse API (proxied via Gateway of course). Using the Anthropic model will pretty much always give better performance, so I'd prefer to not present this as an equivalent alternative to the AsyncAnthropicBedrock approach. (The little note at the end of the section linking to the bedrock docs is sufficient)

...
```

=== "Directly to Provider API"

Use the [`AsyncAnthropicBedrock`](https://docs.anthropic.com/en/api/claude-on-amazon-bedrock) client from the `anthropic` package:

```python {test="skip"}
from anthropic import AsyncAnthropicBedrock

from pydantic_ai import Agent
from pydantic_ai.models.anthropic import AnthropicModel
from pydantic_ai.providers.anthropic import AnthropicProvider

bedrock_client = AsyncAnthropicBedrock() # Uses AWS credentials from environment
provider = AnthropicProvider(anthropic_client=bedrock_client)
model = AnthropicModel('claude-sonnet-4-5', provider=provider)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agent = Agent(model)
...
```

!!! note "Bedrock vs BedrockConverseModel"
This approach uses Anthropic's SDK with AWS Bedrock credentials. For an alternative using AWS SDK (boto3) directly, see [`BedrockConverseModel`](bedrock.md).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK here I agree it is is worth mentioning the BedrockConverseModel, and then that page will also show the Gateway option.


### Google Vertex AI

To use Claude models via [Google Cloud Vertex AI](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude):

=== "With Pydantic AI Gateway"

```python {title="<a href='/gateway/' style='float: right;'>Learn about Gateway</a>" test="skip"}
from pydantic_ai import Agent

agent = Agent('gateway/google-vertex:claude-sonnet-4-5@20250514')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as up; this uses the Google Vertex API, rather than the Anthropic API hosted on Vertex.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And let's also have a section pointing out GoogleModel

...
```

=== "Directly to Provider API"

Use the [`AsyncAnthropicVertex`](https://docs.anthropic.com/en/api/claude-on-vertex-ai) client from the `anthropic` package:

```python {test="skip"}
from anthropic import AsyncAnthropicVertex

from pydantic_ai import Agent
from pydantic_ai.models.anthropic import AnthropicModel
from pydantic_ai.providers.anthropic import AnthropicProvider

vertex_client = AsyncAnthropicVertex(region='us-east5', project_id='your-project-id')
provider = AnthropicProvider(anthropic_client=vertex_client)
model = AnthropicModel('claude-sonnet-4-5', provider=provider)
agent = Agent(model)
...
```

### Microsoft Azure

Azure offers Claude models through their "Models as a Service" using an OpenAI-compatible API. Use [`OpenAIModel`][pydantic_ai.models.openai.OpenAIModel] with an Azure-configured client:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we should be able to use AnthropicModel with AsyncAnthropicFoundry now: https://platform.claude.com/docs/en/build-with-claude/claude-in-microsoft-foundry#api-key-authentication

So let's update these docs to be like the other 3, and then also update this:

AsyncAnthropicClient: TypeAlias = AsyncAnthropic | AsyncAnthropicBedrock | AsyncAnthropicVertex


```python {test="skip"}
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
from openai import AsyncAzureOpenAI

from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel
from pydantic_ai.providers.openai import OpenAIProvider

token_provider = get_bearer_token_provider(
DefaultAzureCredential(),
'https://cognitiveservices.azure.com/.default',
)

azure_client = AsyncAzureOpenAI(
azure_ad_token_provider=token_provider,
azure_endpoint='https://your-resource.services.ai.azure.com/api/projects/your-project',
api_version='2025-01-01-preview',
)

provider = OpenAIProvider(openai_client=azure_client)
model = OpenAIModel('claude-sonnet-4-5', provider=provider)
agent = Agent(model)
...
```

See [Azure's Claude documentation](https://learn.microsoft.com/en-us/azure/ai-foundry/foundry-models/how-to/use-foundry-models-claude) for setup instructions.

## Prompt Caching

Anthropic supports [prompt caching](https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching) to reduce costs by caching parts of your prompts. Pydantic AI provides four ways to use prompt caching:
Expand Down
7 changes: 5 additions & 2 deletions pydantic_ai_slim/pydantic_ai/providers/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ def __init__(
api_key: The API key to use for authentication, if not provided, the `ANTHROPIC_API_KEY` environment variable
will be used if available.
base_url: The base URL to use for the Anthropic API.
anthropic_client: An existing [`AsyncAnthropic`](https://github.com/anthropics/anthropic-sdk-python)
client to use. If provided, the `api_key` and `http_client` arguments will be ignored.
anthropic_client: An existing Anthropic client to use. Accepts
[`AsyncAnthropic`](https://github.com/anthropics/anthropic-sdk-python),
[`AsyncAnthropicBedrock`](https://docs.anthropic.com/en/api/claude-on-amazon-bedrock), or
[`AsyncAnthropicVertex`](https://docs.anthropic.com/en/api/claude-on-vertex-ai).
If provided, the `api_key` and `http_client` arguments will be ignored.
http_client: An existing `httpx.AsyncClient` to use for making HTTP requests.
"""
if anthropic_client is not None:
Expand Down