Introducing the Laravel AI SDK

Introducing the Laravel AI SDK

AI should make you faster, not slower. Wrestling with different API endpoints, managing scattered code, and reading through provider-specific documentation kind of defeats the entire point. The Laravel AI SDK, currently in beta, solves this by giving you a unified, Laravel-native way to work with multiple AI providers.

Laravel’s AI software development kit brings OpenAI, Anthropic, Google Gemini, Groq, xAI, and other providers under one API. It handles all the provider-specific details so you can build Laravel apps quickly and easily, from generating text with Claude or ChatGPT to creating images with DALL-E or Gemini, and building complex agent workflows with tools and structured output.

As AI transforms how we build software, we want to ensure no developer is left behind and can enjoy clean, simple workflows that align with Laravel’s passion for a great developer experience.

To try the Laravel AI SDK, install it via Composer and add your API keys:

1composer require laravel/ai
1# .env
2OPENAI_API_KEY=your-key-here
3ANTHROPIC_API_KEY=your-key-here

Laravel AI SDK Provider Support

The Laravel AI SDK currently supports these providers:

Text Generation: OpenAI, Anthropic, Gemini, Groq, xAI

Image Generation: OpenAI, Gemini, xAI

Text-to-Speech: OpenAI, ElevenLabs

Speech-to-Text: OpenAI, ElevenLabs

Embeddings: OpenAI, Gemini, Cohere, Jina

Reranking: Cohere, Jina

Files: OpenAI, Anthropic, Gemini

Support for additional providers is planned.

Building with Agents and the Laravel AI SDK

Agents are the core of the Laravel AI SDK. Agents organize your AI interactions into clean, testable classes with clear responsibilities. Generate an agent with an Artisan command:

1php artisan make:agent SalesCoach

This creates a structured class where you define instructions, conversation context, available tools, and output schemas:

1blog-code-blocks/blog-698729acb1790.php

Once you've created an agent, you can prompt it using the prompt method:

1$response = (new SalesCoach)->prompt('Analyze this sales transcript...');
2 
3return (string) $response;

Need to switch providers? Just pass the provider name:

1$response = (new SalesCoach)->prompt(
2 'Analyze this sales transcript...',
3 provider: 'anthropic',
4);

The AI SDK defaults to sensible model choices (configured in config/ai.php), but you can override them per request. This makes it trivial to test different models and find what works best for your use case.

Agent middleware lets you intercept and modify prompts:

1public function middleware(): array
2{
3 return [
4 new LogPrompts,
5 new FilterSensitiveData,
6 ];
7}

Attributes can be used to configure additional agent behavior:

1#[Provider('anthropic')]
2#[MaxTokens(4096)]
3#[Temperature(0.7)]
4class SalesCoach implements Agent
5{
6 // ...
7}

Structured Output

When you need more than free-form text, agents support structured output using JSON schemas. This is perfect for building features like lead qualification systems, data extraction pipelines, or any workflow that needs predictable, parseable responses.

To instruct your agent to return structured output, implement the HasStructuredOutput interface:

1use Illuminate\Contracts\JsonSchema\JsonSchema;
2use Laravel\Ai\Contracts\HasStructuredOutput;
3 
4class SalesCoach implements Agent, HasStructuredOutput
5{
6 // ...
7 
8 public function schema(JsonSchema $schema): array
9 {
10 return [
11 'score' => $schema->integer()->min(1)->max(10)->required(),
12 'feedback' => $schema->string()->required(),
13 ];
14 }
15}

Then, after prompting the agent, you can access the structured response like an array:

1$response = (new SalesCoach)->prompt('Analyze this sales transcript...');
2 
3$score = $response['score'];
4$feedback = $response['feedback'];

Failover Support

AI providers occasionally experience outages, rate limits, or service disruptions. Model failover automatically switches to a backup provider when your primary choice is unavailable, keeping your application running smoothly.

Pass an array of providers to automatically failover if one is unavailable:

1$response = (new SalesCoach)->prompt(
2 'Analyze this sales transcript...',
3 provider: ['openai', 'anthropic'],
4);

If OpenAI is rate-limited or down, the request automatically fails over to Anthropic.

Agent Tools

You can also give agents access to additional functionality through tools. Create a tool with an Artisan command:

1php artisan make:tool RandomNumberGenerator

Tools define a schema and handle method:

1blog-code-blocks/blog-698729acb1a71.php

Return tools from your agent's tools method, and the model will use them automatically when needed.

Semantic Search and RAG

The AI SDK includes a SimilaritySearch tool that searches vector embeddings in your database. This means you can build knowledge bases and let agents answer questions using your own data with retrieval-augmented generation (RAG). The SimilaritySearch tool uses brand new vector comparison methods offered by Laravel’s query builder:

1use App\Models\Document;
2use Laravel\Ai\Tools\SimilaritySearch;
3 
4public function tools(): iterable
5{
6 return [
7 SimilaritySearch::usingModel(Document::class, 'embedding'),
8 ];
9}

Provider tools like WebSearch, WebFetch, and FileSearch let agents access real-time information and stored documents. These tools are executed by the provider itself, enabling powerful capabilities like searching the web for current events or fetching content from URLs.

Images, Audio, and More

Need to generate marketing visuals, product mockups, or user avatars on the fly? Generate images with a clean API:

1use Laravel\Ai\Image;
2 
3$image = Image::of('A donut sitting on the kitchen counter')
4 ->landscape()
5 ->quality('high')
6 ->generate();
7 
8$path = $image->store();

The SDK supports OpenAI, Gemini, and xAI for image generation. You can even pass reference images as attachments to transform existing photos, and pick their format (for example, square, landscape, or portrait).

Plus, you can create voiceovers, accessibility features, or conversational interfaces by generating audio from text:

1use Laravel\Ai\Audio;
2 
3$audio = Audio::of('I love coding with Laravel.')
4 ->female()
5 ->generate();
6 
7$path = $audio->store();

Build automated meeting notes, call center analytics, or podcast transcription features by transcribing audio files with speaker diarization:

1use Laravel\Ai\Transcription;
2 
3$transcript = Transcription::fromStorage('call.mp3')
4 ->diarize()
5 ->generate();
6 
7foreach ($transcript->segments as $segment) {
8 echo "{$segment->speaker}: {$segment->text}\n";
9}

Streaming

Stream responses in real-time using server-sent events:

1Route::get('/coach', function () {
2 return (new SalesCoach)
3 ->stream('Analyze this sales transcript...')
4 ->then(function (StreamedAgentResponse $response) {
5 // Called when streaming completes...
6 Log::info($response->text);
7 });
8});

Or, broadcast streamed events directly to users using Laravel Reverb and Echo:

1(new SalesCoach)->broadcastOnQueue(
2 'Analyze this sales transcript...',
3 new Channel('channel-name'),
4);

Deep Laravel Integration

The Laravel AI SDK integrates deeply with the framework. For example, you can quickly store generated files with one line:

1$image = Image::of('A sunset over mountains')->generate();
2 
3$path = $image->store(); // Uses your default disk
4$path = $image->storePublicly(); // Public storage
5$path = $image->storeAs('sunset.jpg', 's3'); // Specific disk

This works across all of Laravel's filesystem drivers: local, S3, DigitalOcean Spaces, and Laravel Cloud's Object Storage.

Or, leverage Laravel's powerful queue system to queue the generation of images, audio, transcriptions, and more:

1use Laravel\Ai\Image;
2use Laravel\Ai\Responses\ImageResponse;
3 
4Image::of('A donut sitting on the kitchen counter')
5 ->portrait()
6 ->queue()
7 ->then(function (ImageResponse $image) {
8 $path = $image->store();
9 
10 // ...
11 });

Conversation Memory

The RemembersConversations trait automatically stores and retrieves conversation history. The Laravel AI SDK provides database migrations to create the tables necessary to store conversations and messages:

1use Laravel\Ai\Concerns\RemembersConversations;
2 
3class ChatAgent implements Agent, Conversational
4{
5 use Promptable, RemembersConversations;
6 
7 public function instructions(): string
8 {
9 return 'You are a helpful assistant.';
10 }
11}

Start a conversation:

1$response = (new ChatAgent)->forUser($user)->prompt('Hello!');
2 
3$conversationId = $response->conversationId;

Continue it later:

1$response = (new ChatAgent)
2 ->continue($conversationId, as: $user)
3 ->prompt('Tell me more about that.');

Embeddings and Vector Search

To power semantic search, recommendation engines, or similarity detection in your application, generate vector embeddings using the toEmbeddings method on Laravel's Stringable class:

1use Illuminate\Support\Str;
2 
3$embeddings = Str::of('Napa Valley has great wine.')->toEmbeddings();

Embeddings can be cached to avoid redundant API calls. Enable caching globally in config/ai.php or per request:

1$embeddings = Str::of('Laravel is amazing')->toEmbeddings(cache: true);

Improve search relevance by reranking results based on semantic similarity rather than keyword matching:

1use Laravel\Ai\Reranking;
2 
3$response = Reranking::of($documents)
4 ->limit(5)
5 ->rerank('Laravel tutorials');
6 
7$topResult = $response->first()->document;

Collections also have received a convenient rerank macro:

1$posts = Post::all()->rerank('body', 'Laravel AI tutorials');

Files and Vector Stores

Store large documents, PDFs, or images with AI providers like OpenAI and Google Gemini to reference them across multiple conversations without re-uploading:

1use Laravel\Ai\Files\Document;
2 
3$file = Document::fromPath('/path/to/report.pdf')->put();
4$file = Document::fromStorage('document.pdf')->put();
5$file = Document::fromUpload($request->file('document'))->put();
6 
7return $file->id;

Once files are uploaded, you can also create vector stores for semantic file search:

1use Laravel\Ai\Stores;
2 
3$store = Stores::create('Knowledge Base');
4 
5$store->add(Document::fromPath('/path/to/doc.pdf'));
6$store->add(Document::fromStorage('manual.pdf'));

Use the SDK’s built-in FileSearch provider tool to let agents search your vector stores:

1use Laravel\Ai\Providers\Tools\FileSearch;
2 
3public function tools(): iterable
4{
5 return [
6 new FileSearch(stores: ['store_id']),
7 ];
8}

You can easily filter searches by metadata to refine your results:

1new FileSearch(stores: ['store_id'], where: [
2 'author' => 'Taylor Otwell',
3 'year' => 2026,
4]);

Real-World Example: Lead Extraction

Here's a practical example of the Laravel AI SDK in action for a structured output. Imagine you receive hundreds of contact form submissions and need to quickly identify quality leads, extract their needs, and route them appropriately.

Create a lead extractor agent that processes CSV data:

1blog-code-blocks/blog-698729acb1d8d.php

Process a CSV of submissions and save qualified leads with embeddings:

1use App\Ai\Agents\LeadExtractor;
2use App\Models\Lead;
3use Illuminate\Support\Facades\Storage;
4use Illuminate\Support\Str;
5 
6Route::post('/extract-leads', function () {
7 $csv = Storage::get('leads.csv');
8 
9 $response = (new LeadExtractor)->prompt(
10 "Extract and qualify leads from this CSV: {$csv}"
11 );
12 
13 foreach ($response['leads'] as $lead) {
14 if ($lead['is_qualified']) {
15 Lead::create([
16 'name' => $lead['name'],
17 'email' => $lead['email'],
18 'needs' => $lead['needs'],
19 'qualification_reason' => $lead['qualification_reason'],
20 'embedding' => Str::of(
21 $lead['qualification_reason'] . ' ' . implode(' ', $lead['needs'])
22 )->toEmbeddings(),
23 ]);
24 }
25 }
26 
27 return response()->json(['processed' => count($response['leads'])]);
28});

The agent automatically filters out spam ("hello, hello, why isn't this sending?"), identifies high-value prospects ("series B startup with 2.3 million users"), and extracts specific needs (MySQL, Laravel Private Cloud, Enterprise support) without any manual classification rules.

Now build a conversational interface to search your leads semantically:

1use App\Ai\Agents\LeadSearchAgent;
2use App\Models\Lead;
3use Illuminate\Http\Request;
4 
5Route::get('/search-leads', function (Request $request) {
6 // "Who are my cloud leads that need SOC 2?"
7 return (new LeadSearchAgent)->stream($request->query('query'));
8});

Create the search agent with the SimilaritySearch tool:

1blog-code-blocks/blog-698729acb1e04.php

The agent uses the embeddings to find semantically similar leads, even when the search query doesn't match exact keywords.

The Laravel AI SDK gives you a clean, organized foundation for building AI-powered features. It allows you to really start leveraging AI’s speed in one place, without juggling provider-specific APIs or scattering AI logic throughout your codebase. Everything has a place, and it all works together seamlessly.

Install the Laravel AI SDK and start building:

1composer require laravel/ai

Check out the full documentation for detailed guides on agents, tools, structured output, streaming, and more.

We can't wait to see what you build with the Laravel AI SDK.

Keep reading

Announcement August 13, 2025

Announcing Laravel Boost

Ship Laravel features faster with Boost, a Composer package that accelerates AI-assisted Laravel development by providing the essential context and structure that AI needs to generate high-quality, Laravel-specific code.

Ashley Hindle