Skip to main content

xAI logoxAI

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

🌐 Access 22 xAI models through Mastra's model router. Authentication is handled automatically using the XAI_API_KEY environment variable.

xAI 文档中了解更多。

🌐 Learn more in the xAI documentation.

.env
XAI_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: "xai/grok-2"
});

// 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
xai/grok-2131K$2$10
xai/grok-2-1212131K$2$10
xai/grok-2-latest131K$2$10
xai/grok-2-vision8K$2$10
xai/grok-2-vision-12128K$2$10
xai/grok-2-vision-latest8K$2$10
xai/grok-3131K$3$15
xai/grok-3-fast131K$5$25
xai/grok-3-fast-latest131K$5$25
xai/grok-3-latest131K$3$15
xai/grok-3-mini131K$0.30$0.50
xai/grok-3-mini-fast131K$0.60$4
xai/grok-3-mini-fast-latest131K$0.60$4
xai/grok-3-mini-latest131K$0.30$0.50
xai/grok-4256K$3$15
xai/grok-4-1-fast2.0M$0.20$0.50
xai/grok-4-1-fast-non-reasoning2.0M$0.20$0.50
xai/grok-4-fast2.0M$0.20$0.50
xai/grok-4-fast-non-reasoning2.0M$0.20$0.50
xai/grok-beta131K$5$15
xai/grok-code-fast-1256K$0.20$2
xai/grok-vision-beta8K$5$15
22 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: "xai/grok-2",
apiKey: process.env.XAI_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
? "xai/grok-vision-beta"
: "xai/grok-2";
}
});

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

🌐 Provider Options

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

🌐 xAI supports the following provider-specific options via the providerOptions parameter:

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

可用选项
Direct link to 可用选项

🌐 Available Options

reasoningEffort?:

"low" | "high" | undefined

parallel_function_calling?:

boolean | undefined

searchParameters?:

{ mode: "off" | "auto" | "on"; returnCitations?: boolean | undefined; fromDate?: string | undefined; toDate?: string | undefined; maxSearchResults?: number | undefined; sources?: ({ ...; } | ... 2 more ... | { ...; })[] | 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/xai

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

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