Anthropic
Access 11 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);
}
Mastra uses the OpenAI-compatible /chat/completions
endpoint. Some provider-specific features may not be available. Check the Anthropic documentation for details.
Models
Model | Context | Tools | Reasoning | Image | Audio | Video | Input $/1M | Output $/1M |
---|---|---|---|---|---|---|---|---|
anthropic/claude-3-5-sonnet-20241022 | 200K | $3 | $15 | |||||
anthropic/claude-3-5-sonnet-20240620 | 200K | $3 | $15 | |||||
anthropic/claude-3-opus-20240229 | 200K | $15 | $75 | |||||
anthropic/claude-sonnet-4-5-20250929 | 1.0M | $3 | $15 | |||||
anthropic/claude-sonnet-4-20250514 | 200K | $3 | $15 | |||||
anthropic/claude-opus-4-20250514 | 200K | $15 | $75 | |||||
anthropic/claude-3-5-haiku-20241022 | 200K | $0.80 | $4 | |||||
anthropic/claude-3-haiku-20240307 | 200K | $0.25 | $1 | |||||
anthropic/claude-3-7-sonnet-20250219 | 200K | $3 | $15 | |||||
anthropic/claude-opus-4-1-20250805 | 200K | $15 | $75 | |||||
anthropic/claude-3-sonnet-20240229 | 200K | $3 | $15 |
Advanced Configuration
Custom Headers
const agent = new Agent({
name: "custom-agent",
model: {
url: "https://api.anthropic.com/v1/chat/completions",
modelId: "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-3-5-haiku-20241022"
: "anthropic/claude-3-5-sonnet-20240620";
}
});
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