Learn how to listen to and handle LarAgent events in your Laravel application
LarAgent dispatches Laravel events at key points during agent execution. This allows you to hook into the agent lifecycle using Laravelβs native event system for logging, monitoring, analytics, and more.
namespace App\Listeners;use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Queue\InteractsWithQueue;use LarAgent\Events\AgentInitialized;class AgentListener{ public function __construct() { // } public function handle(AgentInitialized $event): void { // Access the agent DTO dd('Agent has been initialized:', $event->agentDto); }}
3
Register the event listener
Open app/Providers/AppServiceProvider.php and register the event with its listener:
namespace App\Providers;use Illuminate\Support\ServiceProvider;use Illuminate\Support\Facades\Event;use LarAgent\Events\AgentInitialized;use App\Listeners\AgentListener;class AppServiceProvider extends ServiceProvider{ public function register(): void { // } public function boot(): void { Event::listen( AgentInitialized::class, AgentListener::class ); }}
Now whenever an agent is initialized, your handle method will be executed.
Use agent hooks for agent-specific customizations that need to modify behavior. Use Laravel events for logging, monitoring, analytics, and other cross-cutting concerns that should be decoupled from your agent classes.