Skip to main content

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
Aliases published by xAI, such as 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

Inspect model.capabilities before setting reasoning or tool options:
  • reasoning.mode is always-on for every model except grok-4.20-0309-non-reasoning, where it is unsupported. xAI lets grok-4.3 skip reasoning with an effort of none, which core-ai does not expose, so grok-4.3 always reasons.
  • supportedEfforts is ['low', 'medium', 'high', 'max'] for models with effort control and empty otherwise. max is sent as xhigh. Other efforts are clamped to the nearest supported value.
  • restrictsSamplingParams is false; temperature and topP are accepted.
  • modalities.input is ['text', 'image'] for every model.
An empty effort list does not mean reasoning output is unavailable. It means the model chooses its own reasoning effort, and the provider never sends an effort parameter.

Strict tool schemas

xAI models report model.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

Use resultToMessage() to carry reasoning into the next turn. On the Responses API, the provider replays encrypted reasoning items. On Chat Completions, it replays reasoning_content.
Encrypted reasoning is only replayed to xAI. Reasoning produced by another provider is sent as plain text.

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 via providerOptions.xai:
Available fields:
  • 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.
Stop sequences and frequency or presence penalties are not available. xAI reasoning models reject these parameters.

Error handling

OpenAI Provider

Responses and Chat Completions reference

core-ai Functions

Learn about generate, stream, and more