Skip to main content

Documentation 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.

The 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.
final class Sdk
{
    public readonly ClientInterface          $httpClient;
    public readonly RequestFactoryInterface  $requestFactory;
    public readonly StreamFactoryInterface   $streamFactory;
    public readonly ?LoggerInterface         $logger;
    public readonly ?ContainerInterface      $container;
    public readonly string                   $userAgent;     // default: 'aisdk-php/1.0'
    public readonly ?Model                   $defaultModel;
}
You rarely construct 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.
MethodArgumentPurpose
withHttpClient()ClientInterfaceSwap the PSR-18 HTTP client (e.g. a custom Guzzle instance with a proxy)
withRequestFactory()RequestFactoryInterfaceOverride the PSR-17 request factory
withStreamFactory()StreamFactoryInterfaceOverride the PSR-17 stream factory
withLogger()LoggerInterfaceAttach a PSR-3 logger for request/response logging
withContainer()ContainerInterfaceProvide a PSR-11 container used to resolve class-based tool handlers
withUserAgent()stringSet a custom User-Agent header value
withDefaultModel()ModelPre-configure a default model for all requests
make()Build and return the Sdk instance

Auto-discovery

When you do not call withHttpClient(), withRequestFactory(), or withStreamFactory(), SdkFactory::make() falls back to php-http/discovery to locate installed PSR implementations automatically:
use AiSdk\Support\SdkFactory;

// All three PSR collaborators are discovered automatically.
$sdk = (new SdkFactory())->make();
guzzlehttp/guzzle and guzzlehttp/psr7 are required dependencies, so discovery succeeds out of the box without any extra configuration.

Three ways to configure

1
Zero config — auto-discovery
2
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.
3
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;
4
Default model — Generate::model()
5
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.
6
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"
7
Full control — Generate::configure()
8
Pass a fully built Sdk for complete control over every PSR collaborator.
9
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

Register the SDK in a service provider and bind Sdk into the container so it can be injected.
namespace App\Providers;

use AiSdk\Generate;
use AiSdk\OpenAI;
use AiSdk\Support\Sdk;
use AiSdk\Support\SdkFactory;
use Illuminate\Support\ServiceProvider;

final class AiSdkServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->singleton(Sdk::class, function () {
            return (new SdkFactory())
                ->withLogger(app('log')->driver())
                ->withContainer($this->app)
                ->withDefaultModel(OpenAI::model(config('ai.default_model', 'gpt-4o')))
                ->make();
        });
    }

    public function boot(): void
    {
        Generate::configure($this->app->make(Sdk::class));
    }
}

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.
use AiSdk\Generate;

Generate::reset(); // Sdk and runtimeFactory are cleared.
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.

Build docs developers (and LLMs) love