maestroerror/laragent
LarAgent is an open-source Laravel framework for building and maintaining AI agents. Define agents, tools, memories, and workflows with an Eloquent-style API, structured outputs, pluggable context/memory, multi-agent orchestration with queues, and MCP tool support.
Installation: Add LarAgent via Composer:
composer require maestroerror/laragent
php artisan vendor:publish --tag="laragent-config"
Configure Providers: Update config/laragent.php with your API keys (e.g., OpenAI, Anthropic) and preferred defaults.
Create Your First Agent: Generate an agent class:
php artisan make:agent CustomerSupportAgent
Define Agent Logic: Customize the generated agent class (app/AiAgents/CustomerSupportAgent.php) with:
instructions() method)#[Tool])$model = 'gpt-4')Test Locally: Use Tinker to interact with your agent:
php artisan tinker
$agent = new \App\AiAgents\CustomerSupportAgent();
$response = $agent->forUser(auth()->user())->respond("How do I reset my password?");
dd($response);
respond() method.Agent Lifecycle:
LarAgent\Agent and override methods like instructions().respond() with optional context (user, history name).#[Tool] and define input/output schemas.Provider Management:
$provider (e.g., ['openai', 'gemini']).$temperature = 0.3).History Handling:
forUser() to scope history to authenticated users.for() to create isolated chat histories (e.g., for("support_tickets")).Tool Integration:
#[Tool] and define parameters.Tool::make() for reusable tools outside agent classes.$parallelToolCalls = true for concurrent tool calls.AgentResponding, AgentResponded, or ToolExecuting events for custom logic./api/agents/{agent}) for frontend integration.AgentJob for async processing:
AgentJob::dispatch(new CustomerSupportAgent(), $message, $user);
LarAgent\Testing\Fakes\FakeAgent for unit tests.class OrderProcessingAgent extends Agent {
protected $tools = ['fetchOrder', 'updateOrderStatus'];
#[Tool('Fetch order details by ID')]
public function fetchOrder(string $orderId) {
return Order::findOrFail($orderId)->toArray();
}
#[Tool('Update order status')]
public function updateOrderStatus(string $orderId, string $status) {
return Order::where('id', $orderId)->update(['status' => $status]);
}
public function instructions() {
return "Process customer orders by fetching details and updating statuses.";
}
}
Tool Schema Mismatches:
#[Tool(schema: "...")] to explicitly define JSON schemas for complex tools.laravel.log for ToolValidationException errors.Rate Limits:
temperature or large context windows may trigger API rate limits.AgentRateLimited event listeners or use fallback providers.History Bloat:
CacheChatHistory or DatabaseChatHistory for persistent storage.Parallel Tool Calls:
$parallelToolCalls = false), which may slow down workflows.AgentResponding event to log input/output:
event(new AgentResponding($agent, $message, $history));
dd($agent->getTools());
fake provider for offline testing:
config(['laragent.providers.default.driver' => \LarAgent\Drivers\Fake\FakeDriver::class]);
Provider Labels:
config/laragent.php (case-sensitive).$provider = 'openai' requires 'openai' => [...] in config.Chat History Keys:
{agent_class}:{user_id}:{history_name}.$historyKey property in the agent class.Extras Handling:
reasoning_effort) must be set in $extras:protected $extras = ['reasoning_effort' => 'high'];
Custom Drivers:
LarAgent\Drivers\LlmDriver and register in config/laragent.php:'custom_driver' => [
'driver' => \App\Drivers\CustomDriver::class,
// ...
],
Chat History:
LarAgent\History\ChatHistory and publish it:php artisan make:history CustomDatabaseHistory
Events:
AgentResponded):event(new AgentResponded($agent, $response));
Tool Facade:
AppServiceProvider:Tool::extend('custom', function () {
return new CustomTool();
});
respondStreamed() for large responses to avoid memory issues.Route::middleware(['throttle:10,1'])->group(function () {
Route::post('/api/agents/{agent}', [AgentController::class, 'respond']);
});
How can I help you explore Laravel packages today?