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.
- Install LarAgent:
1composer require maestroerror/laragent
- Publish configuration:
1php artisan vendor:publish --tag=laragent-config
- Set your API key (OpenAI or local models like Ollama):
1OPENAI_API_KEY=your-openai-api-key2# Or3GEMINI_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:
1php 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:
1public function instructions() {2 return "Ye be a pirate assistant, speakin' in pirate lingo, matey!";3}
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:
1public function prompt($message) {2 return "Arrr, here be the message: {$message}";3}
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:
1php artisan agent:chat PirateAgent
Or integrate within your Laravel controller:
1echo PirateAgent::for('chat-session')->respond('Hello!');
Where are the chats stored? You choose, by setting the agent property:
1// PirateAgent class in 'AiAgent/PirateAgent.php'2protected $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:
1use LarAgent\Attributes\Tool;2 3// ...4 5#[Tool('Get the current treasure location')]6public function getTreasureLocation($island = 'Tortuga') {7 return "X marks the spot on {$island}!";8}
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! 🚀