Chat and text generation using xAI's Grok language models with comprehensive model support and configuration options.
// Required imports for type definitions
import type { LanguageModelV2 } from '@ai-sdk/provider';Multiple ways to create language model instances from the provider.
/**
* Default call method - creates a chat language model
* @param modelId - xAI model identifier
* @returns LanguageModelV2 instance for text generation
*/
(modelId: XaiChatModelId): LanguageModelV2;
/**
* Explicit language model creation
* @param modelId - xAI model identifier
* @returns LanguageModelV2 instance for text generation
*/
languageModel(modelId: XaiChatModelId): LanguageModelV2;
/**
* Chat model creation (alias for default call)
* @param modelId - xAI model identifier
* @returns LanguageModelV2 instance for chat
*/
chat(modelId: XaiChatModelId): LanguageModelV2;Usage Examples:
import { xai } from '@ai-sdk/xai';
import { generateText, streamText } from 'ai';
// Default call method
const model1 = xai('grok-beta');
// Explicit methods
const model2 = xai.languageModel('grok-beta');
const model3 = xai.chat('grok-beta');
// Use with AI SDK functions
const { text } = await generateText({
model: xai('grok-beta'),
prompt: 'Explain quantum computing',
});
// Streaming
const { textStream } = await streamText({
model: xai('grok-4'),
prompt: 'Write a story about space exploration',
});Complete list of supported xAI Grok model identifiers.
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 & {});Model Categories:
Common patterns for using different model types.
Usage Examples:
import { xai } from '@ai-sdk/xai';
import { generateText } from 'ai';
// Fast models for quick responses
const quickResponse = await generateText({
model: xai('grok-3-fast'),
prompt: 'Summarize this in one sentence: ...',
});
// Mini models for cost-effective operations
const costEffective = await generateText({
model: xai('grok-3-mini'),
prompt: 'Simple question answering',
});
// Vision models for image understanding
const visionResponse = await generateText({
model: xai('grok-2-vision'),
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'What do you see in this image?' },
{ type: 'image', image: imageData },
],
},
],
});
// Latest models for best performance
const bestResponse = await generateText({
model: xai('grok-4-latest'),
prompt: 'Complex reasoning task...',
});All xAI language models implement the AI SDK's LanguageModelV2 interface, providing full compatibility with AI SDK functions.
// Returned by all language model methods
interface LanguageModelV2 {
// Full AI SDK LanguageModelV2 interface implementation
// Supports generateText, streamText, generateObject, etc.
}