# <img src="https://models.dev/logos/xai.svg" alt="xAI logo" className="inline w-8 h-8 mr-2 align-middle dark:invert dark:brightness-0 dark:contrast-200" />xAI
[EN] Source: https://mastra.ai/models/providers/xai

Access 20 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";

const agent = new 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-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-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({
  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({
  name: "dynamic-agent",
  model: ({ runtimeContext }) => {
    const useAdvanced = runtimeContext.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": "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](https://www.npmjs.com/package/@ai-sdk/xai) for more details.

```bash npm2yarn copy
npm install @ai-sdk/xai
```

For detailed provider-specific documentation, see the [AI SDK xAI provider docs](https://ai-sdk.dev/providers/ai-sdk-providers/xai).


---
title: "Z.AI Coding Plan | Models | Mastra"
description: "Use Z.AI Coding Plan models with Mastra. 5 models available."
---

{/* This file is auto-generated by generate-model-docs.ts - DO NOT EDIT MANUALLY */}