anthropic.md•1.92 kB
# Anthropic
## Install
To use `AnthropicModel` models, you need to either install `pydantic-ai`, or install `pydantic-ai-slim` with the `anthropic` optional group:
```bash
pip/uv-add "pydantic-ai-slim[anthropic]"
```
## Configuration
To use [Anthropic](https://anthropic.com) through their API, go to [console.anthropic.com/settings/keys](https://console.anthropic.com/settings/keys) to generate an API key.
`AnthropicModelName` contains a list of available Anthropic models.
## Environment variable
Once you have the API key, you can set it as an environment variable:
```bash
export ANTHROPIC_API_KEY='your-api-key'
```
You can then use `AnthropicModel` by name:
```python
from pydantic_ai import Agent
agent = Agent('anthropic:claude-3-5-sonnet-latest')
...
```
Or initialise the model directly with just the model name:
```python
from pydantic_ai import Agent
from pydantic_ai.models.anthropic import AnthropicModel
model = AnthropicModel('claude-3-5-sonnet-latest')
agent = Agent(model)
...
```
## `provider` argument
You can provide a custom `Provider` via the `provider` argument:
```python
from pydantic_ai import Agent
from pydantic_ai.models.anthropic import AnthropicModel
from pydantic_ai.providers.anthropic import AnthropicProvider
model = AnthropicModel(
'claude-3-5-sonnet-latest', provider=AnthropicProvider(api_key='your-api-key')
)
agent = Agent(model)
...
```
## Custom HTTP Client
You can customize the `AnthropicProvider` with a custom `httpx.AsyncClient`:
```python
from httpx import AsyncClient
from pydantic_ai import Agent
from pydantic_ai.models.anthropic import AnthropicModel
from pydantic_ai.providers.anthropic import AnthropicProvider
custom_http_client = AsyncClient(timeout=30)
model = AnthropicModel(
'claude-3-5-sonnet-latest',
provider=AnthropicProvider(api_key='your-api-key', http_client=custom_http_client),
)
agent = Agent(model)
...
```