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

Published on by

▶️ Watch the video tutorial (10 minutes)

Our support platform works great in development. Then you ship it. OpenAI has an outage at 2am and every customer gets an error. A hundred people hit the chat at once and requests pile up. A response comes back wrong and three weeks later you have no way to figure out why. This episode is about the infrastructure that handles all of that.

We start with provider failover, the scariest one. Right now the agent is locked to a single provider. I show how to pass an array instead, so if OpenAI fails the SDK automatically retries with Anthropic, then Gemini. Same agent, same tools, same instructions, just a different provider behind the scenes. The customer never knows. I set it up at the call site first, then move the whole failover chain into config so you can change it without touching code. I also add a listener for the AgentFailedOver event so you get alerted the moment a provider drops.

Next, queues. Not every AI call needs to block a request. I take the TicketClassifier from Episode 2 and swap prompt for queue, so classification runs in the background through Laravel's queue system. The customer gets an instant confirmation and the AI work happens behind the scenes, with failover still in place on the queued job.

Then middleware. The SDK lets you intercept every prompt and response flowing through an agent, exactly like HTTP middleware. I build three. One logs each prompt along with response metadata like token counts, provider, and duration. One handles per-user rate limiting at ten prompts a minute. One tracks cost using token counts so you can calculate spend per user, per agent, per day. Then I attach all three to the agent in a deliberate order, because rate limiting should run before you waste time logging or tracking a request you're about to reject.

By the end, the agent survives provider outages, handles background work, and logs everything you need to debug and budget in production.

Next episode we test the entire system with Pest, faking agents, asserting prompts, and testing tools so nothing breaks even when you ship on a Friday afternoon.

⭐ GitHub: https://github.com/harris21/ship-ai-with-laravel

▶️ Watch Episode 9 now →

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech

The latest

View all →
Route Metadata Support in Laravel 13.17 image

Route Metadata Support in Laravel 13.17

Read article
Monitor and Control Schedules, Queues, and Errors in Laravel with Watchtower image

Monitor and Control Schedules, Queues, and Errors in Laravel with Watchtower

Read article
Showcase Your PhpStorm Expertise on LinkedIn image

Showcase Your PhpStorm Expertise on LinkedIn

Read article
Privacy Filter: Detect PII in Text from Laravel image

Privacy Filter: Detect PII in Text from Laravel

Read article
NationForge: A Self-Hosted Admin Panel for Civic Organizations image

NationForge: A Self-Hosted Admin Panel for Civic Organizations

Read article
Monitor Laravel Queues, Commands, and Schedulers on Any Driver with Vigilance image

Monitor Laravel Queues, Commands, and Schedulers on Any Driver with Vigilance

Read article