# ![xAI logo](https://models.dev/logos/xai.svg)xAI Access 22 xAI models through Mastra's model router. Authentication is handled automatically using the `XAI_API_KEY` environment variable. Learn more in the [xAI documentation](https://docs.x.ai/docs/models). ```bash XAI_API_KEY=your-api-key ``` ```typescript 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); } ``` ## Models | Model | Context | Tools | Reasoning | Image | Audio | Video | Input $/1M | Output $/1M | | --------------------------------- | ------- | ----- | --------- | ----- | ----- | ----- | ---------- | ----------- | | `xai/grok-2` | 131K | | | | | | $2 | $10 | | `xai/grok-2-1212` | 131K | | | | | | $2 | $10 | | `xai/grok-2-latest` | 131K | | | | | | $2 | $10 | | `xai/grok-2-vision` | 8K | | | | | | $2 | $10 | | `xai/grok-2-vision-1212` | 8K | | | | | | $2 | $10 | | `xai/grok-2-vision-latest` | 8K | | | | | | $2 | $10 | | `xai/grok-3` | 131K | | | | | | $3 | $15 | | `xai/grok-3-fast` | 131K | | | | | | $5 | $25 | | `xai/grok-3-fast-latest` | 131K | | | | | | $5 | $25 | | `xai/grok-3-latest` | 131K | | | | | | $3 | $15 | | `xai/grok-3-mini` | 131K | | | | | | $0.30 | $0.50 | | `xai/grok-3-mini-fast` | 131K | | | | | | $0.60 | $4 | | `xai/grok-3-mini-fast-latest` | 131K | | | | | | $0.60 | $4 | | `xai/grok-3-mini-latest` | 131K | | | | | | $0.30 | $0.50 | | `xai/grok-4` | 256K | | | | | | $3 | $15 | | `xai/grok-4-1-fast` | 2.0M | | | | | | $0.20 | $0.50 | | `xai/grok-4-1-fast-non-reasoning` | 2.0M | | | | | | $0.20 | $0.50 | | `xai/grok-4-fast` | 2.0M | | | | | | $0.20 | $0.50 | | `xai/grok-4-fast-non-reasoning` | 2.0M | | | | | | $0.20 | $0.50 | | `xai/grok-beta` | 131K | | | | | | $5 | $15 | | `xai/grok-code-fast-1` | 256K | | | | | | $0.20 | $2 | | `xai/grok-vision-beta` | 8K | | | | | | $5 | $15 | ## Advanced Configuration ### Custom Headers ```typescript 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" } } }); ``` ### Dynamic Model Selection ```typescript 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"; } }); ``` ## Provider Options xAI supports the following provider-specific options via the `providerOptions` parameter: ```typescript const response = await agent.generate("Hello!", { providerOptions: { xai: { // See available options in the table below } } }); ``` ### 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 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](https://www.npmjs.com/package/@ai-sdk/xai) for more details. **npm**: ```bash npm install @ai-sdk/xai ``` **pnpm**: ```bash pnpm add @ai-sdk/xai ``` **Yarn**: ```bash yarn add @ai-sdk/xai ``` **Bun**: ```bash bun add @ai-sdk/xai ``` For detailed provider-specific documentation, see the [AI SDK xAI provider docs](https://ai-sdk.dev/providers/ai-sdk-providers/xai).