Ship AI with Laravel: Test Your AI System with Zero API Calls

Last updated on by

▶️ Watch the video tutorial (15 minutes)

We've built a full AI platform across this series. Agents, tools, a knowledge base, streaming chat, failover, middleware. Now comes the question nobody likes to deal with: how do you test code that calls an AI? You can't hit OpenAI in your test suite. It's slow, it costs money on every run, and the responses change each time so your assertions never hold.

In this episode we test the whole thing with Pest and zero API calls, using the SDK's faking system. It works exactly like faking mail, notifications, or queues in Laravel. You define the responses up front and assert that the right prompts went out.

I start with a quick cleanup, moving the middleware into an AI/middleware namespace, then write tests that check the agent gets prompted correctly, handles conversation context, and returns deterministic responses. I use preventStrayPrompts to catch accidental AI calls in flows that shouldn't trigger the agent at all, like ticket creation.

Then queued prompts. I assert the TicketClassifier gets queued on a valid submission and stays out of the queue on an invalid one. Custom tools get tested directly against real database records, and a failing test catches a factory mismatch that I fix on camera. That's the kind of bug tests are supposed to catch.

I feature-test the chat endpoint for authentication, validation, and correct agent interaction, then test the vector store setup and knowledge base seeding by faking store creation, document uploads, and embedding generation.

By the end all 12 AI tests pass, run fast, and never touch an external API. You can run them on every commit without worrying about cost or flakiness.

Next episode we get into security. We protect the agent against prompt injection and other attacks, including screening every message with a local LLM before it ever reaches OpenAI. Zero API cost on the blocked ones.

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

▶️ Watch Episode 10 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
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi
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
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud
Shift logo

Shift

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

Shift
Lucky Media logo

Lucky Media

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

Lucky Media
PhpStorm logo

PhpStorm

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

PhpStorm
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
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
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
Tinkerwell logo

Tinkerwell

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

Tinkerwell

The latest

View all →
Clonio CLI: Clone Production Databases With Anonymized Data image

Clonio CLI: Clone Production Databases With Anonymized Data

Read article
Laravel WhatsApp: Two Backends Behind One Facade image

Laravel WhatsApp: Two Backends Behind One Facade

Read article
Turn PHP Attributes Into Docs With Signal image

Turn PHP Attributes Into Docs With Signal

Read article
USAIGE: Track Token Usage and Costs for Laravel AI SDK Requests image

USAIGE: Track Token Usage and Costs for Laravel AI SDK Requests

Read article
Help make Filament faster! image

Help make Filament faster!

Read article
Yammi Audit Log: Track Who Really Made a Change Across Jobs and Queues image

Yammi Audit Log: Track Who Really Made a Change Across Jobs and Queues

Read article