Store Chat Messages & State Without Managing Infrastructure.Check Out DialogueDB
Skip to content

xAI

When using xAI models, llm-exe will make POST requests to https://api.x.ai/v1/chat/completions. All models are supported if you pass xai.chat.v1 as the first argument, and then specify a model in the options.

llm-exe ships typed shorthands for the most common xAI models so you do not have to remember the exact model strings:

ShorthandDefault model
xai.chat.v1none — set model
xai.grok-2grok-2-latest
xai.grok-3grok-3
xai.grok-3-minigrok-3-mini
xai.grok-4grok-4
xai.grok-4-fastgrok-4-fast-non-reasoning
xai.grok-4-1-fastgrok-4-1-fast-non-reasoning
xai.grok-4.3grok-4.3 (reasoning model)
xai.grok-4.20grok-4.20-0309-non-reasoning
xai.grok-4.20-reasoninggrok-4.20-0309-reasoning
xai.grok-4.5grok-4.5 (reasoning model)
xai.grok-4.6grok-4.6 (reasoning model)

Basic Usage

xAI Chat

ts
const llm = useLlm("xai.chat.v1", {
  model: "grok-4", // specify a model
});

x.ai Chat By Model

ts
const llm = useLlm("xai.grok-4", {
  // other options,
  // no model needed, using grok-4
});
ts
const llm = useLlm("xai.grok-3-mini", {
  // other options,
  // no model needed, using grok-3-mini
});
INFO
You can use the following models using this shorthand:
  • xai.grok-4.3
  • xai.grok-4.20
  • xai.grok-4.20-reasoning
  • xai.grok-4.5
  • xai.grok-4.6
  • xai.grok-3deprecated
    Model "xai.grok-3" was retired by xAI on 2026-05-15 and now redirects to grok-4.3. Migrate to "xai.grok-4.3".
  • xai.grok-4-fastdeprecated
    Model "xai.grok-4-fast" was retired by xAI on 2026-05-15 and now redirects to grok-4.3. Migrate to "xai.grok-4.3".
  • xai.grok-4-1-fastdeprecated
    Model "xai.grok-4-1-fast" was retired by xAI on 2026-05-15 and now redirects to grok-4.3. Migrate to "xai.grok-4.3".
  • xai.grok-2deprecated
    Model "xai.grok-2" is no longer served by xAI and will fail at the API. Migrate to "xai.grok-4.3".
  • xai.grok-3-minideprecated
    Model "xai.grok-3-mini" is deprecated — xAI no longer lists it as a current model. Migrate to "xai.grok-4.3".
  • xai.grok-4deprecated
    Model "xai.grok-4" is deprecated — xAI no longer lists it as a current model. Migrate to "xai.grok-4.3".
Deprecated shorthands still resolve and will continue to work for now, but the underlying provider may stop accepting them at any time. Migrate to a current shorthand when you can.

Authentication

To authenticate, you need to provide an xAI API Key. You can provide the API key various ways, depending on your use case.

  1. Pass in as execute options using xAiApiKey
  2. Pass in as setup options using xAiApiKey
  3. Use a default key by setting an environment variable of XAI_API_KEY

Generally you pass the LLM instance off to an LLM Executor and call that. However, it is possible to interact with the LLM object directly, if you wanted.

ts
// call the LLM directly with a prompt
await llm.call(prompt);

xAI-Specific Options

In addition to the generic options, the following options are xAI-specific and can be passed in when creating a llm function.

OptionTypeDefaultDescription
modelstringThe model to use. Must be specified when using xai.chat.v1.
xAiApiKeystringundefinedAPI key for xAI. Optionally can be set using process.env.XAI_API_KEY
topPnumberundefinedMaps to top_p. See xAI Docs
stopSequencesarrayundefinedMaps to stop. See xAI Docs
frequencyPenaltynumberundefinedMaps to frequency_penalty. See xAI Docs
logitBiasobjectundefinedMaps to logit_bias. See xAI Docs
useJsonbooleanundefinedWhen true, sets response_format to json_object
effortstringundefinedMaps to reasoning_effort. Valid values: "minimal", "low", "medium", "high". Only sent for xAI reasoning models (grok-4.3, grok-4.5, grok-4.6); on any other model, or for a value outside that list, it is silently dropped.

effort and xai.grok-4.20-reasoning

Despite the shorthand name, xai.grok-4.20-reasoning (which resolves to grok-4.20-0309-reasoning) is not currently treated as a reasoning model by llm-exe, so effort is dropped rather than sent as reasoning_effort. There is no error or warning — the request simply goes out without the parameter.

Tracked in llm-exe#732. If you need reasoning_effort on that model today, use xai.grok-4.3, xai.grok-4.5, or xai.grok-4.6.

See xAI API Reference for details on these parameters.