Adding an AI Assistant to Your Laravel App: A Practical Guide

Adding an AI Assistant to Your Laravel App: A Practical Guide

You do not need to rewrite your PHP app in Python to add AI. Here is a clean, production-ready pattern for building a Laravel AI assistant with LLMs.

PHP teams often assume AI features require a separate Python stack. For most product features — assistants, summaries, classification, drafting — they do not. A large language model is just an HTTPS API.

Architecture

  • A small service class wraps the provider API.
  • Prompts live in versioned templates, not scattered strings.
  • Slow calls run in queued jobs or stream to the browser.
  • Every request and response is logged for quality review and cost tracking.

A minimal service

namespace App\Services;

use Illuminate\Support\Facades\Http;

class AiAssistant
{
    public function answer(string $question, string $context): string
    {
        $response = Http::withToken(config('services.llm.key'))
            ->timeout(60)
            ->retry(2, 500)
            ->post(config('services.llm.url'), [
                'model'    => config('services.llm.model'),
                'messages' => [
                    ['role' => 'system', 'content' => view('prompts.support', compact('context'))->render()],
                    ['role' => 'user', 'content' => $question],
                ],
            ])
            ->throw();

        return $this->extractText($response->json());
    }
}

Keep the provider details in config so you can switch models — or providers — without touching business logic.

Grounding answers in your data

Pass relevant context (help articles, order details, product data) into the prompt. For larger knowledge bases, use retrieval-augmented generation: embed your documents, store them in a vector database and fetch the most relevant chunks per question.

Production checklist

  • Rate-limit per user and cap tokens per request
  • Never send secrets or unnecessary personal data in prompts
  • Validate structured outputs (JSON) before using them
  • Show sources and offer a human hand-off for support use cases
  • Track cost per feature and per customer

Testing and monitoring your Laravel AI assistant

A Laravel AI assistant needs the same discipline as any other feature — plus a few AI-specific checks:

  • Fake the HTTP layer in tests. Laravel’s HTTP client lets you fake responses, so unit tests stay fast and free while still covering your logic.
  • Keep an evaluation set. Store real questions with good answers and run them whenever you change prompts or models.
  • Log every call. Record the prompt version, model, tokens used, latency and outcome (without storing sensitive personal data unnecessarily).
  • Rate limit and queue. Use Laravel’s rate limiting and queues so heavy users or slow responses cannot affect the rest of the application.
  • Alert on cost and errors. Notify the team when spend or error rates move outside normal ranges.

These patterns use standard features documented in the Laravel documentation, so any Laravel developer can maintain your AI assistant long after launch.

We build AI features into existing PHP, Laravel, Magento and WordPress platforms. Explore our AI services.

Need a hand?

Stuck on a build?
Let's solve it together.

Our engineers fix, upgrade and scale Magento, WordPress and modern stacks every day.