Configuration options for creating and customizing xAI provider instances.
Creates a customized xAI provider instance with specific settings.
/**
* Creates a customized xAI provider instance
* @param options - Configuration options for the provider
* @returns Configured XaiProvider instance
*/
function createXai(options?: XaiProviderSettings): XaiProvider;
interface XaiProviderSettings {
/** Base URL for xAI API calls. Defaults to 'https://api.x.ai/v1' */
baseURL?: string;
/** API key for authentication. Defaults to XAI_API_KEY environment variable */
apiKey?: string;
/** Custom headers to include in requests */
headers?: Record<string, string>;
/** Custom fetch implementation for middleware or testing */
fetch?: FetchFunction;
}
// Required imports for type definitions
import type { FetchFunction } from '@ai-sdk/provider-utils';Usage Examples:
import { createXai } from '@ai-sdk/xai';
// Using default settings
const provider = createXai();
// Custom configuration
const customProvider = createXai({
apiKey: 'your-api-key',
baseURL: 'https://custom-api.example.com/v1',
headers: {
'X-Custom-Header': 'value',
},
});
// Create models with custom provider
const model = customProvider('grok-beta');Pre-configured provider instance using default settings.
/**
* Default xAI provider instance with standard configuration
* Uses XAI_API_KEY environment variable and default base URL
*/
const xai: XaiProvider;Usage Examples:
import { xai } from '@ai-sdk/xai';
// Direct usage with default provider
const model = xai('grok-beta');
const imageModel = xai.image('grok-2-image');The provider automatically loads configuration from environment variables when not explicitly provided.
The provider includes structured error handling for API failures.
interface XaiErrorData {
error: {
message: string;
type?: string | null;
param?: any;
code?: string | number | null;
};
}