Installation
Setup
Set your xAI API key as an environment variable:Usage
Streaming
API Reference
ChatXAI
ChatXAI extends ChatOpenAI and inherits all of its parameters. It’s preconfigured to use xAI’s API endpoint.
Name of xAI model to use (e.g.,
grok-4, grok-3.5, grok-vision-beta).Sampling temperature between 0 and 2. Higher values mean more random completions, lower values (like 0.2) mean more focused and deterministic completions.
Maximum number of tokens to generate. Refer to your model’s documentation for limits.
Whether to return log probabilities of output tokens.
Timeout for requests in seconds.
Maximum number of retries for failed requests.
xAI API key. If not provided, reads from
XAI_API_KEY environment variable.Supported Models
- Grok-4: Latest flagship model with advanced capabilities
- Grok-3.5: Balanced performance and speed
- Grok Vision Beta: Multimodal model with vision capabilities
Features
- Text generation
- Function/tool calling
- Vision capabilities (Grok Vision Beta)
- Streaming
- Async support
- Real-time information access (via X/Twitter)
Grok models have unique access to real-time information from X (formerly Twitter), which can be useful for current events and trending topics. Refer to xAI’s documentation for API details.