# ![Anthropic logo](https://models.dev/logos/anthropic.svg)Anthropic

Access 23 Anthropic models through Mastra's model router. Authentication is handled automatically using the `ANTHROPIC_API_KEY` environment variable.

Learn more in the [Anthropic documentation](https://docs.anthropic.com/en/docs/about-claude/models).

```bash
ANTHROPIC_API_KEY=your-api-key
```

```typescript
import { Agent } from "@mastra/core/agent";

const agent = new Agent({
  id: "my-agent",
  name: "My Agent",
  instructions: "You are a helpful assistant",
  model: "anthropic/claude-3-5-haiku-20241022"
});

// Generate a response
const response = await agent.generate("Hello!");

// Stream a response
const stream = await agent.stream("Tell me a story");
for await (const chunk of stream) {
  console.log(chunk);
}
```

## Models

| Model                                  | Context | Tools | Reasoning | Image | Audio | Video | Input $/1M | Output $/1M |
| -------------------------------------- | ------- | ----- | --------- | ----- | ----- | ----- | ---------- | ----------- |
| `anthropic/claude-3-5-haiku-20241022`  | 200K    |       |           |       |       |       | $0.80      | $4          |
| `anthropic/claude-3-5-haiku-latest`    | 200K    |       |           |       |       |       | $0.80      | $4          |
| `anthropic/claude-3-5-sonnet-20240620` | 200K    |       |           |       |       |       | $3         | $15         |
| `anthropic/claude-3-5-sonnet-20241022` | 200K    |       |           |       |       |       | $3         | $15         |
| `anthropic/claude-3-7-sonnet-20250219` | 200K    |       |           |       |       |       | $3         | $15         |
| `anthropic/claude-3-haiku-20240307`    | 200K    |       |           |       |       |       | $0.25      | $1          |
| `anthropic/claude-3-opus-20240229`     | 200K    |       |           |       |       |       | $15        | $75         |
| `anthropic/claude-3-sonnet-20240229`   | 200K    |       |           |       |       |       | $3         | $15         |
| `anthropic/claude-haiku-4-5`           | 200K    |       |           |       |       |       | $1         | $5          |
| `anthropic/claude-haiku-4-5-20251001`  | 200K    |       |           |       |       |       | $1         | $5          |
| `anthropic/claude-opus-4-0`            | 200K    |       |           |       |       |       | $15        | $75         |
| `anthropic/claude-opus-4-1`            | 200K    |       |           |       |       |       | $15        | $75         |
| `anthropic/claude-opus-4-1-20250805`   | 200K    |       |           |       |       |       | $15        | $75         |
| `anthropic/claude-opus-4-20250514`     | 200K    |       |           |       |       |       | $15        | $75         |
| `anthropic/claude-opus-4-5`            | 200K    |       |           |       |       |       | $5         | $25         |
| `anthropic/claude-opus-4-5-20251101`   | 200K    |       |           |       |       |       | $5         | $25         |
| `anthropic/claude-opus-4-6`            | 1.0M    |       |           |       |       |       | $5         | $25         |
| `anthropic/claude-opus-4-7`            | 1.0M    |       |           |       |       |       | $5         | $25         |
| `anthropic/claude-sonnet-4-0`          | 200K    |       |           |       |       |       | $3         | $15         |
| `anthropic/claude-sonnet-4-20250514`   | 200K    |       |           |       |       |       | $3         | $15         |
| `anthropic/claude-sonnet-4-5`          | 200K    |       |           |       |       |       | $3         | $15         |
| `anthropic/claude-sonnet-4-5-20250929` | 200K    |       |           |       |       |       | $3         | $15         |
| `anthropic/claude-sonnet-4-6`          | 1.0M    |       |           |       |       |       | $3         | $15         |

## Advanced configuration

### Custom headers

```typescript
const agent = new Agent({
  id: "custom-agent",
  name: "custom-agent",
  model: {
    id: "anthropic/claude-3-5-haiku-20241022",
    apiKey: process.env.ANTHROPIC_API_KEY,
    headers: {
      "X-Custom-Header": "value"
    }
  }
});
```

### Dynamic model selection

```typescript
const agent = new Agent({
  id: "dynamic-agent",
  name: "Dynamic Agent",
  model: ({ requestContext }) => {
    const useAdvanced = requestContext.task === "complex";
    return useAdvanced
      ? "anthropic/claude-sonnet-4-6"
      : "anthropic/claude-3-5-haiku-20241022";
  }
});
```

## Provider Options

Anthropic supports the following provider-specific options via the `providerOptions` parameter:

```typescript
const response = await agent.generate("Hello!", {
  providerOptions: {
    anthropic: {
      // See available options in the table below
    }
  }
});
```

### Available Options

**sendReasoning** (`boolean | undefined`)

**structuredOutputMode** (`"outputFormat" | "jsonTool" | "auto" | undefined`)

**thinking** (`{ type: "adaptive"; display?: "omitted" | "summarized" | undefined; } | { type: "enabled"; budgetTokens?: number | undefined; } | { type: "disabled"; } | undefined`)

**disableParallelToolUse** (`boolean | undefined`)

**cacheControl** (`{ type: "ephemeral"; ttl?: "5m" | "1h" | undefined; } | undefined`)

**metadata** (`{ userId?: string | undefined; } | undefined`)

**mcpServers** (`{ type: "url"; name: string; url: string; authorizationToken?: string | null | undefined; toolConfiguration?: { enabled?: boolean | null | undefined; allowedTools?: string[] | null | undefined; } | null | undefined; }[] | undefined`)

**container** (`{ id?: string | undefined; skills?: { type: "anthropic" | "custom"; skillId: string; version?: string | undefined; }[] | undefined; } | undefined`)

**toolStreaming** (`boolean | undefined`)

**effort** (`"low" | "medium" | "high" | "xhigh" | "max" | undefined`)

**taskBudget** (`{ type: "tokens"; total: number; remaining?: number | undefined; } | undefined`)

**speed** (`"fast" | "standard" | undefined`)

**inferenceGeo** (`"us" | "global" | undefined`)

**anthropicBeta** (`string[] | undefined`)

**contextManagement** (`{ edits: ({ type: "clear_tool_uses_20250919"; trigger?: { type: "input_tokens"; value: number; } | { type: "tool_uses"; value: number; } | undefined; keep?: { type: "tool_uses"; value: number; } | undefined; clearAtLeast?: { ...; } | undefined; clearToolInputs?: boolean | undefined; excludeTools?: string[] | undefin...`)

## Direct provider installation

This provider can also be installed directly as a standalone package, which can be used instead of the Mastra model router string. View the [package documentation](https://www.npmjs.com/package/@ai-sdk/anthropic) for more details.

**npm**:

```bash
npm install @ai-sdk/anthropic
```

**pnpm**:

```bash
pnpm add @ai-sdk/anthropic
```

**Yarn**:

```bash
yarn add @ai-sdk/anthropic
```

**Bun**:

```bash
bun add @ai-sdk/anthropic
```

For detailed provider-specific documentation, see the [AI SDK Anthropic provider docs](https://ai-sdk.dev/providers/ai-sdk-providers/anthropic).