Agent Queuing allows you to execute AI agents asynchronously using Laravel’s queue system. This is useful for long-running tasks, background processing, and scenarios where you don’t need immediate responses.
Background Processing
Run LLM agents and media generation in the background without blocking requests
Laravel Integration
Uses Laravel’s native queue system - works with any queue driver (Redis, SQS, database, etc.)
Retry & Timeout
Built-in retry logic and configurable timeouts for reliable execution
Event-Driven
Dispatches events on completion and failure for easy integration
LLM agents can be queued using the fluent executor API. This is useful for tasks that may take a while to complete, such as complex analysis or multi-step reasoning.
use App\Agents\AnalysisAgent;$result = AnalysisAgent::ask('Analyze the quarterly sales data and generate insights') ->forUser($user) // Associate with user ->withSession('analysis-session') // Track with session ID ->withContext([ // Add extra context 'report_type' => 'quarterly', 'department' => 'sales', ]) ->onQueue('analysis') // Use specific queue ->delay(30) // Wait 30 seconds before processing ->tries(5) // Retry up to 5 times ->timeout(600) // 10-minute timeout ->go();// Store the job ID for tracking$jobId = $result['job_id'];
Configure automatic storage directly in the fluent chain:
Copy
// Store with auto-generated filename (ULID)ImageAgent::run('A landscape photo') ->onQueue('media') ->store() ->go();// Store with specific filenameImageAgent::run('A portrait photo') ->onQueue('media') ->storeAs('portraits/user-avatar.png') ->go();// Store to a specific diskImageAgent::run('A banner image') ->onQueue('media') ->storeAs('banners/hero.png', 's3') ->go();
use Illuminate\Support\Facades\Event;// Listen for all agent job completionsEvent::listen('agent.job.completed', function (array $data) { Log::info('Agent job completed', [ 'job_id' => $data['job_id'], 'agent_class' => $data['agent_class'], 'result' => $data['result'], ]);});// Listen for a specific agentEvent::listen('agent.analysis_agent.completed', function (array $data) { $result = $data['result']; $sessionId = $data['session_id']; // Process the analysis result ProcessAnalysisResult::dispatch($result, $sessionId);});// Listen for media generation completionEvent::listen('media.image_agent.completed', function (array $data) { $response = $data['response']; $jobId = $data['job_id']; Log::info("Image generated: " . $response->url());});// Handle failuresEvent::listen('agent.job.failed', function (array $data) { Log::error('Agent job failed', [ 'job_id' => $data['job_id'], 'agent_class' => $data['agent_class'], 'error' => $data['error'], ]); // Notify admin or trigger alerting Notification::route('slack', config('services.slack.webhook')) ->notify(new AgentJobFailed($data));});
Event payloads include the job_id, agent_class, session_id, and the full result or response object. Use these to update your database, send notifications, or trigger follow-up actions.
If you’re using Laravel Horizon, you can filter jobs by these tags:
Copy
// In your Horizon config, jobs will appear with tags like:// vizra:my_agent, session:user_123_abc// Search for all jobs from a specific agent// Filter: vizra:analysis_agent// Search for all media generation jobs// Filter: vizra:media
# Process all queuesphp artisan queue:work# Process specific queuephp artisan queue:work --queue=agents# Process multiple queues with priorityphp artisan queue:work --queue=agents,media,default# With Horizon (recommended for production)php artisan horizon
// Separate queues by workload type->onQueue('agents') // For LLM agents->onQueue('media') // For image/audio generation->onQueue('analysis') // For long-running analysis tasks