Overview
The xAI provider connects to the xAI API for Grok models. It uses the Responses API by default and also supports the legacy Chat Completions API.Installation
createXAI()
Create an xAI provider instance.Options
string
Your xAI API key.
string
Custom API base URL. Defaults to
https://api.x.ai/v1.OpenAI
Provide your own configured OpenAI SDK client instance.
Returns
XAIProvider with methods:
chatModel(modelId)— Responses API. Use this for new code.chat.chatModel(modelId)— legacy Chat Completions API.
Responses API behavior
xai.chatModel() sends stateless requests. The provider sets store: false, so xAI does not retain your conversations, and requests encrypted reasoning content so reasoning survives multi-turn conversations without server-side state.
Set providerOptions.xai.store to true to opt in to xAI’s server-side storage.
Supported models
- grok-4.7, grok-4.6, grok-4.5, grok-4.3 — always-on reasoning with effort control
- grok-4.20-0309-reasoning, grok-4.20-multi-agent-0309 — always-on reasoning without effort control
- grok-4.20-0309-non-reasoning — no reasoning
- grok-build-0.1 — coding model with always-on reasoning and no effort control
grok-4.5-latest and grok-4.20, resolve to the same capabilities. Unknown model IDs are treated like grok-4.7: always-on reasoning with effort control.
Provider support
xAI’s server-side tools (web search, X search, code execution, remote MCP) are not available through this provider.
Model capabilities
Inspectmodel.capabilities before setting reasoning or tool options:
reasoning.modeisalways-onfor every model exceptgrok-4.20-0309-non-reasoning, where it isunsupported. xAI letsgrok-4.3skip reasoning with an effort ofnone, which core-ai does not expose, sogrok-4.3always reasons.supportedEffortsis['low', 'medium', 'high', 'max']for models with effort control and empty otherwise.maxis sent asxhigh. Other efforts are clamped to the nearest supported value.restrictsSamplingParamsisfalse;temperatureandtopPare accepted.modalities.inputis['text', 'image']for every model.
Strict tool schemas
xAI models reportmodel.capabilities.tools.strictSchemas as { supported: true }. Tools with strict: true are forwarded per tool with a normalized schema; other tools are sent unchanged. See Strict tool schemas for the schema contract and error reasons.
Examples
Basic chat
Reasoning effort
Multi-turn reasoning preservation
UseresultToMessage() to carry reasoning into the next turn. On the Responses API, the provider replays encrypted reasoning items. On Chat Completions, it replays reasoning_content.
Structured output
Usage reporting
usage.outputTokens includes reasoning tokens on both APIs, and usage.outputTokenDetails.reasoningTokens reports them separately. xAI’s Chat Completions API excludes reasoning tokens from completion_tokens; the provider adds them so totals are comparable across providers.
Provider-specific options
Pass xAI-specific options viaproviderOptions.xai:
- Responses API (
xai.chatModel()):store,include,parallelToolCalls,user,promptCacheKey - Chat Completions API (
xai.chat.chatModel()):parallelToolCalls,seed,user
promptCacheKey routes requests that share a prefix to the same cache, which improves cache hit rates in long conversations.
Error handling
Related
OpenAI Provider
Responses and Chat Completions reference
core-ai Functions
Learn about generate, stream, and more