> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/vercel/ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Anthropic Provider

> Learn how to use Anthropic's Claude models for text generation and tool calling with the AI SDK.

# Anthropic Provider

The Anthropic provider enables you to use Claude models for advanced text generation, reasoning, and tool calling capabilities.

## Installation

<CodeGroup>
  ```bash npm theme={null}
  npm install @ai-sdk/anthropic
  ```

  ```bash pnpm theme={null}
  pnpm add @ai-sdk/anthropic
  ```

  ```bash yarn theme={null}
  yarn add @ai-sdk/anthropic
  ```

  ```bash bun theme={null}
  bun add @ai-sdk/anthropic
  ```
</CodeGroup>

## Setup

Get your API key from [Anthropic's Console](https://console.anthropic.com/) and set it as an environment variable:

```bash theme={null}
ANTHROPIC_API_KEY=your-api-key
```

## Usage

### Basic Text Generation

```typescript theme={null}
import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';

const { text } = await generateText({
  model: anthropic('claude-3-5-sonnet-20241022'),
  prompt: 'Explain quantum computing in simple terms.',
});
```

### Streaming Responses

```typescript theme={null}
import { anthropic } from '@ai-sdk/anthropic';
import { streamText } from 'ai';

const result = streamText({
  model: anthropic('claude-3-5-sonnet-20241022'),
  prompt: 'Write a short story about AI.',
});

for await (const chunk of result.textStream) {
  process.stdout.write(chunk);
}
```

### Structured Output

```typescript theme={null}
import { anthropic } from '@ai-sdk/anthropic';
import { generateObject } from 'ai';
import { z } from 'zod';

const { object } = await generateObject({
  model: anthropic('claude-3-5-sonnet-20241022'),
  schema: z.object({
    ingredients: z.array(z.string()),
    instructions: z.array(z.string()),
  }),
  prompt: 'Generate a recipe for chocolate chip cookies.',
});
```

## Configuration

### Custom Provider Instance

```typescript theme={null}
import { createAnthropic } from '@ai-sdk/anthropic';

const anthropic = createAnthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
  baseURL: 'https://api.anthropic.com/v1',
});
```

### Provider Options

* **apiKey**: Your Anthropic API key (defaults to `ANTHROPIC_API_KEY` env var)
* **baseURL**: Custom API endpoint URL
* **headers**: Custom headers for requests

## Available Models

### Claude 3.5 Family

* `claude-3-5-sonnet-20241022` - Most capable Claude 3.5 model
* `claude-3-5-haiku-20241022` - Fastest Claude 3.5 model

### Claude 3 Family

* `claude-3-opus-20240229` - Most powerful model
* `claude-3-sonnet-20240229` - Balanced performance
* `claude-3-haiku-20240307` - Fast and cost-effective

## Advanced Features

### Tool Calling

```typescript theme={null}
import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
import { z } from 'zod';

const result = await generateText({
  model: anthropic('claude-3-5-sonnet-20241022'),
  tools: {
    calculator: {
      description: 'Perform calculations',
      parameters: z.object({
        expression: z.string(),
      }),
      execute: async ({ expression }) => {
        return { result: eval(expression) };
      },
    },
  },
  prompt: 'What is 15 multiplied by 24?',
});
```

### Vision

```typescript theme={null}
import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
import fs from 'fs';

const result = await generateText({
  model: anthropic('claude-3-5-sonnet-20241022'),
  messages: [
    {
      role: 'user',
      content: [
        { type: 'text', text: 'Describe this image in detail.' },
        { type: 'image', image: fs.readFileSync('./photo.jpg') },
      ],
    },
  ],
});
```

### PDF Support

```typescript theme={null}
import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
import fs from 'fs';

const result = await generateText({
  model: anthropic('claude-3-5-sonnet-20241022'),
  messages: [
    {
      role: 'user',
      content: [
        { type: 'text', text: 'Summarize this document.' },
        {
          type: 'file',
          data: fs.readFileSync('./document.pdf'),
          mediaType: 'application/pdf',
        },
      ],
    },
  ],
});
```

### Prompt Caching

```typescript theme={null}
import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';

const result = await generateText({
  model: anthropic('claude-3-5-sonnet-20241022'),
  messages: [
    {
      role: 'user',
      content: 'Long context that should be cached...',
      providerOptions: {
        anthropic: {
          cacheControl: { type: 'ephemeral' },
        },
      },
    },
  ],
});
```

### Reasoning Models

```typescript theme={null}
import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';

const { text, reasoningText } = await generateText({
  model: anthropic('claude-opus-4-20250514'),
  prompt: 'How many people will live in the world in 2040?',
  providerOptions: {
    anthropic: {
      thinking: { type: 'enabled', budgetTokens: 12000 },
    },
  },
});

console.log('Reasoning:', reasoningText);
console.log('Answer:', text);
```

## Model Capabilities

| Model                      | Vision | Tools | Structured Output |
| -------------------------- | ------ | ----- | ----------------- |
| claude-3-5-sonnet-20241022 | ✓      | ✓     | ✓                 |
| claude-3-5-haiku-20241022  | ✓      | ✓     | ✓                 |
| claude-3-opus-20240229     | ✓      | ✓     | ✓                 |
| claude-3-sonnet-20240229   | ✓      | ✓     | ✓                 |
| claude-3-haiku-20240307    | ✓      | ✓     | ✓                 |

## Resources

* [Anthropic Documentation](https://docs.anthropic.com/)
* [AI SDK Documentation](/docs)
* [Claude API Reference](https://docs.anthropic.com/claude/reference)
