Using Multiple LLM Providers with the Laravel AI SDK
The Laravel AI SDK ( laravel/ai , v0.8.1) ships with built-in support for 14 AI providers: OpenAI, Anthropic, Google Gemini, Groq, Mistral, DeepSeek, xAI, Ollama, Azure OpenAI, Cohere, OpenRouter, Jina, VoyageAI, and ElevenLabs. That breadth is genuinely useful — but unlocking it correctly requires more than swapping an env variable. This article covers how to configure multiple providers in the…
This article explains how to utilize multiple Large Language Model (LLM) providers within a Laravel application using the Laravel AI SDK. The SDK supports 14 AI providers, including OpenAI, Anthropic, Google Gemini, Groq, Mistral, DeepSeek, xAI, Ollama, Azure OpenAI, Cohere, OpenRouter, Jina, VoyageAI, and ElevenLabs. To configure multiple providers in a single application, the article provides a step-by-step guide.
First, ensure that you have PHP 8.3+ and Laravel 12 or 13 installed along with the laravel/ai package (v0.8.1) in your project. At minimum, you should have two API keys for testing, such as OpenAI and Anthropic keys. To get started, install the SDK via Composer and publish the configuration file using the command `composer require laravel/ai php artisan vendor:publish --tag=ai-config`.
The SDK resolves providers by mapping model strings to provider drivers using the `config/ai.php` file. By default, the configuration reads a single AI_PROVIDER env variable, which is suitable for simple setups. However, for multi-provider applications, you need named provider instances. In the `config/ai.php` file, you can define multiple providers, each with its own driver and API key.
The `.env` file should contain the corresponding API keys, such as `OPENAI_API_KEY`, `ANTHROPIC_API_KEY`, `GROQ_API_KEY`, and `OPENROUTER_API_KEY`.
To select a provider at runtime, the `using()` method of the AI facade can be used with a model identifier that corresponds to a provider driver in the configuration. The SDK will match the model string to the appropriate provider. If there is ambiguity between providers offering the same model name, you can qualify the model string with a provider prefix to avoid confusion.
For better organization and maintainability, the article recommends creating a provider router service class. This service can encapsulate various provider-specific methods, allowing you to call them as needed. For instance, you can implement methods like `fast()`, `precise()`, and `summarise()` that correspond to different providers and their respective strengths.
To handle provider-specific errors, the SDK throws distinct exceptions such as `Laravel\AI\Exceptions\RateLimitedException` and `Laravel\AI\Exceptions\ProviderOverloadedException`. By catching these exceptions and implementing appropriate fallback logic, you can ensure your application remains robust and resilient when working with multiple AI providers.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written; read the original for the full account.
