Laravel AI SDK: Build AI Agents, RAG, and Multimodal Apps using laravel

Laravel AI SDK: Build AI Agents, RAG, and Multimodal Apps using laravel

Explore the Laravel AI SDK and learn how to build AI agents, RAG systems, streaming chat, and multimodal features using a single, unified Laravel package.

For a long time, building AI-powered features in Laravel meant stitching together community packages, manually handling Guzzle requests to OpenAI, and writing complex logic to persist conversation history.

That just changed.

Laravel has officially released the Laravel AI SDK, a first-party, unified API that brings the “Laravel Way” to Large Language Models. Whether you’re using OpenAI, Anthropic, Gemini, or other providers, this SDK turns AI from a third-party integration into a first-class citizen of your application.

At HeyGeeks, we’ve been tracking the shift toward AI-native development. This SDK is one of the biggest leaps forward for PHP developers in years. Here’s why it matters.


One SDK for Every AI Capability

The Laravel AI SDK provides a single, consistent interface for:

  • Text generation
  • AI agents
  • Image generation
  • Text-to-speech
  • Speech-to-text
  • Embeddings
  • Reranking
  • Vector stores
  • File management

Instead of installing separate SDKs and writing adapters, you interact with everything through familiar Laravel patterns.

Install it in seconds:

composer require laravel/ai
php artisan vendor:publish --provider="Laravel\Ai\AiServiceProvider"
php artisan migrate

1. Class-Based Agents: The New Standard

In the AI SDK, the core unit of work is the Agent. Instead of messy controller logic, you define your AI’s behavior in a dedicated PHP class.

php artisan make:agent TeacherAssistant --structured

An Agent encapsulates:

  • Instructions (system prompt)
  • Conversation memory
  • Tools
  • Structured outputs

Example:

class TeacherAssistant implements Agent
{
    use Promptable;
public function instructions(): string
{
    return 'You are a helpful teacher who explains concepts clearly and adjusts difficulty based on the student’s level.';
}

}

Usage:

$response = (new TeacherAssistant)->prompt(
    'Explain what a database index is.'
);

2. Built-In Conversation Memory

Instead of building your own chat tables, just use:

use Laravel\Ai\Concerns\RemembersConversations;

class TeacherAssistant implements Agent, Conversational { use Promptable, RemembersConversations; }

Start a conversation:

$response = (new TeacherAssistant)
    ->forUser($user)
    ->prompt('Teach me about recursion.');

Continue it:

$response = (new TeacherAssistant)
    ->continue($conversationId, as: $user)
    ->prompt('Give me a simple example.');

Laravel handles:

  • Message storage
  • Context loading
  • Conversation tracking

3. Tools & True “Agentic” Behavior

Agents can call tools inside your Laravel app.

public function tools(): iterable
{
    return [
        new RandomNumberGenerator,
        SimilaritySearch::usingModel(Document::class, 'embedding'),
    ];
}

This allows your agent to:

  • Query your database
  • Trigger business logic
  • Perform calculations
  • Call external services

Provider Tools

The SDK also supports native provider tools:

  • WebSearch
  • WebFetch
  • FileSearch

These let the model browse the web or search documents without custom code.


4. Structured Output That Actually Works

Instead of parsing messy text, you can enforce a schema:

public function schema(JsonSchema $schema): array
{
    return [
        'explanation' => $schema->string()->required(),
        'difficulty' => $schema->string()->required(),
        'quiz_question' => $schema->string()->required(),
    ];
}

Then:

$response = (new TeacherAssistant)->prompt('Teach arrays to a beginner.');

$question = $response[‘quiz_question’];

This is critical for:

  • Dashboards
  • Automation
  • API responses
  • Data pipelines

5. RAG and Vector Search Out-of-the-Box

Retrieval-Augmented Generation (RAG) is usually the hardest part of AI apps. The Laravel AI SDK simplifies it with native vector support.

Generate embeddings:

$embeddings = Str::of('Laravel is a PHP framework.')
    ->toEmbeddings();

Query similar content:

Document::query()
    ->whereVectorSimilarTo('embedding', 'Laravel tutorials')
    ->limit(10)
    ->get();

Expose it to an agent:

SimilaritySearch::usingModel(Document::class, 'embedding');

Now your agent can search your knowledge base automatically.


6. Streaming, Broadcasting, and Queueing

The SDK supports real-time AI responses using SSE:

return (new TeacherAssistant)
    ->stream('Explain how HTTP works.');

You can also:

  • Broadcast responses via WebSockets
  • Queue long AI jobs
  • Process heavy tasks in the background
(new TeacherAssistant)
    ->queue('Create a lesson plan about sorting algorithms.')
    ->then(fn ($response) => ...);

7. Multimodal APIs (Images, Audio, Transcription)

The SDK isn’t just for text.

Image generation

$image = Image::of('A futuristic city')
    ->landscape()
    ->generate();

Text-to-speech

$audio = Audio::of('Hello Laravel AI')
    ->female()
    ->generate();

Speech-to-text

$transcript = Transcription::fromStorage('audio.mp3')
    ->generate();

8. Vector Stores and File-Based RAG

You can create vector stores for document search:

$store = Stores::create('Knowledge Base');
$store->add(Document::fromPath('/docs/manual.pdf'));

Then attach it to an agent:

new FileSearch(stores: [$store->id]);

This enables full file-based RAG systems inside Laravel.


9. Automatic Failover Between Providers

AI providers fail. Rate limits happen.

Laravel handles it automatically:

$response = (new TeacherAssistant)->prompt(
    'Explain stacks vs queues.',
    provider: ['anthropic', 'openai']
);

No custom retry logic required.


10. Testing the Non-Deterministic

You can fake AI responses in tests:

TeacherAssistant::fake([
    'A stack is a LIFO data structure...',
]);

TeacherAssistant::assertPrompted(‘Explain stacks.’);

This makes AI features:

  • Predictable
  • Testable
  • CI-friendly

Links

Related posts

Explore more product news and best practices for teams building with HeyGeeks.