Laravel AI: One Fluent API for OpenAI, Anthropic, Gemini, and More
If you've ever built an app that uses more than one AI provider, you know the pain. Each one—OpenAI, Anthropic, Google's Gemini—has its own SDK, its own method signatures, and its own quirks. Switching between them or supporting multiple options often means writing a bunch of adapter code. It's a hassle that pulls you away from building your actual features.
The Laravel team just dropped a new first-party package that aims to solve this. It’s called Laravel AI, and it provides a single, clean, fluent Laravel API to interact with all the major AI models. Think of it like Laravel's database layer, but for LLMs. You write your prompts one way, and you can decide—or let your users decide—which engine runs under the hood.
What It Does
Laravel AI is a Composer package that unifies multiple AI providers behind a consistent interface. You install it, configure your API keys, and suddenly you can send prompts and handle responses without caring if you're talking to GPT-4, Claude 3, or Gemini Pro. The package handles the differences for you.
It’s built on top of the excellent openai-php/client by Nuno Maduro but extends it significantly with Laravel-specific conveniences, like easy service container integration, managed configuration, and a beautifully fluent query builder-style API.
Why It's Cool
The biggest win is developer ergonomics. Instead of memorizing different SDK patterns, you use one familiar style. Need to switch a feature from OpenAI to Anthropic because of context length? That's now a configuration change, not a code rewrite.
It also embraces the Laravel ecosystem perfectly. Configuration is published to your config folder. You can easily swap providers via environment variables. It uses Laravel's HTTP client under the hood, so you benefit from its debugging and testing utilities. The fluent API makes complex tasks—like defining system messages, tools (functions), or response formats—readable and intuitive.
For example, streaming a response from a model looks as clean as:
$stream = AI::connect('openai')->chat()->createStreamed([
'model' => 'gpt-4',
'messages' => [['role' => 'user', 'content' => 'Tell me a story']],
]);
foreach ($stream as $response) {
echo $response->choices[0]->delta->content;
}
The potential use cases are wide open: multi-provider SaaS platforms, fallback systems for when one provider is down, or simply future-proofing your app so you can easily adopt the next best model.
How to Try It
Getting started is straightforward. Install the package via Composer:
composer require laravel/ai
Next, publish the configuration file:
php artisan vendor:publish --tag=ai-config
This will create a config/ai.php file where you can set up your connections (OpenAI, Anthropic, Gemini) with their API keys. You can then use the AI facade throughout your application.
The GitHub repository has all the details, including a full list of supported providers and comprehensive documentation on the available methods.
Final Thoughts
This package feels like a natural and welcome addition to the Laravel ecosystem. It doesn't try to do anything overly magical—it just abstracts a messy part of modern development into a clean, maintainable interface. If your app uses AI, or you're planning to add it, this will likely save you a decent amount of boilerplate and headache. It’s the kind of tool that makes you wonder how you managed without it.
Follow us for more cool projects: @githubprojects
Repository: https://github.com/laravel/ai