docs/src/content/en/models/providers/anthropic.mdx
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.
ANTHROPIC_API_KEY=your-api-key
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);
}
<ProviderModelsTable models={[ { "model": "anthropic/claude-3-5-haiku-20241022", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 200000, "maxOutput": 8192, "inputCost": 0.8, "outputCost": 4 }, { "model": "anthropic/claude-3-5-haiku-latest", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 200000, "maxOutput": 8192, "inputCost": 0.8, "outputCost": 4 }, { "model": "anthropic/claude-3-5-sonnet-20240620", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 200000, "maxOutput": 8192, "inputCost": 3, "outputCost": 15 }, { "model": "anthropic/claude-3-5-sonnet-20241022", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 200000, "maxOutput": 8192, "inputCost": 3, "outputCost": 15 }, { "model": "anthropic/claude-3-7-sonnet-20250219", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 200000, "maxOutput": 64000, "inputCost": 3, "outputCost": 15 }, { "model": "anthropic/claude-3-7-sonnet-latest", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 200000, "maxOutput": 64000, "inputCost": 3, "outputCost": 15 }, { "model": "anthropic/claude-3-haiku-20240307", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 200000, "maxOutput": 4096, "inputCost": 0.25, "outputCost": 1.25 }, { "model": "anthropic/claude-3-opus-20240229", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 200000, "maxOutput": 4096, "inputCost": 15, "outputCost": 75 }, { "model": "anthropic/claude-3-sonnet-20240229", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 200000, "maxOutput": 4096, "inputCost": 3, "outputCost": 15 }, { "model": "anthropic/claude-haiku-4-5", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 200000, "maxOutput": 64000, "inputCost": 1, "outputCost": 5 }, { "model": "anthropic/claude-haiku-4-5-20251001", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 200000, "maxOutput": 64000, "inputCost": 1, "outputCost": 5 }, { "model": "anthropic/claude-opus-4-0", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 200000, "maxOutput": 32000, "inputCost": 15, "outputCost": 75 }, { "model": "anthropic/claude-opus-4-1", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 200000, "maxOutput": 32000, "inputCost": 15, "outputCost": 75 }, { "model": "anthropic/claude-opus-4-1-20250805", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 200000, "maxOutput": 32000, "inputCost": 15, "outputCost": 75 }, { "model": "anthropic/claude-opus-4-20250514", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 200000, "maxOutput": 32000, "inputCost": 15, "outputCost": 75 }, { "model": "anthropic/claude-opus-4-5", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 200000, "maxOutput": 64000, "inputCost": 5, "outputCost": 25 }, { "model": "anthropic/claude-opus-4-5-20251101", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 200000, "maxOutput": 64000, "inputCost": 5, "outputCost": 25 }, { "model": "anthropic/claude-opus-4-6", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 1000000, "maxOutput": 128000, "inputCost": 5, "outputCost": 25 }, { "model": "anthropic/claude-sonnet-4-0", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 200000, "maxOutput": 64000, "inputCost": 3, "outputCost": 15 }, { "model": "anthropic/claude-sonnet-4-20250514", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 200000, "maxOutput": 64000, "inputCost": 3, "outputCost": 15 }, { "model": "anthropic/claude-sonnet-4-5", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 200000, "maxOutput": 64000, "inputCost": 3, "outputCost": 15 }, { "model": "anthropic/claude-sonnet-4-5-20250929", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 200000, "maxOutput": 64000, "inputCost": 3, "outputCost": 15 }, { "model": "anthropic/claude-sonnet-4-6", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 1000000, "maxOutput": 64000, "inputCost": 3, "outputCost": 15 } ]} />
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"
}
}
});
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";
}
});
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
}
}
});
<PropertiesTable content={[ { "name": "sendReasoning", "type": "boolean | undefined", "description": "", "isOptional": true }, { "name": "structuredOutputMode", "type": ""outputFormat" | "jsonTool" | "auto" | undefined", "description": "", "isOptional": true }, { "name": "thinking", "type": "{ type: "adaptive"; } | { type: "enabled"; budgetTokens?: number | undefined; } | { type: "disabled"; } | undefined", "description": "", "isOptional": true }, { "name": "disableParallelToolUse", "type": "boolean | undefined", "description": "", "isOptional": true }, { "name": "cacheControl", "type": "{ type: "ephemeral"; ttl?: "5m" | "1h" | undefined; } | undefined", "description": "", "isOptional": true }, { "name": "container", "type": "{ id?: string | undefined; skills?: { type: "anthropic" | "custom"; skillId: string; version?: string | undefined; }[] | undefined; } | undefined", "description": "", "isOptional": true }, { "name": "effort", "type": ""low" | "medium" | "high" | "max" | undefined", "description": "", "isOptional": true }, { "name": "speed", "type": ""fast" | undefined", "description": "", "isOptional": true }, { "name": "contextManagement", "type": "{ edits: ({ type: "clear_01"; trigger?: { type: "input_tokens"; value: number; } | undefined; keep?: "all" | { type: "thinking_turns"; value: number; } | undefined; } | { type: "compact_20260112"; trigger?: { ...; } | undefined; pauseAfterCompaction?: boolean | undefined; instructions?: string | undefined; })[]; } |...", "description": "", "isOptional": true } ]} />
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.