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 XAI class in namespace AiSdk is the main entry point for the xAI PHP provider. It acts as a static facade over an XAIProvider singleton, so you can call XAI::model() or XAI::image() anywhere in your application without manually constructing or passing a provider instance. Internally it uses the RegistersModels trait and holds a single default XAIProvider that is lazily created the first time it is needed.

Namespace and import

use AiSdk\XAI;

Methods

XAI::create()

public static function create(array $config = []): XAIProvider
Creates a new XAIProvider from the given configuration array and stores it as the default singleton, replacing any previously set default.
config
array<string, mixed>
default:"[]"
Associative configuration array passed directly to XAIOptions::fromArray(). Accepted keys are apiKey, baseUrl, headers, and sdk. When a key is omitted the corresponding environment variable (XAI_API_KEY, XAI_BASE_URL) is read automatically.
return
XAIProvider
The newly created (and stored) XAIProvider instance.
$provider = XAI::create([
    'apiKey'  => 'xai-...',
    'baseUrl' => 'https://api.x.ai/v1',
]);

XAI::default()

public static function default(): XAIProvider
Returns the default XAIProvider singleton. If no provider has been created yet, it lazily calls create() with an empty config array, which loads credentials from the XAI_API_KEY environment variable.
return
XAIProvider
The current default XAIProvider instance, auto-created if necessary.
$provider = XAI::default(); // auto-creates from env XAI_API_KEY

XAI::model()

public static function model(string $modelId): TextModelInterface
Returns a TextModelInterface for the given Grok model ID by delegating to the default provider’s textModel() method.
modelId
string
required
The identifier of the Grok language model to use. For example: 'grok-4', 'grok-3-turbo', 'grok-2-vision'.
return
TextModelInterface
A ready-to-use text model instance bound to the requested model ID.
$model = XAI::model('grok-4');

XAI::image()

public static function image(string $modelId): ImageModelInterface
Returns an ImageModelInterface for the given image model ID by delegating to the default provider’s imageModel() method.
modelId
string
required
The identifier of the xAI image model to use. For example: 'grok-imagine-image-quality'.
return
ImageModelInterface
A ready-to-use image model instance bound to the requested model ID.
$model = XAI::image('grok-imagine-image-quality');

XAI::reset()

public static function reset(): void
Clears the stored default provider singleton, setting it back to null. Use this in test teardown hooks to prevent state from leaking between test cases.
afterEach(function () {
    XAI::reset();
});

Build docs developers (and LLMs) love