6 min read

Getting Started with the Laravel AI SDK: Provider-Agnostic AI in Laravel 13

Laravel 13's first-party AI SDK brings provider-agnostic text, image, audio, and embedding generation to PHP developers with a clean, familiar API.

Featured image for "Getting Started with the Laravel AI SDK: Provider-Agnostic AI in Laravel 13"

When Laravel 13 landed on March 17, 2026, the headline feature for a lot of developers was zero breaking changes and the stability of a familiar upgrade path. But buried in the release notes, and arguably more consequential for where PHP web development is heading, was the promotion of the Laravel AI SDK from beta to production-stable. This is a first-party package that gives you a single, provider-agnostic interface for text generation, tool-calling agents, image creation, audio synthesis, and embedding generation. If you have been cobbling together your own AI integrations with raw HTTP clients or vendor-specific SDKs, this is a good moment to see what the official approach looks like.

Why Provider-Agnostic Matters

The AI provider landscape is moving fast. The model that makes sense for your use case today may not be the one you reach for six months from now, and pricing shifts have a way of forcing architectural decisions you did not plan for. Tying your application tightly to one provider’s SDK is a liability in that environment. The Laravel AI SDK follows the same philosophy you see in Laravel’s database layer: you write against a stable, Laravel-native API and configure which driver backs it.

Provider configuration lives in config/ai.php, with support for OpenAI, Anthropic, and Google Gemini out of the box, with a driver interface that allows custom implementations. Swapping providers becomes a config change, not an application rewrite.

Text Generation

The most common use case is text generation, and the SDK keeps it simple. You create an agent class that describes the model, any system instructions, and the tools it can call, then prompt it:

<?php

namespace App\Ai\Agents;

use Laravel\Ai\Agent;

class SupportAgent extends Agent
{
    protected string $model = 'claude-sonnet-4-6';

    protected string $system = 'You are a helpful customer support agent for a SaaS product. 
        Be concise, accurate, and friendly.';
}
use App\Ai\Agents\SupportAgent;

$response = SupportAgent::make()->prompt('How do I reset my API key?');

return (string) $response;

The Agent class handles the provider negotiation, request formatting, and response parsing. The cast to string gives you the raw text content. If you need more control over the response, the result object exposes usage data, finish reasons, and the full message history.

You can also use the lower-level Text facade for one-off generation without defining an agent class:

use Laravel\Ai\Text;

$summary = Text::of($longArticle)
    ->instruct('Summarize this in three bullet points.')
    ->using('gpt-4o')
    ->generate();

return (string) $summary;

Tool-Calling Agents

The more interesting capability is tool calling. You define tools as invokable classes, register them with your agent, and the SDK handles the back-and-forth negotiation with the model automatically:

<?php

namespace App\Ai\Tools;

use App\Models\Order;
use Laravel\Ai\Tool;

class LookupOrder extends Tool
{
    public string $description = 'Looks up an order by ID and returns its status and shipping details.';

    public function __invoke(string $orderId): array
    {
        $order = Order::find($orderId);

        if (!$order) {
            return ['error' => 'Order not found'];
        }

        return [
            'id' => $order->id,
            'status' => $order->status,
            'shipped_at' => $order->shipped_at?->toDateString(),
            'tracking' => $order->tracking_number,
        ];
    }
}
use App\Ai\Agents\SupportAgent;
use App\Ai\Tools\LookupOrder;

$response = SupportAgent::make()
    ->withTools([LookupOrder::class])
    ->prompt('What is the status of order ORD-1042?');

The agent will call LookupOrder with the extracted order ID, receive the result, and incorporate it into a natural language response. You can register multiple tools, and the SDK will call as many as the model decides it needs to answer the question. This is the foundation for building agents that can interact with your application’s data and services without you managing the tool-call loop yourself.

Embeddings are where the AI SDK connects directly to Laravel 13’s new vector search capabilities. You can generate embeddings from any string using the Str helper:

use Illuminate\Support\Str;

$embedding = Str::of('Best practices for Laravel queue workers')->toEmbeddings();

The result is a float array that represents the semantic meaning of the text. Stored in a vector column, embeddings unlock similarity search. Laravel 13 adds whereVectorSimilarTo to the query builder when using a compatible database driver (PostgreSQL with pgvector is the primary target):

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

$results = DB::table('knowledge_base')
    ->whereVectorSimilarTo('embedding', 'How do I configure queue connections?')
    ->orderByVectorSimilarity('embedding', 'How do I configure queue connections?')
    ->limit(5)
    ->get();

The practical application is a retrieval-augmented generation (RAG) workflow: embed your documentation or knowledge base entries, store the vectors, then at query time retrieve the most semantically similar chunks and inject them into your agent’s context. This is how you build a support bot that answers from your actual documentation rather than hallucinating.

Image and Audio Generation

The SDK covers the less common but still useful cases of image and audio generation with a similarly clean API:

use Laravel\Ai\Image;

$image = Image::of('A clean dashboard interface for a SaaS product, minimal design, blue palette')
    ->generate();

// Store the raw binary content
Storage::put('generated/dashboard-preview.png', (string) $image);
use Laravel\Ai\Audio;

$audio = Audio::of('Welcome to your daily briefing. Here is a summary of your open tickets.')
    ->voice('nova')
    ->generate();

Storage::put('audio/briefing.mp3', (string) $audio);

These are not the primary use cases for most Laravel applications, but having them in the same package with the same configuration means you are not pulling in additional SDKs for occasional needs.

Practical Considerations

A few things to keep in mind as you start using the SDK in production. Cost management is the big one. LLM API calls are metered, and tool-calling agents can rack up tokens quickly if you are not careful about limiting tool call iterations. The SDK exposes a maxSteps option on agents to cap how many tool-call rounds it will attempt.

The other consideration is latency. Text generation from a remote API is slow relative to a database query, often in the 500ms to 3 second range for a typical response. Use Laravel’s queue system for any generation that does not need to block the HTTP response. The SDK integrates naturally with queued jobs since the agent is just a PHP class:

<?php

namespace App\Jobs;

use App\Ai\Agents\SummaryAgent;
use App\Models\Article;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;

class GenerateArticleSummary implements ShouldQueue
{
    use Queueable;

    public function __construct(public Article $article) {}

    public function handle(): void
    {
        $summary = SummaryAgent::make()->prompt(
            "Summarize the following article in 2-3 sentences:\n\n{$this->article->body}"
        );

        $this->article->update(['summary' => (string) $summary]);
    }
}

The Laravel AI SDK is a well-considered addition to the framework. It does not try to solve every AI use case or expose every provider-specific capability. What it does is give you a stable, familiar entry point for the capabilities that most web applications will actually need. If you are building on Laravel 13, it deserves a spot in your toolkit sooner rather than later.

Sources