Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/phpaisdk/xai/llms.txt

Use this file to discover all available pages before exploring further.

The aisdk/xai package is the official xAI provider for the PHP AI SDK. It wraps xAI’s OpenAI-compatible chat completions API so you can generate text from any Grok model using the same fluent builder interface you use with every other provider in the SDK.

Basic usage

Install the package, configure your API key, then call Generate::text() to build a request and ->run() to execute it:
use AiSdk\Generate;
use AiSdk\XAI;

XAI::create(['apiKey' => 'xai-...']);

$result = Generate::text()
    ->model(XAI::model('grok-4'))
    ->instructions('Write short, clear answers.')
    ->prompt('Explain closures in PHP.')
    ->run();

echo $result->text;
XAI::create() reads XAI_API_KEY and XAI_BASE_URL from your environment when no explicit values are passed, so in most applications you only need to call it once at bootstrap time — or skip it entirely if you export those environment variables.

Choosing a model

XAI::model(string $modelId) returns a TextModelInterface instance backed by the XAITextModel class. Pass any concrete model ID that matches one of the wildcard patterns registered in the model catalog:
PatternGenerationNotable capabilities
grok-4*Grok 4text generation, streaming, tool calling, structured output, reasoning, image input
grok-3*Grok 3text generation, streaming, tool calling, structured output, reasoning, image input
grok-2*Grok 2text generation, streaming, tool calling, structured output, image input
The wildcards mean any model ID that starts with the prefix is automatically matched — for example grok-4.3, grok-3-mini, or grok-2-vision are all valid. If you pass an ID that does not match any catalog entry, the SDK falls back to assuming basic text generation support so newly released models work without a catalog update.
// Specific versioned model
$model = XAI::model('grok-4.3');

// Vision-capable Grok 2 variant
$model = XAI::model('grok-2-vision');

The response object

->run() returns a TextModelResponse object with the following fields:
FieldTypeDescription
$result->textstringThe generated text returned by the model.
$result->usage->inputTokensintNumber of tokens in the prompt sent to the API.
$result->usage->outputTokensintNumber of tokens in the completion returned by the API.
$result->providerMetadata['xai']['id']stringThe completion ID assigned by the xAI API (e.g. chatcmpl_xai).
$result->providerMetadata['xai']['model']stringThe model identifier echoed back by the API response.
$result = Generate::text('What is 2 + 2?')
    ->model(XAI::model('grok-4.3'))
    ->run();

echo $result->text;
// "4"

echo $result->usage->inputTokens;
// 12

echo $result->usage->outputTokens;
// 1

echo $result->providerMetadata['xai']['id'];
// "chatcmpl_xai"

echo $result->providerMetadata['xai']['model'];
// "grok-4.3"

Setting a system prompt

Pass a system prompt with ->instructions(). The SDK maps this to the system role message in the chat completions request:
$result = Generate::text()
    ->model(XAI::model('grok-4'))
    ->instructions('You are a concise technical writer. Always use bullet points.')
    ->prompt('What are the benefits of strict types in PHP?')
    ->run();

echo $result->text;

Passing the prompt directly

When you only need a user prompt and no system instructions, you can pass it straight to Generate::text() as a shorthand:
$result = Generate::text('Explain closures in PHP.')
    ->model(XAI::model('grok-4'))
    ->run();

echo $result->text;
This is equivalent to calling ->prompt() separately and keeps simple one-shot requests compact.

Build docs developers (and LLMs) love