Back to Mastra

xAI | Models

docs/src/content/en/models/providers/xai.mdx

2025-12-189.4 KB
Original Source

xAI

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

Learn more in the xAI documentation.

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

<ProviderModelsTable models={[ { "model": "xai/grok-2", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 131072, "maxOutput": 8192, "inputCost": 2, "outputCost": 10 }, { "model": "xai/grok-2-1212", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 131072, "maxOutput": 8192, "inputCost": 2, "outputCost": 10 }, { "model": "xai/grok-2-latest", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 131072, "maxOutput": 8192, "inputCost": 2, "outputCost": 10 }, { "model": "xai/grok-2-vision", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 8192, "maxOutput": 4096, "inputCost": 2, "outputCost": 10 }, { "model": "xai/grok-2-vision-1212", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 8192, "maxOutput": 4096, "inputCost": 2, "outputCost": 10 }, { "model": "xai/grok-2-vision-latest", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 8192, "maxOutput": 4096, "inputCost": 2, "outputCost": 10 }, { "model": "xai/grok-3", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 131072, "maxOutput": 8192, "inputCost": 3, "outputCost": 15 }, { "model": "xai/grok-3-fast", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 131072, "maxOutput": 8192, "inputCost": 5, "outputCost": 25 }, { "model": "xai/grok-3-fast-latest", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 131072, "maxOutput": 8192, "inputCost": 5, "outputCost": 25 }, { "model": "xai/grok-3-latest", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 131072, "maxOutput": 8192, "inputCost": 3, "outputCost": 15 }, { "model": "xai/grok-3-mini", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 131072, "maxOutput": 8192, "inputCost": 0.3, "outputCost": 0.5 }, { "model": "xai/grok-3-mini-fast", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 131072, "maxOutput": 8192, "inputCost": 0.6, "outputCost": 4 }, { "model": "xai/grok-3-mini-fast-latest", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 131072, "maxOutput": 8192, "inputCost": 0.6, "outputCost": 4 }, { "model": "xai/grok-3-mini-latest", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 131072, "maxOutput": 8192, "inputCost": 0.3, "outputCost": 0.5 }, { "model": "xai/grok-4", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 256000, "maxOutput": 64000, "inputCost": 3, "outputCost": 15 }, { "model": "xai/grok-4-1-fast", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 2000000, "maxOutput": 30000, "inputCost": 0.2, "outputCost": 0.5 }, { "model": "xai/grok-4-1-fast-non-reasoning", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 2000000, "maxOutput": 30000, "inputCost": 0.2, "outputCost": 0.5 }, { "model": "xai/grok-4-fast", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 2000000, "maxOutput": 30000, "inputCost": 0.2, "outputCost": 0.5 }, { "model": "xai/grok-4-fast-non-reasoning", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 2000000, "maxOutput": 30000, "inputCost": 0.2, "outputCost": 0.5 }, { "model": "xai/grok-4.20-beta-latest-non-reasoning", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 2000000, "maxOutput": 30000, "inputCost": 2, "outputCost": 6 }, { "model": "xai/grok-4.20-beta-latest-reasoning", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 2000000, "maxOutput": 30000, "inputCost": 2, "outputCost": 6 }, { "model": "xai/grok-4.20-multi-agent-beta-latest", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": false, "reasoning": true, "contextWindow": 2000000, "maxOutput": 30000, "inputCost": 2, "outputCost": 6 }, { "model": "xai/grok-beta", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 131072, "maxOutput": 4096, "inputCost": 5, "outputCost": 15 }, { "model": "xai/grok-code-fast-1", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 256000, "maxOutput": 10000, "inputCost": 0.2, "outputCost": 1.5 }, { "model": "xai/grok-vision-beta", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 8192, "maxOutput": 4096, "inputCost": 5, "outputCost": 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

<PropertiesTable content={[ { "name": "reasoningEffort", "type": ""low" | "high" | undefined", "description": "", "isOptional": true }, { "name": "parallel_function_calling", "type": "boolean | undefined", "description": "", "isOptional": true }, { "name": "searchParameters", "type": "{ mode: "off" | "auto" | "on"; returnCitations?: boolean | undefined; fromDate?: string | undefined; toDate?: string | undefined; maxSearchResults?: number | undefined; sources?: ({ ...; } | ... 2 more ... | { ...; })[] | undefined; } | undefined", "description": "", "isOptional": true } ]} />

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.

bash
npm install @ai-sdk/xai

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