# zidane/providers/xai

> The xAI Grok provider, with an API key or SuperGrok OAuth credentials.

Import: `import { … } from 'zidane/providers/xai'`. Source: [src/providers/xai.ts](https://github.com/Tahul/zidane/blob/main/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](https://zidane.si/docs/providers.md).

```ts
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

### `xai`

Function.

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`](https://zidane.si/docs/reference/providers-openai-compat.md#openaiCompat). The only difference is
the bearer:

- **API key**: `XAI_API_KEY` (or `params.apiKey`) → used verbatim.
- **OAuth**:   no static key → [`resolveOAuthApiKey`](https://zidane.si/docs/reference/providers.md#resolveOAuthApiKey) resolves + lazily
  refreshes the stored `xai-oauth` token 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.

```ts
function xai(params?: XaiParams): Provider
```

**Parameters**

- `params?` (`XaiParams`): API key or OAuth credentials, default model, capabilities and extra headers.

**Returns** `Provider`

**Example**

```ts
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.' })
```

Source: [src/providers/xai.ts:58](https://github.com/Tahul/zidane/blob/main/src/providers/xai.ts#L58)

## Interfaces

### `XaiParams`

Interface.

Options for [`xai`](https://zidane.si/docs/reference/providers-xai.md#xai).

```ts
interface XaiParams extends OAuthParams
```

**Properties**

- `access?` (`string`, from `OAuthParams`): OAuth access token supplied at runtime. Used together with `refresh` and `expires` to refresh without a credentials file.
- `account?` (`string`, from `OAuthParams`): Run on ONE stored account of the provider instead of its active one: the
  credential's stable id (`ProviderCredential.id`, see `chat/credentials`).
  Resolved to the slot holding it on every token read and refresh, so
  promoting another account mid-session never moves this provider to it.
  The provider's env key is ignored while pinned (it is the active
  credential's, not this account's). A missing account fails the call with
  [`pinnedAccountMissingError`](https://zidane.si/docs/reference/providers.md#pinnedAccountMissingError) — never a silent fallback to another
  account.
- `apiKey?` (`string`, from `OAuthParams`): 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
  `{ vision: true, imageInToolResult: false }`. Override for text-only
  deployments.
- `defaultModel?` (`string`, default `'grok-4.6'`): Model used when a request does not set one.
- `expires?` (`number`, from `OAuthParams`): Expiry of `access` as a Unix timestamp in milliseconds.
- `extraHeaders?` (`Record<string, string>`): Extra HTTP headers sent on every request.
- `refresh?` (`string`, from `OAuthParams`): OAuth refresh token supplied at runtime.

Source: [src/providers/xai.ts:11](https://github.com/Tahul/zidane/blob/main/src/providers/xai.ts#L11)
