Skip to main content

Anthropic logoAnthropic

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

Learn more in the Anthropic documentation.

ANTHROPIC_API_KEY=your-api-key
import { Agent } from "@mastra/core";

const agent = new 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

ModelImageAudioVideoToolsStreamingContext Window
anthropic/claude-3-5-haiku-20241022200,000
anthropic/claude-3-5-haiku-latest200,000
anthropic/claude-3-5-sonnet-20240620200,000
anthropic/claude-3-5-sonnet-20241022200,000
anthropic/claude-3-7-sonnet-20250219200,000
anthropic/claude-3-7-sonnet-latest200,000
anthropic/claude-3-haiku-20240307200,000
anthropic/claude-3-opus-20240229200,000
anthropic/claude-3-sonnet-20240229200,000
anthropic/claude-haiku-4-5200,000
anthropic/claude-haiku-4-5-20251001200,000
anthropic/claude-opus-4-0200,000
anthropic/claude-opus-4-1200,000
anthropic/claude-opus-4-1-20250805200,000
anthropic/claude-opus-4-20250514200,000
anthropic/claude-sonnet-4-0200,000
anthropic/claude-sonnet-4-20250514200,000
anthropic/claude-sonnet-4-5200,000
anthropic/claude-sonnet-4-5-20250929200,000

Advanced Configuration

Custom Headers

const agent = new 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

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

Provider Options

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

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

Available Options

sendReasoning?:

boolean | undefined

thinking?:

{ type: "enabled" | "disabled"; budgetTokens?: number | undefined; } | undefined

disableParallelToolUse?:

boolean | undefined

cacheControl?:

{ type: "ephemeral"; ttl?: "5m" | "1h" | undefined; } | undefined

container?:

{ id?: string | undefined; skills?: { type: "anthropic" | "custom"; skillId: string; version?: string | undefined; }[] | undefined; } | undefined

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 for more details.

npm install @ai-sdk/anthropic

For detailed provider-specific documentation, see the AI SDK Anthropic provider docs.