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

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

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

  [ home ](https://www.msaied.com)    [ articles ](https://www.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://www.msaied.com/articles?category=laravel) [  AI ](https://www.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%2Fwww.msaied.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%2Fwww.msaied.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://www.msaied.com/articles) 

 [ ![Read/Write Splitting, Connection Pooling, and Sticky Reads in Laravel](https://cdn.msaied.com/295/d977bd189583149245c03d6d763d9db5.png) laravel database performance 

### Read/Write Splitting, Connection Pooling, and Sticky Reads in Laravel

Learn how Laravel's database layer handles read/write splitting, when sticky reads matter, and how to layer Pg...

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

 26 Jun 2026     4 min read  

  Read    

 ](https://www.msaied.com/articles/readwrite-splitting-connection-pooling-and-sticky-reads-in-laravel-2) [ ![Laravel Service Container: Contextual Binding, Tagging, and Method Injection](https://cdn.msaied.com/294/e5b9d047bd33c3f8b80069ef6a178884.png) laravel service-container dependency-injection 

### Laravel Service Container: Contextual Binding, Tagging, and Method Injection

Go beyond basic binding. Learn how contextual binding resolves different implementations per consumer, how tag...

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

 26 Jun 2026     3 min read  

  Read    

 ](https://www.msaied.com/articles/laravel-service-container-contextual-binding-tagging-and-method-injection-1) [ ![PostgreSQL JSONB in Laravel: Indexing, Querying, and Casting Without the Pain](https://cdn.msaied.com/293/f392a5ea52536901eac9677ffa2d070d.png) laravel postgresql eloquent 

### PostgreSQL JSONB in Laravel: Indexing, Querying, and Casting Without the Pain

JSONB columns unlock flexible schemas, but most Laravel apps leave performance on the table. Learn how to inde...

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

 25 Jun 2026     4 min read  

  Read    

 ](https://www.msaied.com/articles/postgresql-jsonb-in-laravel-indexing-querying-and-casting-without-the-pain) 

   [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://www.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://www.msaied.com)
- [Projects](https://www.msaied.com/projects)
- [Articles](https://www.msaied.com/articles)
- [Certificates](https://www.msaied.com/certificates)
- [Contact](https://www.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)
