xAI Grok provider for the AI SDK containing language model support for the xAI chat and completion APIs
npx @tessl/cli install tessl/npm-ai-sdk--xai@2.0.0The xAI provider enables developers to integrate xAI's Grok language models through the AI SDK framework. It provides a comprehensive TypeScript-based implementation with support for text generation, chat functionality, and image generation using xAI's Grok models.
npm install @ai-sdk/xaiimport { xai, createXai } from '@ai-sdk/xai';
import type {
XaiProvider,
XaiProviderSettings,
XaiProviderOptions,
XaiErrorData
} from '@ai-sdk/xai';
import type { LanguageModelV2, ImageModelV2, ProviderV2 } from '@ai-sdk/provider';
import type { FetchFunction } from '@ai-sdk/provider-utils';For CommonJS:
const { xai, createXai } = require('@ai-sdk/xai');import { xai } from '@ai-sdk/xai';
import { generateText } from 'ai';
// Simple text generation
const { text } = await generateText({
model: xai('grok-beta'),
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});
// Using image generation
const imageModel = xai.image('grok-2-image');The xAI provider is built around several key components:
XaiProvider interface extending AI SDK's ProviderV2Factory functions for creating xAI provider instances with custom configuration.
function createXai(options?: XaiProviderSettings): XaiProvider;
const xai: XaiProvider;
interface XaiProviderSettings {
baseURL?: string;
apiKey?: string;
headers?: Record<string, string>;
fetch?: FetchFunction;
}Chat and text generation using xAI's Grok models with comprehensive model ID support.
// Provider methods for language models
(modelId: XaiChatModelId): LanguageModelV2;
languageModel(modelId: XaiChatModelId): LanguageModelV2;
chat(modelId: XaiChatModelId): LanguageModelV2;
type XaiChatModelId =
| 'grok-code-fast-1'
| 'grok-4' | 'grok-4-0709' | 'grok-4-latest'
| 'grok-3' | 'grok-3-latest' | 'grok-3-fast' | 'grok-3-fast-latest'
| 'grok-3-mini' | 'grok-3-mini-latest' | 'grok-3-mini-fast' | 'grok-3-mini-fast-latest'
| 'grok-2-vision-1212' | 'grok-2-vision' | 'grok-2-vision-latest'
| 'grok-2-image-1212' | 'grok-2-image' | 'grok-2-image-latest'
| 'grok-2-1212' | 'grok-2' | 'grok-2-latest'
| 'grok-vision-beta' | 'grok-beta'
| (string & {});Image generation capabilities using xAI's image models.
image(modelId: XaiImageModelId): ImageModelV2;
imageModel(modelId: XaiImageModelId): ImageModelV2;
type XaiImageModelId = 'grok-2-image' | (string & {});Text embedding model support (currently not supported - throws NoSuchModelError).
textEmbeddingModel(modelId: string): never;Provider-specific options for search parameters and reasoning effort.
interface XaiProviderOptions {
reasoningEffort?: 'low' | 'high';
searchParameters?: {
mode: 'off' | 'auto' | 'on';
returnCitations?: boolean;
fromDate?: string;
toDate?: string;
maxSearchResults?: number;
sources?: Array<
| { type: 'web'; country?: string; excludedWebsites?: string[]; allowedWebsites?: string[]; safeSearch?: boolean; }
| { type: 'x'; xHandles?: string[]; }
| { type: 'news'; country?: string; excludedWebsites?: string[]; safeSearch?: boolean; }
| { type: 'rss'; links: string[]; }
>;
};
}interface XaiProvider extends ProviderV2 {
(modelId: XaiChatModelId): LanguageModelV2;
languageModel(modelId: XaiChatModelId): LanguageModelV2;
chat(modelId: XaiChatModelId): LanguageModelV2;
textEmbeddingModel(modelId: string): never;
image(modelId: XaiImageModelId): ImageModelV2;
imageModel(modelId: XaiImageModelId): ImageModelV2;
}
interface XaiErrorData {
error: {
message: string;
type?: string | null;
param?: any;
code?: string | number | null;
};
}