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.

By default, aisdk/xai uses whatever PSR-18 HTTP client is registered with the global Generate facade. For most applications that is perfectly sufficient, but sometimes you need precise control over the transport layer — a specific connection timeout, traffic routed through a proxy, retry logic wrapped around flaky upstream responses, or a deterministic fake client in automated tests. All of these are handled through the Sdk value object.

The Sdk object

aisdk/core defines AiSdk\Support\Sdk, a small value object that bundles three PSR interfaces together:
Constructor argumentInterfacePurpose
httpClientPsr\Http\Client\ClientInterfaceExecutes HTTP requests.
requestFactoryPsr\Http\Message\RequestFactoryInterfaceCreates PSR-7 RequestInterface instances.
streamFactoryPsr\Http\Message\StreamFactoryInterfaceCreates PSR-7 StreamInterface instances.
Passing an Sdk instance via the sdk configuration key wires your custom client directly into the XAIProvider, bypassing the global Generate HTTP client entirely.

Injecting a custom client

Install a PSR-17 factory package such as nyholm/psr7 and a PSR-18 client that meets your requirements, then build an Sdk and hand it to XAI::create():
use AiSdk\Support\Sdk;
use AiSdk\XAI;
use Nyholm\Psr7\Factory\Psr17Factory;
use Your\CustomHttpClient;

$factory = new Psr17Factory;
$sdk = new Sdk(
    httpClient: new CustomHttpClient,
    requestFactory: $factory,
    streamFactory: $factory,
);

XAI::create([
    'apiKey' => 'xai-...',
    'sdk'    => $sdk,
]);
Every request the provider makes will now flow through CustomHttpClient, giving you full control over connection management, headers, and error handling at the transport layer.

Use cases

  • Custom timeouts — configure the HTTP client with a shorter read timeout to avoid blocking workers on slow API responses.
  • HTTP proxies — route traffic through a corporate proxy or an API gateway by configuring the underlying client’s proxy settings.
  • Retry middleware — wrap the client in a middleware that retries on transient 5xx responses or connection errors before surfacing failures to application code.
  • Test doubles — replace the real HTTP client with a FakeHttpClient in automated tests to exercise your code without hitting the live API.

Testing with a fake client

The aisdk/xai test suite ships a FakeHttpClient that implements Psr\Http\Client\ClientInterface. It records the last request it received and returns a pre-configured response, making assertions on request shape straightforward:
use AiSdk\Generate;
use AiSdk\Support\Sdk;
use AiSdk\XAI;
use Nyholm\Psr7\Factory\Psr17Factory;

$client = new FakeHttpClient(200, json_encode([
    'choices' => [['message' => ['content' => 'Hello'], 'finish_reason' => 'stop']],
]));
$factory = new Psr17Factory;
Generate::configure(new Sdk(
    httpClient: $client,
    requestFactory: $factory,
    streamFactory: $factory,
));
XAI::create(['apiKey' => 'xai-test']);
After running the code under test you can inspect what was sent:
// Assert on the raw request headers.
expect($client->lastRequest->getHeaderLine('Authorization'))
    ->toBe('Bearer xai-test');

// Assert on the decoded JSON body.
$body = $client->sentBody();
expect($body['model'])->toBe('grok-4.3')
    ->and($body['stream'])->toBeFalse();
FakeHttpClient::sentBody() decodes the request body from JSON and returns it as an associative array, so you can make precise assertions without parsing raw strings. For a complete walkthrough of the testing patterns used across the SDK — including how to reset state between tests with XAI::reset() and Generate::reset() — see the testing overview.

Build docs developers (and LLMs) love