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 provider (aisdk/xai) brings xAI’s Grok models into the PHP AI SDK ecosystem. It implements the shared OpenAI-compatible adapter so you can generate text, stream completions, produce images, and leverage Grok’s reasoning capabilities — all from a unified PHP interface.

Installation

Add the provider to your project with a single Composer command

Quickstart

Make your first Grok API call in under five minutes

Guides

Explore text generation, streaming, image generation, and reasoning

API Reference

Full class and method reference for the xAI provider

What you can do

Text Generation

Generate completions with any Grok text model using the fluent builder API

Streaming

Stream tokens in real time with PHP generators for responsive UIs

Image Generation

Create images with grok-imagine-image-quality with aspect ratio control

Reasoning

Control reasoning effort (low / medium / high) on Grok 3 and 4 models

Tool Calling

Bind PHP functions as tools for function-calling workflows

Provider Options

Pass raw xAI-specific parameters like search_parameters directly

Get started in minutes

1

Install via Composer

composer require aisdk/xai
2

Set your API key

Export the XAI_API_KEY environment variable, or pass it directly when creating the provider.
export XAI_API_KEY=xai-...
3

Generate your first completion

use AiSdk\Generate;
use AiSdk\XAI;

$result = Generate::text()
    ->model(XAI::model('grok-4'))
    ->prompt('Explain closures in PHP.')
    ->run();

echo $result->text;
Requires PHP 8.3+ and Composer. The provider depends on aisdk/core and aisdk/openai-compatible, both installed automatically.

Build docs developers (and LLMs) love