Skip to main content

Anthropic logoAnthropic

通过 Mastra 的模型路由访问 21 个 Anthropic 模型。身份验证会自动使用 ANTHROPIC_API_KEY 环境变量处理。

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

Anthropic文档中了解更多。

🌐 Learn more in the Anthropic documentation.

.env
ANTHROPIC_API_KEY=your-api-key
src/mastra/agents/my-agent.ts
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);
}

模型
Direct link to 模型

🌐 Models

ModelContextToolsReasoningImageAudioVideoInput $/1MOutput $/1M
anthropic/claude-3-5-haiku-20241022200K$0.80$4
anthropic/claude-3-5-haiku-latest200K$0.80$4
anthropic/claude-3-5-sonnet-20240620200K$3$15
anthropic/claude-3-5-sonnet-20241022200K$3$15
anthropic/claude-3-7-sonnet-20250219200K$3$15
anthropic/claude-3-7-sonnet-latest200K$3$15
anthropic/claude-3-haiku-20240307200K$0.25$1
anthropic/claude-3-opus-20240229200K$15$75
anthropic/claude-3-sonnet-20240229200K$3$15
anthropic/claude-haiku-4-5200K$1$5
anthropic/claude-haiku-4-5-20251001200K$1$5
anthropic/claude-opus-4-0200K$15$75
anthropic/claude-opus-4-1200K$15$75
anthropic/claude-opus-4-1-20250805200K$15$75
anthropic/claude-opus-4-20250514200K$15$75
anthropic/claude-opus-4-5200K$5$25
anthropic/claude-opus-4-5-20251101200K$5$25
anthropic/claude-sonnet-4-0200K$3$15
anthropic/claude-sonnet-4-20250514200K$3$15
anthropic/claude-sonnet-4-5200K$3$15
anthropic/claude-sonnet-4-5-20250929200K$3$15
21 available models

高级配置
Direct link to 高级配置

🌐 Advanced Configuration

自定义头
Direct link to 自定义头

🌐 Custom Headers

src/mastra/agents/my-agent.ts
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"
}
}
});

动态模型选择
Direct link to 动态模型选择

🌐 Dynamic Model Selection

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

提供商选项
Direct link to 提供商选项

🌐 Provider Options

Anthropic 通过 providerOptions 参数支持以下特定于提供商的选项:

🌐 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
}
}
});

可用选项
Direct link to 可用选项

🌐 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 link to 直接提供商安装

🌐 Direct Provider Installation

这个提供程序也可以作为独立包直接安装,可以用来替代 Mastra 模型路由字符串。有关更多详细信息,请查看 包文档

🌐 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

有关详细的特定提供商文档,请参阅 AI SDK Anthropic 提供商文档

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