Laravel AI Agents: Failover, Queues &amp; Middleware | Mohamed Said       [Skip to content](#main)  [ ![](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png) Mohamed SaidLaravel Backend Engineer ](https://www.msaied.com/public) - [Home](https://www.msaied.com/public)
- [Projects](https://www.msaied.com/public/projects)
- [Articles](https://www.msaied.com/public/articles)
- [Certificates](https://www.msaied.com/public/certificates)
- [About](https://www.msaied.com/public#about)

           [  Contact](https://www.msaied.com/public#contact) Menu 

Menu
----

Close 

 - [HomeStart here](https://www.msaied.com/public)
- [ProjectsCase studies](https://www.msaied.com/public/projects)
- [ArticlesEngineering notes](https://www.msaied.com/public/articles)
- [CertificatesCredentials](https://www.msaied.com/public/certificates)
- [AboutHow I work](https://www.msaied.com/public#about)
- [ContactGet in touch](https://www.msaied.com/public#contact)

  [Start a conversation](https://www.msaied.com/public#contact) [WhatsApp](https://wa.me/201094619204) [Email](mailto:hello@msaied.com) 

 1. [Home](https://www.msaied.com/public)
2. /
3. [Articles](https://www.msaied.com/public/articles)
4. /
5. [Laravel](https://www.msaied.com/public/articles?category=laravel)
6. /
7. Ship AI with Laravel: Failover, Queues, and Middleware for AI Agents

   [Laravel](https://www.msaied.com/public/articles?category=laravel) [AI](https://www.msaied.com/public/articles?category=ai) 

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

 Learn how to make Laravel AI agents production-ready with automatic provider failover, background queue processing, and middleware for logging, rate limiting, and cost tracking.

 ![](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp) [Mohamed Said](https://www.msaied.com/public#person) Published 24 Jun 2026 · Updated 24 Jun 2026 · 3 min read

ShareCopy linkCopied

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

  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)

 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 

- [Laravel](https://www.msaied.com/public/articles?search=Laravel)
- [AI Agents](https://www.msaied.com/public/articles?search=AI%20Agents)
- [Queues](https://www.msaied.com/public/articles?search=Queues)
- [Middleware](https://www.msaied.com/public/articles?search=Middleware)
- [Provider Failover](https://www.msaied.com/public/articles?search=Provider%20Failover)
- [PHP](https://www.msaied.com/public/articles?search=PHP)

 Frequently asked questions 
---------------------------

  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.

   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.

   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.

   ![Mohamed Said](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp)About the author
----------------

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

[About](https://www.msaied.com/public#about) [GitHub ↗](https://github.com/EG-Mohamed) [LinkedIn ↗](https://www.linkedin.com/in/msaiedm/) [WhatsApp ↗](https://wa.me/201094619204) [Email Address ↗](mailto:hello@msaied.com) [My CV ↗](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)  

   [Previous articleLaravel Queues: Reliable Job Middleware, Idempotency, and Graceful Failure Handling](https://www.msaied.com/public/articles/laravel-queues-reliable-job-middleware-idempotency-and-graceful-failure-handling) [Next articleRAG in Laravel: pgvector, Embeddings, and Retrieval-Augmented Generation in Practice](https://www.msaied.com/public/articles/rag-in-laravel-pgvector-embeddings-and-retrieval-augmented-generation-in-practice)  

   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)

 ###  Have a technical challenge?

 Tell me what you’re building. I reply within two working days.

[Start a conversation](https://www.msaied.com/public#contact) 

   Related articles
-----------------

 [ ![](https://cdn.msaied.com/740/cce86edc21eddcbdd2f2454fadaf9c70.png)  · 3 min read### The Pipeline Pattern in Laravel: Custom Pipelines Beyond Middleware

5 Oct 2026 ](https://www.msaied.com/public/articles/the-pipeline-pattern-in-laravel-custom-pipelines-beyond-middleware-1) [ ![](https://cdn.msaied.com/739/2d6897fdcdcf090613f96f72a64b8a78.png)  · 4 min read### MySQL Full-Text Search in Laravel: Indexes, Relevance Scoring, and Boolean Mode

4 Oct 2026 ](https://www.msaied.com/public/articles/mysql-full-text-search-in-laravel-indexes-relevance-scoring-and-boolean-mode) [ ![](https://cdn.msaied.com/738/073696a3fefe18bec825beec5ac658f5.png)  · 4 min read### Laravel Queue Rate-Limited Middleware: Throttling Jobs Without Losing Work

4 Oct 2026 ](https://www.msaied.com/public/articles/laravel-queue-rate-limited-middleware-throttling-jobs-without-losing-work) 

  Have a technical challenge?
----------------------------

Tell me what you’re building. I reply within two working days.

 [Discuss your project ↗](https://www.msaied.com/public#contact) 

  © 2026 Mohamed Said · Built with Laravel, meant to last.Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

 - [Home](https://www.msaied.com/public)
- [Articles](https://www.msaied.com/public/articles)
- [Certificates](https://www.msaied.com/public/certificates)
- [GitHub](https://github.com/EG-Mohamed)
- [LinkedIn](https://www.linkedin.com/in/msaiedm/)
- [WhatsApp](https://wa.me/201094619204)
- [Email Address](mailto:hello@msaied.com)
- [My CV](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)
- [Sitemap](https://www.msaied.com/public/sitemap.xml)
