跳到主要内容

Anthropic 标志Anthropic

通过 Mastra 的模型路由器访问 13 个 Anthropic 模型。系统会自动使用 ANTHROPIC_API_KEY 环境变量完成身份验证。

更多信息请参阅Anthropic 文档

.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-fable-5',
})

// 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)
}

模型
模型的直接链接

ModelContextToolsReasoningImageAudioVideoInput $/1MOutput $/1M
anthropic/claude-fable-51.0M$10$50
anthropic/claude-haiku-4-5200K$1$5
anthropic/claude-haiku-4-5-20251001200K$1$5
anthropic/claude-opus-4-5200K$5$25
anthropic/claude-opus-4-5-20251101200K$5$25
anthropic/claude-opus-4-61.0M$5$25
anthropic/claude-opus-4-71.0M$5$25
anthropic/claude-opus-4-81.0M$5$25
anthropic/claude-opus-51.0M$5$25
anthropic/claude-sonnet-4-51.0M$3$15
anthropic/claude-sonnet-4-5-202509291.0M$3$15
anthropic/claude-sonnet-4-61.0M$3$15
anthropic/claude-sonnet-51.0M$2$10
13 available models

高级配置
高级配置的直接链接

自定义请求头
自定义请求头的直接链接

src/mastra/agents/my-agent.ts
const agent = new Agent({
id: 'custom-agent',
name: 'custom-agent',
model: {
id: 'anthropic/claude-fable-5',
apiKey: process.env.ANTHROPIC_API_KEY,
headers: {
'X-Custom-Header': 'value',
},
},
})

动态选择模型
动态选择模型的直接链接

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-5' : 'anthropic/claude-fable-5'
},
})

Provider 选项
Provider 选项的直接链接

Anthropic 通过 providerOptions 参数支持以下 Provider 特有选项:

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

可用选项
可用选项的直接链接

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

fallbacks?:

"default" | { model: string; max_tokens?: number | undefined; thinking?: Record<string, unknown> | undefined; output_config?: Record<string, unknown> | undefined; speed?: "fast" | "standard" | undefined; }[] | 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...

直接安装 Provider
直接安装 Provider的直接链接

你也可以直接将此 Provider 安装为独立 package,用它代替 Mastra 模型路由字符串。详情请参阅package 文档

npm install @ai-sdk/anthropic

有关此 Provider 的详细说明,请参阅 AI SDK Anthropic Provider 文档