Create AI Agents in Laravel — The Simple Way

Revaz Ghambarashvili · 7th August 2025

In this article

Ever tried adding an AI assistant to your Laravel app… but gave up the moment you saw 12 tabs of confusing docs, complex tooling, and token limits? 😵‍💫

You’re not alone.

But what if I told you there’s an easier way?

That’s exactly why LarAgent exists.

LarAgent is a Laravel-native package that lets you build AI agents—chatbots, assistants, automation bots, AI workflows, you name it—directly inside your app. With Laravel-style syntax, zero vendor lock-in, and full extensibility, it feels like a first-class Laravel citizen.

In this guide, I’ll walk you through:

  • Installing and configuring LarAgent 🛠️
  • Creating your first AI agent (yes, even a pirate assistant) ☠️
  • Chatting with it via CLI or code 💬
  • Powering it up with tools and real-world skills 🧰

Let’s build something smart—and do it the Laravel way.

Installing and Configuring LarAgent

You only need two things: a Laravel app and Composer.

  1. Install LarAgent:

    composer require maestroerror/laragent
    
  2. Publish configuration:

    php artisan vendor:publish --tag=laragent-config
    
  3. Set your API key (OpenAI or local models like Ollama):

    OPENAI_API_KEY=your-openai-api-key
    # Or
    GEMINI_API_KEY=your-gemini-api-key
    

Note: You can add other providers via the config file config/laragent.php

Want cost-free development? Try a local model setup with Ollama—no API fees!

Creating Your First Agent

Let's create a fun agent with personality, say, a Pirate assistant.

Run this artisan command:

php artisan make:agent PirateAgent

The command creates a new agent class at ‘app/AiAgents/PirateAgent.php’

Your new agent has:

  • A personality defined in instructions().
  • User interaction handled by the prompt() method.
  • Properties to tweak configs like chat history, model and provider.

Here's an example of pirate-themed instructions:

public function instructions() {
    return "Ye be a pirate assistant, speakin' in pirate lingo, matey!";
}

Any user message is passed through the prompt method, where you can enhance it with custom prompt or enrich with context using RAG.

Here is a simple example:

public function prompt($message) {
    return "Arrr, here be the message: {
    $message}";
}

Wanna get fancy? You can use Blade files too for prompts and instructions. Yup, returning view("agents.prompts.pirate", ['message'=> $message]) works!

Chatting with Your Agent

Ready to chat? Test directly from your CLI:

php artisan agent:chat PirateAgent

Or integrate within your Laravel controller:

echo PirateAgent::for('chat-session')->respond('Hello!');

Where are the chats stored? You choose, by setting the agent property:

// PirateAgent class in 'AiAgent/PirateAgent.php'
protected $history = 'session';

LarAgent comes with 5 built-in drivers for history, including cache and JSON. Choose one of them or easily create your own.

Powering Up with Tools

Tools = real actions your agent can perform. Think API calls, calculations, reading data from DB, or even fallback logic.

Define tools with PHP attributes easily, right inside your Agent class:

use LarAgent\Attributes\Tool;

// ...
#[Tool('Get the current treasure location')]
public function getTreasureLocation($island = 'Tortuga') {
    return "X marks the spot on {
    $island}!";
}

Note: There are 2 more ways to create the tools, check out the documentation for details.

Real-World Integration Scenarios

LarAgent integrates seamlessly into real-world applications:

  • Customer support chatbots.
  • Admin panel content generators.
  • AI-driven feedback for forms.

And anything you can imagine. Whether you're using cloud services or local models, LarAgent scales smoothly and gives you great control over internal processes via events and hooks.

What Makes LarAgent Special

Why choose LarAgent?

  • No learning curve—Laravel-native experience.
  • Supports multiple AI providers (OpenAI, Gemini, Ollama, OpenRouter, etc.).
  • Frame for Agent development and maintenance.
  • Stream responses live to users.
  • Robust event and tool systems.

Conclusion and What’s Next

You've now seen how easy it is to get started with LarAgent and integrate powerful AI into your Laravel apps. It's quick, intuitive, and scalable.

Ready to build your own AI assistant? Check out LarAgent on GitHub for documentation, community support, and contribute to its growth.

So, what's the first agent you’re planning to create? A friendly helper or something quirky? Let us know in the community Discord server!

Happy coding! 🚀


Blog post banner image by Jess Pickup.