{"id":2085,"date":"2026-07-03T10:56:10","date_gmt":"2026-07-03T10:56:10","guid":{"rendered":"https:\/\/debugspot.com\/blogs\/?p=2085"},"modified":"2026-07-03T10:56:10","modified_gmt":"2026-07-03T10:56:10","slug":"laravel-ai-chatbot","status":"publish","type":"post","link":"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/","title":{"rendered":"Laravel AI Chatbot: The Easy 2026 Guide (Laravel 13 AI SDK)"},"content":{"rendered":"<p><strong>Want to add a smart chatbot to your app?<\/strong> Building a <strong>Laravel AI chatbot<\/strong> used to mean juggling raw API calls and glue code. With the brand-new <strong>Laravel 13 AI SDK<\/strong>, you can build a streaming, memory-aware Laravel AI chatbot in minutes &#8211; with a clean, expressive API that works across OpenAI, Anthropic, Gemini and more. This step-by-step 2026 guide shows you exactly how, with full code.<\/p>\n<div style=\"text-align:center;margin:30px 0;\"><a href=\"https:\/\/debugspot.com\/blogs\/category\/laravel\/\" style=\"display:inline-block;background:#7c3aed;color:#ffffff;padding:15px 36px;border-radius:8px;font-size:18px;font-weight:700;text-decoration:none;box-shadow:0 6px 18px rgba(124,58,237,0.4);\">&#128218; More Laravel 13 Tutorials &rarr;<\/a><\/div>\n<figure><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/debugspot.com\/blogs\/wp-content\/uploads\/2026\/07\/laravel-ai-chatbot-featured-v2.png\" alt=\"Build a Laravel AI chatbot with the Laravel 13 AI SDK\" width=\"1200\" height=\"630\" \/><\/figure>\n<div style=\"background:#f6f7fb;border:1px solid #e3e6ef;border-radius:10px;padding:18px 24px;margin:26px 0;\">\n<strong style=\"font-size:18px;\">&#128209; Table of Contents<\/strong><\/p>\n<ul style=\"margin-top:10px;line-height:1.9;\">\n<li><a href=\"#what\">What Is the Laravel AI SDK?<\/a><\/li>\n<li><a href=\"#build\">What You Will Build<\/a><\/li>\n<li><a href=\"#install\">Step 1: Install the Laravel AI SDK<\/a><\/li>\n<li><a href=\"#agent\">Step 4: Create a Chatbot Agent<\/a><\/li>\n<li><a href=\"#memory\">Step 5: Add Conversation Memory<\/a><\/li>\n<li><a href=\"#stream\">Step 7: Stream Replies to the Browser<\/a><\/li>\n<li><a href=\"#providers\">Switch AI Providers<\/a><\/li>\n<li><a href=\"#faq\">Frequently Asked Questions<\/a><\/li>\n<\/ul>\n<\/div>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#What_Is_the_Laravel_AI_SDK\" >What Is the Laravel AI SDK?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#What_You_Will_Build\" >What You Will Build<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Step_1_Install_the_Laravel_AI_SDK\" >Step 1: Install the Laravel AI SDK<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Step_2_Add_Your_AI_Provider_Key\" >Step 2: Add Your AI Provider Key<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Step_3_Your_First_AI_Response\" >Step 3: Your First AI Response<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Step_4_Create_a_Chatbot_Agent\" >Step 4: Create a Chatbot Agent<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Step_5_Add_Conversation_Memory\" >Step 5: Add Conversation Memory<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Step_6_Build_the_Chat_Controller_and_Routes\" >Step 6: Build the Chat Controller and Routes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Step_7_Stream_Replies_to_the_Browser\" >Step 7: Stream Replies to the Browser<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Step_8_A_Simple_Chat_UI\" >Step 8: A Simple Chat UI<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Switch_AI_Providers_in_One_Line\" >Switch AI Providers in One Line<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Give_Your_Chatbot_Tools_Function_Calling\" >Give Your Chatbot Tools (Function Calling)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Structured_Output_for_Reliable_Data\" >Structured Output for Reliable Data<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Security_and_Cost_Tips\" >Security and Cost Tips<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Common_Errors_and_Fixes\" >Common Errors and Fixes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Related_Laravel_Guides\" >Related Laravel Guides<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Frequently_Asked_Questions\" >Frequently Asked Questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#What_is_the_Laravel_AI_SDK\" >What is the Laravel AI SDK?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Do_I_need_Laravel_13_to_build_a_Laravel_AI_chatbot\" >Do I need Laravel 13 to build a Laravel AI chatbot?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Which_AI_providers_does_it_support\" >Which AI providers does it support?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Does_the_chatbot_remember_previous_messages\" >Does the chatbot remember previous messages?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Can_I_stream_replies_like_ChatGPT\" >Can I stream replies like ChatGPT?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Is_the_Laravel_AI_SDK_free\" >Is the Laravel AI SDK free?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-24\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#How_do_I_keep_API_costs_down\" >How do I keep API costs down?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-25\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Can_my_chatbot_perform_actions_not_just_chat\" >Can my chatbot perform actions, not just chat?<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-26\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Next_Steps_After_Your_First_Chatbot\" >Next Steps After Your First Chatbot<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-27\" href=\"https:\/\/debugspot.com\/blogs\/laravel-ai-chatbot\/#Start_Building_Your_Laravel_AI_Chatbot\" >Start Building Your Laravel AI Chatbot<\/a><\/li><\/ul><\/nav><\/div>\n<h2 id=\"what\"><span class=\"ez-toc-section\" id=\"What_Is_the_Laravel_AI_SDK\"><\/span>What Is the Laravel AI SDK?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The Laravel AI SDK is a first-party package, introduced with Laravel 13 in March 2026, that gives you one clean, expressive API for talking to any AI provider &#8211; OpenAI, Anthropic, Gemini, Groq, Mistral, xAI and more. Instead of hand-rolling HTTP requests for each provider, you define <strong>Agents<\/strong> (small PHP classes that hold your instructions, tools, and memory) and call them like any other Laravel service. It handles streaming, conversation history, tool calling, and structured output out of the box, which is exactly what you need to build a real Laravel AI chatbot. You can read the full <a href=\"https:\/\/laravel.com\/docs\/13.x\/ai-sdk\" target=\"_blank\" rel=\"noopener\">Laravel AI SDK documentation<\/a> for the complete reference.<\/p>\n<h2 id=\"build\"><span class=\"ez-toc-section\" id=\"What_You_Will_Build\"><\/span>What You Will Build<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>By the end of this guide you will have a working Laravel AI chatbot that streams replies in real time and remembers the conversation. You need three things:<\/p>\n<ul>\n<li><strong>Laravel 13<\/strong> (this SDK ships with it) running on <strong>PHP 8.3+<\/strong>.<\/li>\n<li>An API key from an AI provider &#8211; a free or paid <strong>OpenAI<\/strong> or <strong>Anthropic<\/strong> key works perfectly.<\/li>\n<li>A few minutes. That is genuinely all it takes.<\/li>\n<\/ul>\n<figure><img decoding=\"async\" src=\"https:\/\/debugspot.com\/blogs\/wp-content\/uploads\/2026\/07\/laravel-ai-chatbot-steps-v2.png\" alt=\"Build a Laravel AI chatbot in 3 steps\" width=\"1200\" height=\"470\" \/><\/figure>\n<h2 id=\"install\"><span class=\"ez-toc-section\" id=\"Step_1_Install_the_Laravel_AI_SDK\"><\/span>Step 1: Install the Laravel AI SDK<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>From the root of your Laravel 13 project, pull in the package, publish its config, and run the migration that stores conversations:<\/p>\n<pre style=\"background:#0d1117;color:#e6edf3;padding:18px;border-radius:8px;overflow-x:auto;font-size:14px;line-height:1.6;\"><code>composer require laravel\/ai\r\n\r\nphp artisan vendor:publish --provider=\"Laravel\\Ai\\AiServiceProvider\"\r\n\r\nphp artisan migrate<\/code><\/pre>\n<p>The migration creates the tables the SDK uses to remember conversations, so your Laravel AI chatbot can pick up where it left off.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Step_2_Add_Your_AI_Provider_Key\"><\/span>Step 2: Add Your AI Provider Key<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Open your <code>.env<\/code> file and add the key for whichever provider you want to use as the default. For OpenAI:<\/p>\n<pre style=\"background:#0d1117;color:#e6edf3;padding:18px;border-radius:8px;overflow-x:auto;font-size:14px;line-height:1.6;\"><code>OPENAI_API_KEY=sk-your-secret-key-here<\/code><\/pre>\n<p>The SDK also recognizes <code>ANTHROPIC_API_KEY<\/code>, <code>GEMINI_API_KEY<\/code>, <code>GROQ_API_KEY<\/code>, <code>MISTRAL_API_KEY<\/code>, <code>XAI_API_KEY<\/code> and others. You can set the default provider and model in the published <code>config\/ai.php<\/code> file, so you never have to repeat them in your code.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Step_3_Your_First_AI_Response\"><\/span>Step 3: Your First AI Response<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Before building the full chatbot, test that everything works with a quick anonymous agent. Open Tinker with <code>php artisan tinker<\/code> and run:<\/p>\n<pre style=\"background:#0d1117;color:#e6edf3;padding:18px;border-radius:8px;overflow-x:auto;font-size:14px;line-height:1.6;\"><code>use function Laravel\\Ai\\agent;\r\n\r\n$response = agent(\r\n    instructions: 'You are a helpful assistant.',\r\n)->prompt('Explain what Laravel is in one sentence.');\r\n\r\necho (string) $response;<\/code><\/pre>\n<p>If you see a sensible answer, your Laravel AI SDK setup is working and you are ready to build a proper chatbot agent.<\/p>\n<h2 id=\"agent\"><span class=\"ez-toc-section\" id=\"Step_4_Create_a_Chatbot_Agent\"><\/span>Step 4: Create a Chatbot Agent<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Agents are the heart of the SDK. Generate one with the new Artisan command:<\/p>\n<pre style=\"background:#0d1117;color:#e6edf3;padding:18px;border-radius:8px;overflow-x:auto;font-size:14px;line-height:1.6;\"><code>php artisan make:agent SupportBot<\/code><\/pre>\n<p>This creates <code>app\/Ai\/Agents\/SupportBot.php<\/code>. Give it a personality by editing its instructions:<\/p>\n<pre style=\"background:#0d1117;color:#e6edf3;padding:18px;border-radius:8px;overflow-x:auto;font-size:14px;line-height:1.6;\"><code>&lt;?php\r\n\r\nnamespace App\\Ai\\Agents;\r\n\r\nuse Laravel\\Ai\\Contracts\\Agent;\r\nuse Laravel\\Ai\\Promptable;\r\n\r\nclass SupportBot implements Agent\r\n{\r\n    use Promptable;\r\n\r\n    public function instructions(): string\r\n    {\r\n        return 'You are a friendly support assistant for an online store. '\r\n            .'Answer clearly and concisely. If you are unsure, say so '\r\n            .'rather than guessing.';\r\n    }\r\n}<\/code><\/pre>\n<p>You can already talk to it. Anywhere in your app, run:<\/p>\n<pre style=\"background:#0d1117;color:#e6edf3;padding:18px;border-radius:8px;overflow-x:auto;font-size:14px;line-height:1.6;\"><code>$reply = (new SupportBot)->prompt('Do you ship internationally?');\r\n\r\nreturn (string) $reply;<\/code><\/pre>\n<figure><img decoding=\"async\" src=\"https:\/\/debugspot.com\/blogs\/wp-content\/uploads\/2026\/07\/laravel-ai-chatbot-grid-v2.png\" alt=\"Laravel AI SDK features for a chatbot\" width=\"1200\" height=\"620\" \/><\/figure>\n<h2 id=\"memory\"><span class=\"ez-toc-section\" id=\"Step_5_Add_Conversation_Memory\"><\/span>Step 5: Add Conversation Memory<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A real Laravel AI chatbot needs to remember what was said. The SDK makes this effortless with two traits. First, add <code>HasConversations<\/code> to your User model:<\/p>\n<pre style=\"background:#0d1117;color:#e6edf3;padding:18px;border-radius:8px;overflow-x:auto;font-size:14px;line-height:1.6;\"><code>&lt;?php\r\n\r\nnamespace App\\Models;\r\n\r\nuse Illuminate\\Foundation\\Auth\\User as Authenticatable;\r\nuse Laravel\\Ai\\Concerns\\HasConversations;\r\n\r\nclass User extends Authenticatable\r\n{\r\n    use HasConversations;\r\n}<\/code><\/pre>\n<p>Then add <code>RemembersConversations<\/code> to the agent and implement the <code>Conversational<\/code> contract:<\/p>\n<pre style=\"background:#0d1117;color:#e6edf3;padding:18px;border-radius:8px;overflow-x:auto;font-size:14px;line-height:1.6;\"><code>&lt;?php\r\n\r\nnamespace App\\Ai\\Agents;\r\n\r\nuse Laravel\\Ai\\Concerns\\RemembersConversations;\r\nuse Laravel\\Ai\\Contracts\\Agent;\r\nuse Laravel\\Ai\\Contracts\\Conversational;\r\nuse Laravel\\Ai\\Promptable;\r\n\r\nclass SupportBot implements Agent, Conversational\r\n{\r\n    use Promptable, RemembersConversations;\r\n\r\n    public function instructions(): string\r\n    {\r\n        return 'You are a friendly support assistant for an online store.';\r\n    }\r\n}<\/code><\/pre>\n<p>Now start a conversation for a user and keep going:<\/p>\n<pre style=\"background:#0d1117;color:#e6edf3;padding:18px;border-radius:8px;overflow-x:auto;font-size:14px;line-height:1.6;\"><code>\/\/ Start a new conversation\r\n$response = (new SupportBot)->forUser($user)->prompt('Hi, I need help with an order.');\r\n\r\n$conversationId = $response->conversationId;\r\n\r\n\/\/ Later, continue the same conversation\r\n$response = (new SupportBot)\r\n    ->continue($conversationId, as: $user)\r\n    ->prompt('It has not arrived yet.');<\/code><\/pre>\n<p>The SDK saves and replays the history automatically &#8211; you never build the message array yourself.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Step_6_Build_the_Chat_Controller_and_Routes\"><\/span>Step 6: Build the Chat Controller and Routes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Wire the agent into a controller so your frontend can talk to it. Add the routes:<\/p>\n<pre style=\"background:#0d1117;color:#e6edf3;padding:18px;border-radius:8px;overflow-x:auto;font-size:14px;line-height:1.6;\"><code>\/\/ routes\/web.php\r\nuse App\\Http\\Controllers\\ChatController;\r\n\r\nRoute::middleware('auth')->group(function () {\r\n    Route::get('\/chat', [ChatController::class, 'index']);\r\n    Route::post('\/chat', [ChatController::class, 'send']);\r\n});<\/code><\/pre>\n<p>Then the controller:<\/p>\n<pre style=\"background:#0d1117;color:#e6edf3;padding:18px;border-radius:8px;overflow-x:auto;font-size:14px;line-height:1.6;\"><code>&lt;?php\r\n\r\nnamespace App\\Http\\Controllers;\r\n\r\nuse App\\Ai\\Agents\\SupportBot;\r\nuse Illuminate\\Http\\Request;\r\n\r\nclass ChatController extends Controller\r\n{\r\n    public function index()\r\n    {\r\n        return view('chat');\r\n    }\r\n\r\n    public function send(Request $request)\r\n    {\r\n        $request->validate(['message' => 'required|string|max:2000']);\r\n\r\n        $user = $request->user();\r\n        $conversationId = $request->input('conversation_id');\r\n\r\n        $bot = $conversationId\r\n            ? (new SupportBot)->continue($conversationId, as: $user)\r\n            : (new SupportBot)->forUser($user);\r\n\r\n        $response = $bot->prompt($request->input('message'));\r\n\r\n        return response()->json([\r\n            'reply' => (string) $response,\r\n            'conversation_id' => $response->conversationId,\r\n        ]);\r\n    }\r\n}<\/code><\/pre>\n<h2 id=\"stream\"><span class=\"ez-toc-section\" id=\"Step_7_Stream_Replies_to_the_Browser\"><\/span>Step 7: Stream Replies to the Browser<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Waiting for a full answer feels slow. Streaming shows words as the AI generates them, exactly like ChatGPT. The SDK makes this a one-liner &#8211; just swap <code>prompt()<\/code> for <code>stream()<\/code>:<\/p>\n<pre style=\"background:#0d1117;color:#e6edf3;padding:18px;border-radius:8px;overflow-x:auto;font-size:14px;line-height:1.6;\"><code>\/\/ routes\/web.php\r\nRoute::post('\/chat\/stream', function (Request $request) {\r\n    $request->validate(['message' => 'required|string|max:2000']);\r\n\r\n    return (new SupportBot)\r\n        ->forUser($request->user())\r\n        ->stream($request->input('message'))\r\n        ->usingVercelDataProtocol();\r\n})->middleware('auth');<\/code><\/pre>\n<p>You can also run a callback once streaming finishes &#8211; handy for logging token usage or analytics:<\/p>\n<pre style=\"background:#0d1117;color:#e6edf3;padding:18px;border-radius:8px;overflow-x:auto;font-size:14px;line-height:1.6;\"><code>use Laravel\\Ai\\Responses\\StreamedAgentResponse;\r\n\r\nreturn (new SupportBot)\r\n    ->forUser($request->user())\r\n    ->stream($request->input('message'))\r\n    ->then(function (StreamedAgentResponse $response) {\r\n        logger()->info('Tokens used', ['usage' => $response->usage]);\r\n    });<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Step_8_A_Simple_Chat_UI\"><\/span>Step 8: A Simple Chat UI<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>On the frontend, if you use React or Inertia, the official <code>useStream<\/code> hook makes the UI trivial:<\/p>\n<pre style=\"background:#0d1117;color:#e6edf3;padding:18px;border-radius:8px;overflow-x:auto;font-size:14px;line-height:1.6;\"><code>import { useStream } from '@laravel\/stream-react';\r\n\r\nconst { data, send, isStreaming } = useStream('\/chat\/stream');\r\n\r\n\/\/ Call send({ message }) on submit, and render `data` as it streams in.<\/code><\/pre>\n<p>Prefer plain Blade? A tiny form plus a <code>fetch()<\/code> call to the non-streaming <code>\/chat<\/code> route works too &#8211; post the message, append the JSON <code>reply<\/code> to the page, and store the returned <code>conversation_id<\/code> so the next message continues the same chat.<\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/debugspot.com\/blogs\/wp-content\/uploads\/2026\/07\/laravel-ai-chatbot-providers-grid-v2.png\" alt=\"Laravel AI SDK supported providers\" width=\"1200\" height=\"620\" \/><\/figure>\n<h2 id=\"providers\"><span class=\"ez-toc-section\" id=\"Switch_AI_Providers_in_One_Line\"><\/span>Switch AI Providers in One Line<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>One of the best things about the SDK is that your Laravel AI chatbot is not locked to a single provider. Override the provider and model per call using the <code>Lab<\/code> enum:<\/p>\n<pre style=\"background:#0d1117;color:#e6edf3;padding:18px;border-radius:8px;overflow-x:auto;font-size:14px;line-height:1.6;\"><code>use Laravel\\Ai\\Enums\\Lab;\r\n\r\n$response = (new SupportBot)->prompt(\r\n    'Summarize our return policy in three bullet points.',\r\n    provider: Lab::Anthropic,\r\n    model: 'claude-haiku-4-5-20251001',\r\n    timeout: 120,\r\n);<\/code><\/pre>\n<p>Switch from OpenAI to Anthropic, Gemini, or a local model without touching the rest of your app &#8211; perfect for balancing cost, speed, and quality.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Give_Your_Chatbot_Tools_Function_Calling\"><\/span>Give Your Chatbot Tools (Function Calling)<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To let the bot actually do things &#8211; look up an order, check stock, create a ticket &#8211; give it tools. Implement the <code>HasTools<\/code> contract and return your tool classes:<\/p>\n<pre style=\"background:#0d1117;color:#e6edf3;padding:18px;border-radius:8px;overflow-x:auto;font-size:14px;line-height:1.6;\"><code>use Laravel\\Ai\\Contracts\\HasTools;\r\n\r\nclass SupportBot implements Agent, Conversational, HasTools\r\n{\r\n    use Promptable, RemembersConversations;\r\n\r\n    public function tools(): iterable\r\n    {\r\n        return [\r\n            new LookupOrder,\r\n            new CheckStock,\r\n        ];\r\n    }\r\n}<\/code><\/pre>\n<p>The model decides when to call a tool, the SDK runs your PHP code, and the result is fed back into the reply &#8211; turning a simple chatbot into a genuine assistant.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Structured_Output_for_Reliable_Data\"><\/span>Structured Output for Reliable Data<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When you need machine-readable answers rather than prose &#8211; a category, a score, a set of fields &#8211; implement <code>HasStructuredOutput<\/code> and define a schema. The response then behaves like an array, so <code>$response['score']<\/code> just works. This is ideal for classification, moderation, or extracting data from user messages inside your chatbot flow.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Security_and_Cost_Tips\"><\/span>Security and Cost Tips<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li><strong>Never expose your API key<\/strong> &#8211; keep it in <code>.env<\/code>, never in JavaScript or the repo.<\/li>\n<li><strong>Validate and limit input<\/strong> &#8211; cap message length (as shown) to control token costs.<\/li>\n<li><strong>Rate-limit the chat route<\/strong> with Laravel throttling so a single user cannot run up your bill.<\/li>\n<li><strong>Pick a cheaper model<\/strong> like a mini or haiku variant for routine replies, and reserve larger models for complex questions.<\/li>\n<li><strong>Set a system instruction<\/strong> that keeps the bot on-topic and refuses out-of-scope requests.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Common_Errors_and_Fixes\"><\/span>Common Errors and Fixes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li><strong>&#8220;Missing API key&#8221;<\/strong> &#8211; confirm the key is in <code>.env<\/code> and run <code>php artisan config:clear<\/code>.<\/li>\n<li><strong>Timeouts on long replies<\/strong> &#8211; increase the <code>timeout<\/code> argument or use streaming so the user sees progress.<\/li>\n<li><strong>Conversation not remembered<\/strong> &#8211; make sure the User model uses <code>HasConversations<\/code> and you call <code>forUser()<\/code> or <code>continue()<\/code>.<\/li>\n<li><strong>Class not found after install<\/strong> &#8211; run <code>composer dump-autoload<\/code>.<\/li>\n<\/ul>\n<h2 id=\"related\"><span class=\"ez-toc-section\" id=\"Related_Laravel_Guides\"><\/span>Related Laravel Guides<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Enjoyed this tutorial? These other free Laravel guides from DebugSpot pair well with it:<\/p>\n<ul>\n<li><a href=\"https:\/\/debugspot.com\/blogs\/search-comma-separated-values-laravel\/\">How to Search Comma Separated Values In Laravel<\/a><\/li>\n<li><a href=\"https:\/\/debugspot.com\/blogs\/generate-admin-panel-in-laravel\/\">Easily Generate an Admin Panel in Laravel in Just 2 Minutes<\/a><\/li>\n<li><a href=\"https:\/\/debugspot.com\/blogs\/install-configure-supervisor-laravel-linux\/\">6 Easy Steps: How to Install and Configure Supervisor for a Laravel Application on Linux<\/a><\/li>\n<li><a href=\"https:\/\/debugspot.com\/blogs\/how-to-check-checkbox-checked-jquery\/\">Easily Ensure At Least One Checkbox is Checked: jQuery Validation Guide<\/a><\/li>\n<li><a href=\"https:\/\/debugspot.com\/blogs\/create-iam-user-in-aws-with-permissions\/\">7 Easy Steps to Successfully Create an IAM User in AWS: A Complete Guide<\/a><\/li>\n<\/ul>\n<h2 id=\"faq\"><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions\"><\/span>Frequently Asked Questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_Laravel_AI_SDK\"><\/span>What is the Laravel AI SDK?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>It is a first-party Laravel 13 package that gives you one unified API to build AI features &#8211; chatbots, agents, and more &#8211; across providers like OpenAI, Anthropic, and Gemini.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Do_I_need_Laravel_13_to_build_a_Laravel_AI_chatbot\"><\/span>Do I need Laravel 13 to build a Laravel AI chatbot?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The AI SDK ships with Laravel 13 and requires PHP 8.3+. It is the recommended way to build AI features going forward.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Which_AI_providers_does_it_support\"><\/span>Which AI providers does it support?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>OpenAI, Anthropic, Gemini, Groq, Mistral, xAI, DeepSeek, Ollama, and more &#8211; switchable per call with the Lab enum.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Does_the_chatbot_remember_previous_messages\"><\/span>Does the chatbot remember previous messages?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Yes. Add the RemembersConversations trait to the agent and HasConversations to your User model, and history is saved and replayed automatically.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Can_I_stream_replies_like_ChatGPT\"><\/span>Can I stream replies like ChatGPT?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Yes. Use the stream() method, and consume it in React with the useStream hook or in Blade with a fetch call.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Is_the_Laravel_AI_SDK_free\"><\/span>Is the Laravel AI SDK free?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The SDK itself is free and open source. You only pay your chosen AI provider for the tokens you use.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_keep_API_costs_down\"><\/span>How do I keep API costs down?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Use a smaller model for routine replies, limit message length, and rate-limit the chat route so usage stays predictable.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Can_my_chatbot_perform_actions_not_just_chat\"><\/span>Can my chatbot perform actions, not just chat?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Yes. Give it tools via the HasTools contract, and the model can call your PHP code to look up data or perform tasks.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Next_Steps_After_Your_First_Chatbot\"><\/span>Next Steps After Your First Chatbot<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>With a working Laravel AI chatbot in place, the natural next steps are to add tools so it can look up real data, connect it to your knowledge base with vector search for grounded answers, and refine the system instructions until its tone matches your brand. From there you can build specialized agents for different jobs &#8211; one for support, one for sales, one for internal queries &#8211; all sharing the same clean SDK foundation. Each improvement is small, but together they turn a simple chat box into a genuinely useful assistant that saves your team real time every day.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Start_Building_Your_Laravel_AI_Chatbot\"><\/span>Start Building Your Laravel AI Chatbot<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The Laravel 13 AI SDK turns what used to be a complex integration into a few expressive classes. You now have everything you need to build a streaming, memory-aware Laravel AI chatbot &#8211; and switch providers whenever you like. Explore more free <a href=\"https:\/\/debugspot.com\/blogs\/category\/laravel\/\">Laravel tutorials<\/a> and handy <a href=\"https:\/\/debugspot.com\/\">free developer tools<\/a> from DebugSpot.<\/p>\n<div style=\"text-align:center;margin:30px 0;\"><a href=\"https:\/\/debugspot.com\/blogs\/category\/laravel\/\" style=\"display:inline-block;background:#7c3aed;color:#ffffff;padding:15px 36px;border-radius:8px;font-size:18px;font-weight:700;text-decoration:none;box-shadow:0 6px 18px rgba(124,58,237,0.4);\">&#128218; More Laravel 13 Tutorials &rarr;<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Build a Laravel AI chatbot in 2026 with the new Laravel 13 AI SDK &#8211; streaming replies, conversation memory, and OpenAI or Anthropic. Full step-by-step with code.<\/p>\n","protected":false},"author":1,"featured_media":2081,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"rank_math_lock_modified_date":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-2085","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel"],"acf":[],"_links":{"self":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts\/2085"}],"collection":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/comments?post=2085"}],"version-history":[{"count":2,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts\/2085\/revisions"}],"predecessor-version":[{"id":2087,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts\/2085\/revisions\/2087"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/media\/2081"}],"wp:attachment":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/media?parent=2085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/categories?post=2085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/tags?post=2085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}