Skip to content

Add Ai::build() for runtime provider configuration overrides#352

Open
inkarnation wants to merge 5 commits into
laravel:0.xfrom
inkarnation:feature/with-provider
Open

Add Ai::build() for runtime provider configuration overrides#352
inkarnation wants to merge 5 commits into
laravel:0.xfrom
inkarnation:feature/with-provider

Conversation

@inkarnation
Copy link
Copy Markdown

@inkarnation inkarnation commented Apr 5, 2026

Multi-tenant Laravel apps that store per-tenant API credentials in a database have no first-party way to use those credentials per request. The only working pattern today mutates global config(), which leaks between requests under Octane/Swoole.

Fixes #105

Usage

Ai::build() mirrors Cache::build() / Storage::build() — pass a full config array, get a fresh provider instance. Nothing cached under a name, no global state mutated.

use Laravel\Ai\Ai;

// Per-tenant provider built from a full config array
$provider = Ai::build([
    'driver' => 'anthropic',
    'key'    => $tenant->key,
]);

(new MyAgent)->prompt($text, provider: $provider);

// Inline at the call site
(new MyAgent)->prompt($text,
    provider: Ai::build([
        'driver'       => 'openai',
        'key'          => $tenant->key,
        'url'          => $tenant->url,
        'organization' => $tenant->org_id,
    ]),
);

// Routed through an Ai::extend()-registered custom driver
(new MyAgent)->prompt($text,
    provider: Ai::build([
        'driver' => 'my-custom-driver',
        'name'   => 'tenant-x',
        'key'    => $tenant->key,
    ]),
);

// Composable with failover lists alongside named providers
(new MyAgent)->prompt($text, provider: [
    Ai::build(['driver' => 'anthropic', 'key' => $tenant->key]),
    'openai',
]);

@pushpak1300
Copy link
Copy Markdown
Member

fix conflicts ?

@inkarnation inkarnation force-pushed the feature/with-provider branch from 8a2fc2c to 5c5bc99 Compare April 15, 2026 04:40
@inkarnation
Copy link
Copy Markdown
Author

@pushpak1300 sure, done

@pushpak1300 pushpak1300 force-pushed the feature/with-provider branch from f148505 to eab9bc7 Compare May 11, 2026 18:14
@pushpak1300 pushpak1300 changed the title Add withProvider() method for runtime provider configuration Add Ai::provider() for runtime provider configuration overrides May 11, 2026
@taylorotwell
Copy link
Copy Markdown
Member

I wonder if this should work more like: https://laravel.com/docs/13.x/filesystem#on-demand-disks

@pushpak1300 pushpak1300 changed the title Add Ai::provider() for runtime provider configuration overrides Add Ai::build() for runtime provider configuration overrides May 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Runtime provider config — API keys and URLs from database

3 participants