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 connects your PHP application to xAI’s Grok language and image models through a clean, fluent API — covering everything from simple text completions to streaming responses, image generation, reasoning, tool calling, and structured output. This page explains what the provider does, how it fits into the SDK’s architecture, and what you need to get started.

What is the xAI Provider?

aisdk/xai is a thin provider that slots into the aisdk/core generation pipeline. Internally it delegates all HTTP communication to the aisdk/openai-compatible adapter — the same shared adapter used by other OpenAI-compatible providers — which means xAI’s API (served at https://api.x.ai/v1) is reached via standard chat-completions and image-generation endpoints without any custom transport code. The public surface area you interact with is the XAI facade. It exposes two static factory methods:
  • XAI::model(string $modelId) — returns a TextModelInterface wired to the named Grok text model.
  • XAI::image(string $modelId) — returns an ImageModelInterface wired to the named Grok image model.
Both are passed directly into Generate::text() or Generate::image() builder chains from aisdk/core, keeping the generation API consistent regardless of which provider you are using. A singleton XAIProvider is created on first use and held by XAI::default(), so you only need to call XAI::create() once — typically at application bootstrap when you want to supply explicit configuration.

Key Features

Text Generation

Generate completions from any Grok text model using the fluent Generate::text() builder. Pass a prompt, optional system instructions, and call ->run() to receive a typed result object.

Streaming

Stream tokens back to the client as they arrive. The same Generate::text() chain supports streaming with no provider-specific changes required — just call ->stream() instead of ->run().

Image Generation

Create images with Grok’s image models via Generate::image(). Control aspect ratio, output count, and provider-specific options like resolution through a consistent API.

Reasoning

Models such as grok-4.3 support extended reasoning. Pass a Reasoning::effort() value to the builder chain to control how much thinking the model applies before responding.

Tool Calling

Register tools with the generation builder and let Grok decide when to invoke them. Tool call results flow back into the conversation automatically through the core pipeline.

Structured Output

Request structured JSON responses that conform to a schema. The core SDK handles serialisation and validation, and Grok models return well-formed objects you can work with directly.

Requirements

Before installing, make sure your environment meets these requirements:
  • PHP 8.3 or higher — the package uses modern PHP features including readonly properties and typed constants.
  • aisdk/core ^0.2 — the shared generation pipeline, builder API, and contracts.
  • aisdk/openai-compatible ^0.2 — the OpenAI-compatible HTTP adapter that aisdk/xai builds on.
Both aisdk/core and aisdk/openai-compatible are pulled in automatically when you install aisdk/xai via Composer, so you do not need to require them separately.

Next Steps

Head to the Installation page to add aisdk/xai to your project and configure your API key.

Build docs developers (and LLMs) love