Laravel AI Agents: Failover, Queues &amp; Middleware | Mohamed Said        [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://msaied.com) [ Home ](https://msaied.com) [ Projects ](https://msaied.com/projects) [ Articles  ](https://msaied.com/articles) [ Certificates ](https://msaied.com/certificates) [ Contact ](https://msaied.com#contact-section) 

       [  ](https://github.com/EG-Mohamed)       

 [ Home ](https://msaied.com) [ Projects ](https://msaied.com/projects) [ Articles ](https://msaied.com/articles) [ Certificates ](https://msaied.com/certificates) [ Contact ](https://msaied.com#contact-section) 

  [ home ](https://msaied.com)    [ articles ](https://msaied.com/articles)    Ship AI with Laravel: Failover, Queues, and Middleware for AI Agents        On this page       1. [  Why AI Agents Break in Production ](#why-ai-agents-break-in-production)
2. [  1. Provider Failover ](#1-provider-failover)
3. [  2. Background Queue Processing ](#2-background-queue-processing)
4. [  3. Agent Middleware ](#3-agent-middleware)
5. [  Logging Middleware ](#logging-middleware)
6. [  Rate Limiting Middleware ](#rate-limiting-middleware)
7. [  Cost Tracking Middleware ](#cost-tracking-middleware)
8. [  Key Takeaways ](#key-takeaways)

  ![Ship AI with Laravel: Failover, Queues, and Middleware for AI Agents](https://cdn.msaied.com/283/f0a6d6a6f22d9131bacb96bae1bfc10b.png)

 [  Laravel ](https://msaied.com/articles?category=laravel) [  AI ](https://msaied.com/articles?category=ai)  #Laravel   #AI Agents   #Queues   #Middleware   #Provider Failover   #PHP  

 Ship AI with Laravel: Failover, Queues, and Middleware for AI Agents 
======================================================================

     24 Jun 2026      3 min read    ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said  

       Table of contents

1. [  01   Why AI Agents Break in Production  ](#why-ai-agents-break-in-production)
2. [  02   1. Provider Failover  ](#1-provider-failover)
3. [  03   2. Background Queue Processing  ](#2-background-queue-processing)
4. [  04   3. Agent Middleware  ](#3-agent-middleware)
5. [  05   Logging Middleware  ](#logging-middleware)
6. [  06   Rate Limiting Middleware  ](#rate-limiting-middleware)
7. [  07   Cost Tracking Middleware  ](#cost-tracking-middleware)
8. [  08   Key Takeaways  ](#key-takeaways)

 Why AI Agents Break in Production
---------------------------------

Development is forgiving. Production is not. A single AI provider outage at 2 AM, a sudden spike of concurrent users, or a mysterious bad response with no audit trail — these are the real problems that ship with your AI features if you don't plan for them.

This tutorial covers three infrastructure patterns that close those gaps: **provider failover**, **queue-based processing**, and **agent middleware**.

---

1. Provider Failover
--------------------

The simplest change with the biggest impact. Instead of locking your agent to a single provider, pass an array of providers to the SDK. If OpenAI fails, the SDK automatically retries with Anthropic, then Gemini — same agent, same tools, same instructions.

```php
$agent->withProviders([
    'openai',
    'anthropic',
    'gemini',
]);

```

Start by setting this up at the call site, then move the failover chain into your config file so you can adjust it without touching application code.

To stay informed when a provider drops, listen for the `AgentFailedOver` event:

```php
Event::listen(AgentFailedOver::class, function ($event) {
    // send alert, log to monitoring, etc.
});

```

Your customers never see an error. You get an alert the moment something goes wrong.

---

2. Background Queue Processing
------------------------------

Not every AI call needs to block the HTTP request. The `TicketClassifier` from an earlier episode is a good candidate: swap `prompt()` for `queue()` and the classification runs through Laravel's queue system in the background.

```php
// Before
$agent->prompt($ticket->body);

// After
$agent->queue($ticket->body);

```

The customer receives an instant confirmation. The AI work — including the full failover chain — happens asynchronously on a queue worker. This pattern keeps response times fast under load and makes your AI features far more resilient to bursts of traffic.

---

3. Agent Middleware
-------------------

The SDK lets you intercept every prompt and response flowing through an agent, exactly like HTTP middleware. Three middleware layers are worth building for any production agent:

### Logging Middleware

Capture each prompt alongside response metadata: token counts, provider used, and duration. This is your audit trail for debugging and compliance.

### Rate Limiting Middleware

Enforce per-user limits — for example, ten prompts per minute — before any other work happens.

### Cost Tracking Middleware

Use token counts to calculate spend per user, per agent, and per day. This is the foundation for billing, budgeting, and anomaly detection.

Attach all three to the agent in a deliberate order:

```php
$agent->withMiddleware([
    RateLimitMiddleware::class,   // reject early
    LoggingMiddleware::class,     // log only accepted requests
    CostTrackingMiddleware::class,
]);

```

Rate limiting runs first so you never waste resources logging or tracking a request you're about to reject.

---

Key Takeaways
-------------

- Pass an array of providers to enable automatic failover with no changes to agent logic.
- Move the failover chain into config so it can be updated without a code deploy.
- Listen for `AgentFailedOver` to get real-time alerts when a provider drops.
- Use `queue()` instead of `prompt()` to move AI work off the request lifecycle.
- Build logging, rate limiting, and cost tracking as reusable middleware.
- Order middleware deliberately: rate limit before logging, log before cost tracking.

---

The full source code for this series is available on GitHub: 

Watch the video tutorial and read the original article at 

 Found this useful?

          [  ](https://twitter.com/intent/tweet?url=https%3A%2F%2Fmsaied.com%2Farticles%2Fship-ai-with-laravel-failover-queues-and-middleware-for-ai-agents&text=Ship+AI+with+Laravel%3A+Failover%2C+Queues%2C+and+Middleware+for+AI+Agents) [  ](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fmsaied.com%2Farticles%2Fship-ai-with-laravel-failover-queues-and-middleware-for-ai-agents) 

 Frequently Asked Questions 
----------------------------

  3 questions  

     Q01  How does provider failover work for Laravel AI agents?        Instead of binding the agent to a single provider, you pass an array of providers (e.g., OpenAI, Anthropic, Gemini) to the SDK. If the first provider fails, the SDK automatically retries with the next one in the list. The agent, tools, and instructions stay the same — only the underlying provider changes. You can also listen for the AgentFailedOver event to receive real-time alerts when a failover occurs. 

      Q02  When should I use queue() instead of prompt() for an AI agent?        Use queue() when the AI response does not need to be returned synchronously to the user. For example, ticket classification or background enrichment tasks can run on a queue worker while the user receives an instant confirmation. This keeps HTTP response times fast under load and still benefits from the full failover chain. 

      Q03  In what order should I attach middleware to a Laravel AI agent?        Rate limiting should run first so requests are rejected before any logging or cost tracking occurs. Logging middleware should run second to capture only accepted requests. Cost tracking middleware should run last. This order avoids wasting resources on requests that will ultimately be rejected. 

  Continue reading

 More Articles 
---------------

 [ View all    ](https://msaied.com/articles) 

 [ ![Laravel Reverb WebSocket Presence Channels: Private State Without a Redis Bottleneck](https://cdn.msaied.com/285/4691db3cdad180d19e485611a8732087.png) laravel reverb websockets 

### Laravel Reverb WebSocket Presence Channels: Private State Without a Redis Bottleneck

Presence channels in Laravel Reverb let you track who is online without hammering Redis on every heartbeat. He...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 24 Jun 2026     4 min read  

  Read    

 ](https://msaied.com/articles/laravel-reverb-websocket-presence-channels-private-state-without-a-redis-bottleneck) [ ![Typed Enums as First-Class Domain Citizens in Laravel with PHP 8.3](https://cdn.msaied.com/282/71a8fc3e4cf4239b1bf6d38d57e0b985.png) laravel php8.3 enums 

### Typed Enums as First-Class Domain Citizens in Laravel with PHP 8.3

Go beyond simple enum labels. Learn how to attach behaviour, implement interfaces, and use backed enums as Elo...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 24 Jun 2026     3 min read  

  Read    

 ](https://msaied.com/articles/typed-enums-as-first-class-domain-citizens-in-laravel-with-php-83) [ ![RAG in Laravel: pgvector, Embeddings, and Retrieval-Augmented Generation in Practice](https://cdn.msaied.com/281/8d2ac57c0e69d3ff9f1e68faf0e4d10c.png) laravel ai pgvector 

### RAG in Laravel: pgvector, Embeddings, and Retrieval-Augmented Generation in Practice

Build a production-ready RAG pipeline in Laravel using pgvector, OpenAI embeddings, and a clean retrieval laye...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 24 Jun 2026     4 min read  

  Read    

 ](https://msaied.com/articles/rag-in-laravel-pgvector-embeddings-and-retrieval-augmented-generation-in-practice) 

   [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://msaied.com)Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

Explore

- [Home](https://msaied.com)
- [Projects](https://msaied.com/projects)
- [Articles](https://msaied.com/articles)
- [Certificates](https://msaied.com/certificates)
- [Contact](https://msaied.com#contact-section)

Connect

- [   hello@msaied.com ](mailto:hello@msaied.com)
- [   +20 109 461 9204 ](tel:+201094619204)

© 2026 Mohamed Said. All rights reserved.

 [  ](https://github.com/EG-Mohamed) [  ](https://www.linkedin.com/in/msaiedm/) [  ](https://wa.me/201094619204) [  ](mailto:hello@msaied.com) [  ](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)
