zidane/ providers/ xai
The xAI Grok provider, with an API key or SuperGrok OAuth credentials.
import { … } from 'zidane/providers/xai'src/providers/xai.ts
Use xai() when your agent talks to Grok. It reads XAI_API_KEY when you pass no key. Without a key, it uses the stored SuperGrok credentials and refreshes them on each turn. You can pick a stored account by name. See Providers.
import { createAgent } from 'zidane'
import { xai } from 'zidane/providers/xai'
const agent = createAgent({ provider: xai({ defaultModel: 'grok-4.6' }) })
const stats = await agent.run({ prompt: 'Summarize README.md in three bullets.' })
console.log(stats.turns)Functions#
xaiFunction#
xAI Grok provider — supports both a static API key (XAI_API_KEY) and the
SuperGrok / X Premium+ OAuth subscription.
Both auth modes hit the same OpenAI-compatible endpoint (https://api.x.ai/v1
unless XAI_BASE_URL or PI_XAI_BASE_URL overrides it),
so the wire handling is entirely openaiCompat. The only difference is
the bearer:
- API key:
XAI_API_KEY(orparams.apiKey) → used verbatim. - OAuth: no static key →
resolveOAuthApiKeyresolves + lazily refreshes the storedxai-oauthtoken on every turn.
resolveOAuthApiKey unifies the two: a real API key in XAI_API_KEY is
returned as-is, while an OAuth access token (or no env value at all) falls
through to the refreshable stored credentials. So this single factory serves
both paths without branching at construction time.
Parameters
| Name | Type | Description |
|---|---|---|
| params? | XaiParams | API key or OAuth credentials, default model, capabilities and extra headers. |
Returns
Example
import { createAgent } from 'zidane'
import { xai } from 'zidane/providers/xai'
const agent = createAgent({
provider: xai({ apiKey: process.env.XAI_API_KEY }),
})
await agent.run({ prompt: 'Say hello.' })Interfaces#
XaiParamsInterface#
Options for xai.
interface XaiParams extends OAuthParamsProperties
| Name | Type | Default | Description |
|---|---|---|---|
access?from OAuthParams | string | OAuth access token supplied at runtime. Used together with | |
account?from OAuthParams | string | Run on ONE stored account of the provider instead of its active one: the
credential's stable id ( | |
apiKey?from OAuthParams | string | Static API key or OAuth access token sent as is, bypassing credential lookup and refresh. | |
| capabilities? | ProviderCapabilities | Provider capability flags. Grok models are vision-capable; default
| |
| defaultModel? | string | 'grok-4.6' | Model used when a request does not set one. |
expires?from OAuthParams | number | Expiry of | |
| extraHeaders? | Record<string, string> | Extra HTTP headers sent on every request. | |
refresh?from OAuthParams | string | OAuth refresh token supplied at runtime. |