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.

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-2
  • xai.grok-3
  • xai.grok-3-mini
  • xai.grok-4
  • xai.grok-4-fast

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".

See xAI API Reference for details on these parameters.