Laravel Daily's

How to Build an AI Support Chatbot in 5 Minutes with the Laravel AI SDK

hero image

Building a chatbot used to mean fighting with messy Python scripts or brittle API wrappers. You had to manage state, handle token limits, and figure out how to stream responses yourself. It felt like reinventing the wheel every time you wanted a basic chat interface.

Laravel changed that. With the release of the Laravel AI SDK, the world’s most elegant php web framework now treats AI as a first-class citizen. You can ship a production-ready support bot in the time it takes to brew a pot of coffee.

This guide shows you how to build a support agent that remembers users, streams text in real-time, and lives directly in your Laravel application.

The AI SDK Philosophy

The Laravel AI SDK isn't just a wrapper for OpenAI. It is a unified layer. It works with Anthropic, Gemini, Groq, and xAI using the same syntax. If OpenAI goes down, you switch a single line in your .env file. Your code doesn't change.

It introduces the concept of Agents. An Agent is a PHP class that holds instructions, tools, and memory. It turns a raw LLM into a focused employee for your app.

Prerequisites

Before you start, ensure you are running the latest version of Laravel. You will need:

  • PHP 8.2 or higher
  • A Laravel 13+ installation
  • An API key from OpenAI or Anthropic

If you are a php developer tools enthusiast, you likely already have Laravel Herd or Valet set up. If not, get your environment ready first.

Step 1: Installation and Configuration

Start by pulling in the SDK. It integrates seamlessly with the existing Laravel ecosystem.

composer require laravel/ai-sdk

Next, publish the configuration file. This is where you define your default provider and models.

php artisan vendor:publish --tag="ai-config"

In your .env, add your provider key:

AI_PROVIDER=openai
OPENAI_API_KEY=sk-...

Step 2: Defining Your Support Agent

In Laravel, we don't write long strings of prompts in our controllers. We create an Agent class. This keeps your logic clean and testable. Use the Artisan command to scaffold a new agent:

php artisan make:ai-agent SupportAgent

Open app/AI/Agents/SupportAgent.php. This is where you define the "soul" of your bot.

namespace App\AI\Agents;

use Laravel\AI\Agent;

class SupportAgent extends Agent
{
    public function instructions(): string
    {
        return "You are a helpful support assistant for Laravel VuejS. 
                Be polite, concise, and technical. 
                Only answer questions about our web development services.";
    }
}

By defining instructions here, you ensure every interaction follows the same rules. You are building a consistent brand voice.

Laravel AI Content Integration

Step 3: Giving the Bot a Memory

A support bot that forgets what you said two seconds ago is useless. The Laravel AI SDK handles conversation history through Eloquent. It uses traits to link messages to your existing models.

Open your User model and add the HasConversations trait:

use Laravel\AI\Models\Traits\HasConversations;

class User extends Model
{
    use HasConversations;
}

Now, every user has a conversations relationship. When you prompt the agent, you pass the conversation instance. The SDK automatically pulls the last 10 or 20 messages and sends them as context. You don't have to manage the array yourself.

Step 4: Streaming the Response

Users expect a "typing" effect. Waiting 10 seconds for a full paragraph to load feels slow. The AI SDK supports Server-Sent Events (SSE) out of the box.

When you build rest api with php, streaming can be tricky. Laravel makes it a single method call.

use App\AI\Agents\SupportAgent;

public function chat(Request $request)
{
    $agent = new SupportAgent();
    
    return $agent->stream($request->message);
}

On the frontend, you can use the official Laravel AI Javascript client to consume this stream. It works perfectly with Vue or React.

Streaming data tokens illustration

Step 5: Connecting the UI

If you use Livewire, this becomes even simpler. You can bind the stream directly to a public property.

public string $response = '';

public function ask()
{
    $this->response = '';

    (new SupportAgent())->stream($this->message)
        ->onToken(fn ($token) => $this->stream(to: 'response', content: $token));
}

This updates the UI in real-time as tokens arrive from the provider. It is the gold standard for modern AI user experiences.

The Ecosystem Advantage

Why build this in Laravel? Because AI doesn't live in a vacuum. Your bot eventually needs to look up an order status or cancel a subscription.

The SDK allows you to define "Tools." These are just methods on your Agent class. If the AI thinks it needs to check a database, it calls your PHP method, gets the data, and continues the conversation.

You are not just building a chat box. You are building an intelligent interface for your entire application. When paired with Laravel Cloud for deployment and Pulse for monitoring, you have a professional-grade AI stack.

Integrated Laravel Ecosystem Stack

Rapid Shipping

The goal of the Laravel AI SDK is to let you focus on features. You shouldn't spend weeks learning vector mathematics or prompt engineering.

The framework provides:

  • Testing Fakes: Mock AI responses in your test suite so you don't spend money on API calls during CI/CD.
  • Failover: If OpenAI returns a 500, the SDK can automatically fall back to Anthropic.
  • Structured Output: Force the AI to return a specific JSON schema every time.

These are the features that move a project from "side hobby" to "enterprise tool."

Your Next Steps

Building an AI support chatbot is the first step. Next, you might want to look into RAG (Retrieval-Augmented Generation). The AI SDK supports vector search directly through Eloquent. You can feed your entire documentation into a database and let the agent search it before answering.

The tools are ready. The syntax is clean.

Dig into the official documentation to explore more advanced agent configurations. If you are building a SaaS, check out our SaaS Helpful Tools for more inspiration on integrating AI into your product.

We'd love to see what you ship. Whether it is a simple support bot or a complex autonomous agent, Laravel is the best place to build it.

Previous
Real-Time UI with Zero Effort: Mastering usePoll in Inertia 3.x
Next
Laravel Trends 2026: Why Everyone Is Talking About Laravel Cloud (And You Should Too)