Laravel Tutorials

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

Published

▶️ 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

Sponsored

laravelcloud logo
Laravel Cloud

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

Visit Laravel Cloud

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article