{"id":11433,"date":"2026-05-01T08:53:16","date_gmt":"2026-05-01T08:53:16","guid":{"rendered":"https:\/\/programmingfields.com\/?p=11433"},"modified":"2026-05-01T16:11:30","modified_gmt":"2026-05-01T16:11:30","slug":"laravel-13-ai-sdk-tutorial","status":"publish","type":"post","link":"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/","title":{"rendered":"Laravel 13 AI SDK: Build AI-Powered Laravel Apps in 2026"},"content":{"rendered":"\n<p>AI is everywhere right now. And as a Laravel developer, you might be wondering \u2014 <em><strong>can I add AI features to my Laravel app without switching frameworks or pulling in a dozen packages?<\/strong><\/em><\/p>\n\n\n\n<p>The answer is yes. And with the <strong>Laravel 13 AI SDK<\/strong>, it has never been easier.<\/p>\n\n\n\n<p>In this guide, we will go through everything you need to know about the <strong>Laravel 13 AI SDK<\/strong>. We will cover what it is, how to set it up, and how to use it in real projects. By the end, you will be able to build AI-powered features directly inside your Laravel application.<\/p>\n\n\n\n<p>So, let&#8217;s get started.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-light-blue 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:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#What_is_the_Laravel_13_AI_SDK\" >What is the Laravel 13 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:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#Why_Laravel_Built_a_Native_AI_SDK\" >Why Laravel Built a Native AI SDK<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#Requirements_Before_You_Begin\" >Requirements Before You Begin<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#Installing_and_Setting_Up_the_Laravel_AI_SDK\" >Installing and Setting Up 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-5\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#Configuring_Your_AI_Provider\" >Configuring Your AI Provider<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#Text_Generation_with_the_Laravel_AI_SDK\" >Text Generation with the Laravel AI SDK<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#Basic_Text_Generation\" >Basic Text Generation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#Generating_Text_from_a_Controller\" >Generating Text from a Controller<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#Using_a_System_Prompt\" >Using a System Prompt<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#Using_a_Different_Provider_at_Runtime\" >Using a Different Provider at Runtime<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#Building_AI_Agents_in_Laravel_13\" >Building AI Agents in Laravel 13<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#Practical_Agent_Use_Cases\" >Practical Agent Use Cases<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#Working_with_Embeddings\" >Working with Embeddings<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#Why_Embeddings_Matter\" >Why Embeddings Matter<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#Generating_Embeddings_with_the_Laravel_AI_SDK\" >Generating Embeddings with 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-16\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#Storing_and_Searching_Embeddings\" >Storing and Searching Embeddings<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#Streaming_AI_Responses\" >Streaming AI Responses<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#Real-World_Use_Cases\" >Real-World Use Cases<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#1_AI-Powered_Blog_Post_Generator\" >1. AI-Powered Blog Post Generator<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#2_Smart_Customer_Support_Auto-Reply\" >2. Smart Customer Support Auto-Reply<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#3_Product_Description_Generator_for_E-commerce\" >3. Product Description Generator for E-commerce<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#4_AI_Code_Review_Assistant\" >4. AI Code Review Assistant<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#Best_Practices_When_Using_Laravel_AI\" >Best Practices When Using Laravel AI<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-24\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#1_Always_Validate_User_Input\" >1. Always Validate User Input<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-25\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#2_Cache_Repeated_Requests\" >2. Cache Repeated Requests<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-26\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#3_Run_Heavy_AI_Tasks_in_the_Background\" >3. Run Heavy AI Tasks in the Background<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-27\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#4_Handle_Errors_Gracefully\" >4. Handle Errors Gracefully<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-28\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#5_Set_Spending_Limits_on_Your_API_Account\" >5. Set Spending Limits on Your API Account<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-29\" href=\"https:\/\/programmingfields.com\/laravel-13-ai-sdk-tutorial\/#Final_Thoughts\" >Final Thoughts<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_the_Laravel_13_AI_SDK\"><\/span><strong>What is the Laravel 13 AI SDK?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <strong>Laravel 13 AI SDK<\/strong> is a first-party package built by the Laravel team. It gives you a clean, native way to work with AI models directly from your Laravel application.<\/p>\n\n\n\n<p>Before Laravel 13, if you wanted to add AI features, you had to install third-party packages manually. You had to write custom wrappers. And you had to figure out authentication, error handling, and formatting on your own.<\/p>\n\n\n\n<p>That was frustrating.<\/p>\n\n\n\n<p>But now, Laravel provides a simple, elegant API \u2014 just like everything else in the framework. You get access to AI capabilities without the boilerplate.<\/p>\n\n\n\n<p>The <strong>Laravel AI SDK<\/strong> currently supports:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Text generation<\/strong> \u2014 Ask a question, get a response<\/li>\n\n\n\n<li><strong>AI agents<\/strong> \u2014 Build smart, tool-using AI workflows<\/li>\n\n\n\n<li><strong>Embeddings<\/strong> \u2014 Convert text into vector representations for semantic search<\/li>\n\n\n\n<li><strong>Streaming<\/strong> \u2014 Get real-time responses instead of waiting for the full output<\/li>\n\n\n\n<li><strong>Content automation<\/strong> \u2014 Automate writing, summarization, classification, and more<\/li>\n<\/ul>\n\n\n\n<p>And all of this works through a unified <code>AI<\/code> facade. Clean. Simple. Very Laravel.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_Laravel_Built_a_Native_AI_SDK\"><\/span><strong>Why Laravel Built a Native AI SDK<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This is a fair question. Why build a native AI SDK when packages like Prism PHP already exist?<\/p>\n\n\n\n<p>Here&#8217;s the thing \u2014 <strong>Laravel AI integration<\/strong> is no longer a nice-to-have. It is becoming a core requirement in modern web applications. Product managers want chatbots. Clients want smart search. Users expect AI-powered features as a default, not a luxury.<\/p>\n\n\n\n<p>By building a native solution, Laravel gives you:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Consistency<\/strong> \u2014 The same conventions you already know<\/li>\n\n\n\n<li><strong>Official support<\/strong> \u2014 No more worrying if a third-party package will be maintained<\/li>\n\n\n\n<li><strong>Framework-level integration<\/strong> \u2014 Works seamlessly with queues, logging, caching, and events<\/li>\n<\/ul>\n\n\n\n<p>In short, the <strong>Laravel 13 AI SDK<\/strong> is Laravel taking AI seriously. And that is a big deal for the entire ecosystem.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Requirements_Before_You_Begin\"><\/span><strong>Requirements Before You Begin<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Before you can use the <strong>Laravel 13 AI SDK<\/strong>, make sure your environment is ready.<\/p>\n\n\n\n<p>Here is what you need:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Laravel 13<\/strong> (obviously)<\/li>\n\n\n\n<li><strong>PHP 8.3 or higher<\/strong> \u2014 Laravel 13 requires this<\/li>\n\n\n\n<li><strong>Composer<\/strong> \u2014 For package installation<\/li>\n\n\n\n<li><strong>An API key<\/strong> from a supported AI provider (OpenAI, Anthropic, Gemini, etc.)<\/li>\n<\/ul>\n\n\n\n<p>If you are still on Laravel 12, check out our <a href=\"https:\/\/programmingfields.com\/laravel-13-upgrade-guide-step-by-step\/\">Laravel 13 upgrade guide<\/a> first. Upgrading is simpler than you think.<\/p>\n\n\n\n<p>Also, if you are new to Laravel 13, you might want to read the <a href=\"https:\/\/programmingfields.com\/laravel-13-new-features-upgrade-guide\/\">Laravel 13 new features overview<\/a> before continuing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Installing_and_Setting_Up_the_Laravel_AI_SDK\"><\/span><strong>Installing and Setting Up the Laravel AI SDK<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <strong>Laravel 13 AI SDK<\/strong> comes as part of the Laravel ecosystem via Composer. Hence, let&#8217;s install it.<\/p>\n\n\n\n<p>Open your terminal and run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">composer require laravel\/ai<\/code><\/pre>\n\n\n\n<p>That&#8217;s it. One command. No extra configuration file needed at this point.<\/p>\n\n\n\n<p>Next, publish the configuration file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">php artisan vendor:publish --provider=\"Laravel\\AI\\AIServiceProvider\"<\/code><\/pre>\n\n\n\n<p>This will create a new file at <code>config\/ai.php<\/code>. We will use this file in the next step to configure our AI provider.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Configuring_Your_AI_Provider\"><\/span><strong>Configuring Your AI Provider<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <strong>Laravel AI SDK<\/strong> supports multiple AI providers. This is one of its biggest strengths. You are not locked into one company.<\/p>\n\n\n\n<p>Open <code><strong>config\/ai.php<\/strong><\/code>. You will see something like this:<\/p>\n\n\n\n<pre title=\"config\/ai.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">return [\n    'default' =&gt; env('AI_PROVIDER', 'openai'),\n\n    'providers' =&gt; [\n        'openai' =&gt; [\n            'api_key' =&gt; env('OPENAI_API_KEY'),\n            'model'   =&gt; env('OPENAI_MODEL', 'gpt-4o'),\n        ],\n        'anthropic' =&gt; [\n            'api_key' =&gt; env('ANTHROPIC_API_KEY'),\n            'model'   =&gt; env('ANTHROPIC_MODEL', 'claude-3-5-sonnet-20241022'),\n        ],\n        'gemini' =&gt; [\n            'api_key' =&gt; env('GEMINI_API_KEY'),\n            'model'   =&gt; env('GEMINI_MODEL', 'gemini-1.5-pro'),\n        ],\n    ],\n];\n<\/code><\/pre>\n\n\n\n<p>Now, add your credentials to your <code>.env<\/code> file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">AI_PROVIDER=openai\nOPENAI_API_KEY=sk-your-openai-key-here\nOPENAI_MODEL=gpt-4o<\/code><\/pre>\n\n\n\n<p>If you prefer <strong>Anthropic<\/strong> or <strong>Gemini<\/strong>, simply change <code><strong>AI_PROVIDER<\/strong><\/code> and add the corresponding key.<\/p>\n\n\n\n<p>This multi-provider setup is a powerful feature of the <strong>Laravel AI integration<\/strong>. You can even switch providers per feature using runtime configuration, which we will cover shortly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Text_Generation_with_the_Laravel_AI_SDK\"><\/span><strong>Text Generation with the Laravel AI SDK<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Text generation is the most common use case. And with the <strong>Laravel 13 AI SDK<\/strong>, it is beautifully simple.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Basic_Text_Generation\"><\/span>Basic Text Generation<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here is the most basic example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">use Illuminate\\Support\\Facades\\AI;\n\n$response = AI::text('What is Laravel 13?');\n\necho $response-&gt;text();<\/code><\/pre>\n\n\n\n<p>Run this from a controller, an Artisan command, or a job. It works anywhere inside your Laravel app.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Generating_Text_from_a_Controller\"><\/span><strong>Generating Text from a Controller<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Let&#8217;s make it more realistic. Here is how you would use <strong>Laravel AI text generation<\/strong> inside a controller:<\/p>\n\n\n\n<pre title=\"ContentController.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\nnamespace App\\Http\\Controllers;\n\nuse Illuminate\\Http\\Request;\nuse Illuminate\\Support\\Facades\\AI;\n\nclass ContentController extends Controller\n{\n    public function generate(Request $request)\n    {\n        $request-&gt;validate([\n            'prompt' =&gt; 'required|string|max:500',\n        ]);\n\n        $response = AI::text($request-&gt;prompt);\n\n        return response()-&gt;json([\n            'result' =&gt; $response-&gt;text(),\n        ]);\n    }\n}<\/code><\/pre>\n\n\n\n<p>Clean and simple. Notice how it follows standard Laravel patterns. There is no learning curve if you already know Laravel.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Using_a_System_Prompt\"><\/span>Using a System Prompt<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Sometimes, you want to give the AI context before asking a question. That is where a system prompt comes in.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$response = AI::text(\n    prompt: 'Write a short product description for a wireless keyboard.',\n    system: 'You are a professional copywriter. Keep responses under 100 words. Use persuasive language.'\n);\n\necho $response-&gt;text();<\/code><\/pre>\n\n\n\n<p>The <code>system<\/code> parameter sets the behavior of the AI. This is extremely useful when you are building <strong>AI-powered Laravel apps<\/strong> with specific personalities or personas.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Using_a_Different_Provider_at_Runtime\"><\/span>Using a Different Provider at Runtime<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>What if you want to use Anthropic for one specific task but OpenAI for everything else?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$response = AI::provider('anthropic')-&gt;text('Summarize this article: ...');\n\necho $response-&gt;text();<\/code><\/pre>\n\n\n\n<p>This is one of the cleanest features of the <strong>Laravel AI SDK<\/strong>. You switch providers with one method call \u2014 no config changes needed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Building_AI_Agents_in_Laravel_13\"><\/span><strong>Building AI Agents in Laravel 13<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>AI agents are more powerful than basic text generation. An agent can <strong>use tools<\/strong> to take actions, fetch data, and make decisions based on results.<\/p>\n\n\n\n<p>Think of an agent as an AI that can reason about a problem and decide what to do next.<\/p>\n\n\n\n<p>Here is a simple example of an AI agent in Laravel 13:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">use Illuminate\\Support\\Facades\\AI;\nuse Laravel\\AI\\Tools\\Tool;\n\n$agent = AI::agent()\n    -&gt;withTool(\n        Tool::make('get_weather')\n            -&gt;description('Get current weather for a city')\n            -&gt;parameter('city', 'string', 'The city name')\n            -&gt;handler(function (string $city) {\n                \/\/ Call your weather API here\n                return \"The weather in {$city} is 25\u00b0C and sunny.\";\n            })\n    )\n    -&gt;run('What is the weather in Bangalore right now?');\n\necho $agent-&gt;text();<\/code><\/pre>\n\n\n\n<p>What happens here? The AI decides on its own that it needs to call the <code>get_weather<\/code> tool. It calls it, gets the result, and returns a final answer.<\/p>\n\n\n\n<p>This is the real power of <strong>AI in Laravel 13<\/strong>. You are not just generating text. You are building systems that reason, act, and respond intelligently.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Practical_Agent_Use_Cases\"><\/span><strong>Practical Agent Use Cases<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>AI agents work great for:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Customer support bots<\/strong> \u2014 Agents that check order status, fetch FAQs, and escalate tickets<\/li>\n\n\n\n<li><strong>Internal tools<\/strong> \u2014 Agents that query your database and summarize results<\/li>\n\n\n\n<li><strong>Smart assistants<\/strong> \u2014 Agents that book appointments, send emails, or create records<\/li>\n\n\n\n<li><strong>Code helpers<\/strong> \u2014 Agents that review code and suggest improvements<\/li>\n<\/ul>\n\n\n\n<p>The <strong>Laravel 13 AI SDK<\/strong> makes all of this possible with clean, readable PHP code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Working_with_Embeddings\"><\/span><strong>Working with Embeddings<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Embeddings are a bit more advanced. But they are extremely powerful once you understand them.<\/p>\n\n\n\n<p>An <strong>embedding<\/strong> is a numerical representation of text. It captures the <em>meaning<\/em> of words, not just the words themselves.<\/p>\n\n\n\n<p>For example, the words &#8220;cat&#8221; and &#8220;kitten&#8221; are different strings. But their embeddings will be very similar because they mean similar things.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_Embeddings_Matter\"><\/span><strong>Why Embeddings Matter<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Embeddings power:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Semantic search<\/strong> \u2014 Find results by meaning, not exact keywords<\/li>\n\n\n\n<li><strong>Recommendation systems<\/strong> \u2014 Suggest similar articles, products, or content<\/li>\n\n\n\n<li><strong>RAG (Retrieval Augmented Generation)<\/strong> \u2014 Give the AI relevant context from your own data<\/li>\n\n\n\n<li><strong>Duplicate detection<\/strong> \u2014 Find similar content in your database<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Generating_Embeddings_with_the_Laravel_AI_SDK\"><\/span>Generating Embeddings with the Laravel AI SDK<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here is how to generate an embedding in Laravel 13:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">use Illuminate\\Support\\Facades\\AI;\n\n$embedding = AI::embed('Laravel is a PHP framework for web artisans.');\n\n\/\/ Returns an array of floats\n$vector = $embedding-&gt;vector();<\/code><\/pre>\n\n\n\n<p>You can then store this vector in your database (PostgreSQL with pgvector works best) and query it later.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Storing_and_Searching_Embeddings\"><\/span><strong>Storing and Searching Embeddings<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">\/\/ Step 1: Store the embedding when creating a post\n$post = Post::create(['title' =&gt; $title, 'content' =&gt; $content]);\n\n$embedding = AI::embed($content)-&gt;vector();\n\n$post-&gt;update(['embedding' =&gt; $embedding]);\n\n\/\/ Step 2: Search by meaning later\n$query = AI::embed('How to deploy a Laravel application?')-&gt;vector();\n\n$results = Post::whereVectorSimilar('embedding', $query)-&gt;limit(5)-&gt;get();<\/code><\/pre>\n\n\n\n<p>This is <strong>Laravel artificial intelligence<\/strong> at its best. Your app now understands what users are <em>looking for<\/em>, not just what they <em>typed<\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Streaming_AI_Responses\"><\/span><strong>Streaming AI Responses<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Nobody likes staring at a blank screen waiting for a full response to load. That is why streaming exists.<\/p>\n\n\n\n<p>With streaming, the AI sends back words as it generates them \u2014 just like ChatGPT. This makes your app feel fast and responsive.<\/p>\n\n\n\n<p>Here is how to stream a response in the <strong>Laravel 13 AI SDK<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">use Illuminate\\Support\\Facades\\AI;\n\n$stream = AI::stream('Write a detailed tutorial about Laravel queues.');\n\nforeach ($stream-&gt;chunks() as $chunk) {\n    echo $chunk-&gt;text();\n    ob_flush();\n    flush();\n}<\/code><\/pre>\n\n\n\n<p>For a proper streaming API endpoint, you can use Laravel&#8217;s <code>StreamedResponse<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">use Illuminate\\Support\\Facades\\AI;\nuse Symfony\\Component\\HttpFoundation\\StreamedResponse;\n\npublic function streamContent(Request $request): StreamedResponse\n{\n    return response()-&gt;stream(function () use ($request) {\n        $stream = AI::stream($request-&gt;prompt);\n\n        foreach ($stream-&gt;chunks() as $chunk) {\n            echo \"data: \" . json_encode(['text' =&gt; $chunk-&gt;text()]) . \"\\n\\n\";\n            ob_flush();\n            flush();\n        }\n    }, 200, [\n        'Content-Type'      =&gt; 'text\/event-stream',\n        'Cache-Control'     =&gt; 'no-cache',\n        'X-Accel-Buffering' =&gt; 'no',\n    ]);\n}<\/code><\/pre>\n\n\n\n<p>On the frontend, you can consume this with the EventSource API or a simple fetch stream. The result is a real-time AI response \u2014 smooth, fast, and professional.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Real-World_Use_Cases\"><\/span><strong>Real-World Use Cases<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Let&#8217;s look at how developers are actually using the <strong>Laravel AI SDK<\/strong> in production.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_AI-Powered_Blog_Post_Generator\"><\/span><strong>1. AI-Powered Blog Post Generator<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">public function generatePost(Request $request)\n{\n    $response = AI::text(\n        prompt: \"Write a blog post about: {$request-&gt;topic}\",\n        system: 'You are a technical writer. Use clear language. Keep paragraphs short. Add code examples where relevant.'\n    );\n\n    return Post::create([\n        'title'   =&gt; $request-&gt;topic,\n        'content' =&gt; $response-&gt;text(),\n        'status'  =&gt; 'draft',\n    ]);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Smart_Customer_Support_Auto-Reply\"><\/span><strong>2. Smart Customer Support Auto-Reply<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">public function autoReply(SupportTicket $ticket)\n{\n    $response = AI::text(\n        prompt: \"Customer says: {$ticket-&gt;message}. Suggest a helpful reply.\",\n        system: 'You are a friendly customer support agent for a SaaS company. Be concise and helpful.'\n    );\n\n    $ticket-&gt;update(['suggested_reply' =&gt; $response-&gt;text()]);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Product_Description_Generator_for_E-commerce\"><\/span>3. Product Description Generator for E-commerce<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">public function generateDescription(Product $product)\n{\n    $response = AI::text(\n        prompt: \"Generate a product description for: {$product-&gt;name}. \n                 Features: {$product-&gt;features}. Target audience: {$product-&gt;audience}.\",\n        system: 'Write compelling e-commerce product descriptions. Keep it under 150 words. Focus on benefits.'\n    );\n\n    $product-&gt;update(['description' =&gt; $response-&gt;text()]);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_AI_Code_Review_Assistant\"><\/span>4. AI Code Review Assistant<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">public function reviewCode(Request $request)\n{\n    $response = AI::text(\n        prompt: \"Review this PHP code and suggest improvements:\\n\\n{$request-&gt;code}\",\n        system: 'You are a senior PHP developer. Focus on security, performance, and Laravel best practices.'\n    );\n\n    return response()-&gt;json(['review' =&gt; $response-&gt;text()]);\n}<\/code><\/pre>\n\n\n\n<p>These examples show just how versatile <strong>Laravel AI integration<\/strong> can be. Almost any feature you can imagine can be enhanced with AI.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Best_Practices_When_Using_Laravel_AI\"><\/span><strong>Best Practices When Using Laravel AI<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Before you ship AI features to production, here are some important things to keep in mind.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Always_Validate_User_Input\"><\/span><strong>1. Always Validate User Input<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Never pass raw user input directly to the AI without validation. This can lead to prompt injection attacks.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$request-&gt;validate([\n    'prompt' =&gt; 'required|string|max:1000',\n]);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Cache_Repeated_Requests\"><\/span><strong>2. Cache Repeated Requests<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>AI API calls cost money. If the same prompt is being called repeatedly, cache the result.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$result = Cache::remember('ai_faq_answer_' . md5($prompt), 3600, function () use ($prompt) {\n    return AI::text($prompt)-&gt;text();\n});<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Run_Heavy_AI_Tasks_in_the_Background\"><\/span><strong>3. Run Heavy AI Tasks in the Background<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>For long-running generations, use Laravel queues. Never make the user wait.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">\/\/ Create a job\nclass GenerateReportJob implements ShouldQueue\n{\n    public function handle(): void\n    {\n        $response = AI::text('Generate a monthly sales report summary...');\n        \/\/ Save result to database\n    }\n}\n\n\/\/ Dispatch it\nGenerateReportJob::dispatch();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Handle_Errors_Gracefully\"><\/span><strong>4. Handle Errors Gracefully<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>AI APIs can fail. Always wrap your calls in try-catch.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">try {\n    $response = AI::text($prompt);\n    return $response-&gt;text();\n} catch (\\Exception $e) {\n    Log::error('AI generation failed: ' . $e-&gt;getMessage());\n    return 'Sorry, we could not generate a response. Please try again.';\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5_Set_Spending_Limits_on_Your_API_Account\"><\/span><strong>5. Set Spending Limits on Your API Account<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>This one is often forgotten. Go to your OpenAI or Anthropic dashboard and set monthly spending limits. This protects you from unexpected bills if traffic spikes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Final_Thoughts\"><\/span><strong>Final Thoughts<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <strong>Laravel 13 AI SDK<\/strong> is genuinely exciting. And not because of hype \u2014 but because of what it makes possible.<\/p>\n\n\n\n<p>Think about it. A few years ago, adding AI to a PHP application meant custom HTTP clients, manual JSON parsing, and fragile integrations. Today, you write <code>AI::text(<\/code>) and you are done.<\/p>\n\n\n\n<p>Moreover, this is just the beginning. The <strong>Laravel AI SDK<\/strong> will grow with every release. More providers, more tools, more capabilities \u2014 all under the same clean, familiar API you already love.<\/p>\n\n\n\n<p>So, if you are starting a new Laravel project in 2026, there is really no reason <em>not<\/em> to consider AI features. The barrier to entry is now incredibly low.<\/p>\n\n\n\n<p>And if you are upgrading an existing app, start small. Add a single AI feature \u2014 a description generator, a smart search, a support auto-reply. See how it performs. Then build from there.<\/p>\n\n\n\n<p><strong>The future of Laravel development is AI-assisted. And with Laravel 13, that future is already here.<\/strong><\/p>\n\n\n\n<p><em>Found this guide helpful? Share it with your fellow Laravel developers. And if you have questions, drop them in the comments below \u2014 we read every single one.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>AI is everywhere right now. And as a Laravel developer, you might be wondering \u2014 can I add AI features to my Laravel app without switching frameworks or pulling in a dozen packages? The answer is yes. And with the Laravel 13 AI SDK, it has never been easier. In this guide, we will go [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":11435,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[2564],"tags":[3131,3112,3118,3136,3130,3137,3133,3132,3135],"yst_prominent_words":[],"class_list":{"0":"post-11433","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-laravel","8":"tag-ai-in-laravel-13","9":"tag-laravel-13","10":"tag-laravel-13-ai-sdk","11":"tag-laravel-ai","12":"tag-laravel-ai-integration","13":"tag-laravel-ai-sdk","14":"tag-laravel-ai-tutorial","15":"tag-laravel-artificial-intelligence","16":"tag-laravel-openai","17":"entry"},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2026\/05\/Laravel-AI-SDK.png?fit=1672%2C941&ssl=1","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/11433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/comments?post=11433"}],"version-history":[{"count":6,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/11433\/revisions"}],"predecessor-version":[{"id":11445,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/11433\/revisions\/11445"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/media\/11435"}],"wp:attachment":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/media?parent=11433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/categories?post=11433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/tags?post=11433"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/yst_prominent_words?post=11433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}