AI integration used to be a chore. You had to juggle API keys, manage complex prompts, and handle messy JSON responses. Laravel changed that. With the launch of the official Laravel AI SDK, the php web framework you love now has first-class support for artificial intelligence.
You can now build a production-ready chatbot in minutes. This isn't just a wrapper around an API. It is a complete toolkit for building intelligent features. This guide shows you exactly how to ship a chatbot using the latest php developer tools.
The Core Concept: Meet the Agent
In the Laravel AI SDK, everything revolves around Agents. An Agent is a dedicated PHP class. It encapsulates instructions, tools, and memory.
Think of an Agent as a specialized digital employee. One Agent might handle customer support. Another might analyze technical documentation. By keeping these concerns in separate classes, your code stays clean and maintainable.

Step 1: Install the SDK
First, you need a Laravel 13 project. Ensure you are running PHP 8.3 or higher. Open your terminal and pull in the SDK.
composer require laravel/ai
After the installation finishes, publish the configuration file. This file lets you manage your AI providers in one place.
php artisan vendor:publish --provider="Laravel\Ai\AiServiceProvider"
php artisan migrate
The migration creates tables for conversation history. This allows your chatbot to "remember" what the user said two minutes ago. It handles the state so you don't have to.
Step 2: Configure Your Provider
The SDK supports OpenAI, Anthropic, Gemini, and more. Choose your favorite and add the key to your .env file.
OPENAI_API_KEY=sk-your-key-here
AI_DEFAULT_PROVIDER=openai
Laravel handles the rest. You can swap providers later without changing a single line of application logic. This flexibility is a hallmark of the Laravel ecosystem.
Step 3: Create Your First Agent
Use the Artisan command to scaffold a new Agent. We will build a support bot for a fictional store.
php artisan make:agent SupportAgent
Open app/Agents/SupportAgent.php. This is where you define the "brain" of your chatbot. Use the $instructions property to set the tone and boundaries.
namespace App\Agents;
use Laravel\Ai\Agents\Agent;
class SupportAgent extends Agent
{
public string $instructions = '
You are a helpful support assistant for our online store.
Be concise. If you do not know an answer, refer the user to human support.
Always maintain a friendly, professional tone.
';
public string $model = 'gpt-4o';
}
This class-based approach is superior to inline prompts. It allows you to use dependency injection and keep your business logic focused.
Step 4: Build the API Endpoint
Now you need a way for your frontend to talk to the Agent. You can quickly build rest api with php using a simple controller.
php artisan make:controller ChatController
In your ChatController, instantiate the Agent and prompt it.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Agents\SupportAgent;
class ChatController extends Controller
{
public function __invoke(Request $request)
{
$message = $request->input('message');
$agent = new SupportAgent();
$response = $agent->prompt($message);
return response()->json([
'reply' => $response->text(),
]);
}
}
The prompt() method handles the entire round-trip to the AI provider. It automatically includes the conversation history stored in your database.

Step 5: Real-Time Streaming
Static responses are fine, but users expect a "typing" feel. The Laravel AI SDK supports streaming out of the box.
If you are using Livewire, you can stream the response directly to the browser. This creates a much more engaging user experience.
public function sendMessage($userMessage)
{
$agent = new SupportAgent();
$agent->stream()->prompt($userMessage, function ($chunk) {
$this->stream(to: 'chat-response', content: $chunk, replace: false);
});
}
This pattern keeps the connection open and pushes bits of text as they arrive. It feels fast and modern.
Advanced Feature: Retrieval-Augmented Generation (RAG)
A chatbot is only as good as its data. Sometimes general knowledge isn't enough. You need it to know your specific documentation or product list.
The AI SDK includes built-in support for embeddings and vector search. You can index your local Markdown files or database records into a vector store.
When a user asks a question, the Agent performs a semantic search. It finds the relevant snippets and uses them to construct an answer. This ensures the AI doesn't hallucinate facts about your business.

Testing Your Agent
Testing AI can be unpredictable. You don't want to spend money on API calls every time you run your test suite.
The Laravel AI SDK provides an AgentFake. This allows you to mock responses and ensure your logic works without hitting the internet.
use Laravel\Ai\Testing\AgentFake;
use App\Agents\SupportAgent;
public function test_it_returns_a_support_reply()
{
AgentFake::make(SupportAgent::class)
->withResponse('How can I help you today?');
$this->postJson('/api/chat', ['message' => 'Hello'])
->assertJsonPath('reply', 'How can I help you today?');
}
This brings the same "joyful development" experience to AI that Laravel brought to standard web apps.
Deployment on Laravel Cloud
Building is only half the battle. You need to ship. Laravel Cloud is the perfect home for AI applications.
AI agents often perform long-running tasks. Laravel Cloud handles the managed infrastructure, auto-scaling, and monitoring for you. You don't have to worry about server timeouts or memory limits.
With Nightwatch integration, you can monitor your AI costs and token usage in real-time. This visibility is crucial for scaling startups and enterprises alike.

Conclusion
The Laravel AI SDK removes the barriers to entry for AI development. You no longer need to be a data scientist to build intelligent features.
Start with a simple Agent. Add tools as you go. Use the robust ecosystem to handle the boring parts like authentication and deployment.
The next wave of web applications will be AI-driven. Laravel ensures you are ready to lead that charge. We would love to see what you build.