TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/phpaisdk/core/llms.txt
Use this file to discover all available pages before exploring further.
Sdk class is the immutable runtime object that every request flows through. It bundles a PSR-18 HTTP client, PSR-17 request and stream factories, an optional PSR-3 logger, an optional PSR-11 container, a user-agent string, and an optional default model into a single value. SdkFactory is the fluent builder that constructs it — auto-discovering PSR collaborators via php-http/discovery when you haven’t provided them explicitly.
The Sdk object
Sdk is a final readonly class. Once constructed, every property is immutable. The only mutation point is withDefaultModel(), which returns a new Sdk copy with the given model attached.
Sdk directly — use SdkFactory or the Generate facade shortcuts instead.
SdkFactory builder methods
SdkFactory exposes a fluent with*() API. Every setter returns $this, so calls can be chained. Call make() at the end to get a configured Sdk.
| Method | Argument | Purpose |
|---|---|---|
withHttpClient() | ClientInterface | Swap the PSR-18 HTTP client (e.g. a custom Guzzle instance with a proxy) |
withRequestFactory() | RequestFactoryInterface | Override the PSR-17 request factory |
withStreamFactory() | StreamFactoryInterface | Override the PSR-17 stream factory |
withLogger() | LoggerInterface | Attach a PSR-3 logger for request/response logging |
withContainer() | ContainerInterface | Provide a PSR-11 container used to resolve class-based tool handlers |
withUserAgent() | string | Set a custom User-Agent header value |
withDefaultModel() | Model | Pre-configure a default model for all requests |
make() | — | Build and return the Sdk instance |
Auto-discovery
When you do not callwithHttpClient(), withRequestFactory(), or withStreamFactory(), SdkFactory::make() falls back to php-http/discovery to locate installed PSR implementations automatically:
guzzlehttp/guzzle and guzzlehttp/psr7 are required dependencies, so discovery succeeds out of the box without any extra configuration.
Three ways to configure
If you don’t call
Generate::configure() or Generate::model(), the SDK lazily builds an Sdk on the first request using auto-discovery. This is the simplest path for scripts and quick prototypes.use AiSdk\Generate;
use AiSdk\OpenAI;
// No setup needed — Guzzle is discovered automatically.
$result = Generate::text('Explain closures in PHP.')
->model(OpenAI::model('gpt-4o'))
->run();
echo $result->text;
Call
Generate::model() at your application’s boot time to set a default model. Subsequent calls to Generate::text() and Generate::image() will use it unless overridden per-request.use AiSdk\Generate;
use AiSdk\OpenAI;
// Typically in a bootstrap file or service provider.
Generate::model(OpenAI::model('gpt-4o'));
// Later, anywhere in your code:
$result = Generate::text('What is the capital of France?')->run();
echo $result->text; // "Paris"
use AiSdk\Generate;
use AiSdk\OpenAI;
use AiSdk\Support\SdkFactory;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$logger = new Logger('aisdk');
$logger->pushHandler(new StreamHandler('php://stdout'));
$sdk = (new SdkFactory())
->withHttpClient(new Client(['timeout' => 30]))
->withRequestFactory(new HttpFactory())
->withStreamFactory(new HttpFactory())
->withLogger($logger)
->withUserAgent('my-app/2.0')
->withDefaultModel(OpenAI::model('gpt-4o'))
->make();
Generate::configure($sdk);
Framework integration
- Laravel
- Symfony
Register the SDK in a service provider and bind
Sdk into the container so it can be injected.Resetting runtime state
Generate::reset() clears the stored Sdk instance and any pending factory configuration, returning the facade to its initial state. This is essential between tests to prevent state leaking from one test into the next.
Always call
Generate::reset() in your test teardown. If you use Pest, put it in an afterEach() hook at the top of your test file. See the Testing page for examples.