By default,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.
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 argument | Interface | Purpose |
|---|---|---|
httpClient | Psr\Http\Client\ClientInterface | Executes HTTP requests. |
requestFactory | Psr\Http\Message\RequestFactoryInterface | Creates PSR-7 RequestInterface instances. |
streamFactory | Psr\Http\Message\StreamFactoryInterface | Creates PSR-7 StreamInterface instances. |
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 asnyholm/psr7 and a PSR-18 client
that meets your requirements, then build an Sdk and hand it to
XAI::create():
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
5xxresponses or connection errors before surfacing failures to application code. - Test doubles — replace the real HTTP client with a
FakeHttpClientin automated tests to exercise your code without hitting the live API.
Testing with a fake client
Theaisdk/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:
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.