Skip to content

Add withProvider() method for runtime provider configuration#352

Open
inkarnation wants to merge 6 commits intolaravel:0.xfrom
inkarnation:feature/with-provider
Open

Add withProvider() method for runtime provider configuration#352
inkarnation wants to merge 6 commits intolaravel:0.xfrom
inkarnation:feature/with-provider

Conversation

@inkarnation
Copy link
Copy Markdown

This PR adds a fluent withProvider() method to agents, allowing on-the-fly configuration of provider credentials (key, URL, driver). This is specifically useful for multi-tenant applications where you need to use a user's own API key or a custom proxy without touching the global app configuration.

Resolves #105.

// 1. Override credentials for an existing config
(new MyAgent)
    ->withProvider('anthropic', key: $tenantKey)
    ->prompt('Analyze this...');

// 2. Custom proxy or alternative URL
(new MyAgent)
    ->withProvider('anthropic', url: 'https://litellm.proxy.local/v1')
    ->prompt('Analyze this...');

// 3. Register a "virtual" provider at runtime
(new MyAgent)
    ->withProvider('tenant-api', driver: 'openai', key: $tenantKey)
    ->prompt('Analyze this...');

Design Decisions

  • Only provided arguments override the base config. Calling withProvider('openai', key: '...') will still respect your global openai URL and driver settings.
  • An explicit provider passed directly to prompt(provider: '...') will take priority, but it will use that provider's default global config (ignoring the instance-level override).
  • Security / Serialization: These overrides are kept in-memory only. They are not serialized. No queue serialization - runtime API keys must never reach Redis/SQS/database. (Exception is created)

@pushpak1300
Copy link
Copy Markdown
Member

fix conflicts ?

Creates fresh, uncached provider instances when config overrides are
provided. Merges overrides with existing provider config. Throws when
a new provider has no driver. Part of laravel#105.
Allows setting provider credentials (key, url, driver) on agent instances
without mutating global config. Enables safe multi-tenant usage.
Resolves laravel#105.
Throws InvalidArgumentException when withProvider() is combined with
queue() or broadcastOnQueue() to prevent API keys from being serialized
into queue payloads.
@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

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

2 participants