AI · Laravel
Adding AI Features to Your Laravel Application Without Overengineering It
September 22, 2026
Almost every client asks at some point: "Can we add AI?" Sometimes it is a concrete idea, like summarising customer support conversations or generating product descriptions. Sometimes it is vaguer. In both cases the question is not whether it is technically possible, but how to build it in a way that works reliably in production without making the codebase unnecessarily complex.
This article is about integrating large language model (LLM) APIs into an existing Laravel application in a practical, maintainable way.
Start with a concrete use case
"Adding AI" is not a feature. A feature is: "summarise the conversation between the support agent and the customer when a ticket is closed." That is something you can build, test, and improve.
The most successful AI integrations we have built are narrow in scope and valuable on their own: classifying text, summarising content, extracting structured data from unstructured input, or generating responses against a fixed knowledge base. Start with one thing and do it well.
The integration architecture
Use a service class, not controller logic
Never embed the LLM call directly in a controller. Create a dedicated service class, such as AiSummaryService, responsible for assembling the prompt, calling the API, and handling the response. This makes the logic testable and easy to swap if you change provider later.
Run async calls through a queue
LLM API calls typically take between one and ten seconds. Never run them synchronously as part of an HTTP request unless the user is explicitly waiting for the result and the expected wait time is acceptable. In most cases, dispatch a Laravel job through the queue and persist the result to the database when it is ready.
Store results, do not recompute them
Every LLM API call costs money. If the input has not changed, there is no reason to regenerate the same summary. Persist generated content to the database and only invalidate when the source data actually changes. This reduces costs substantially at scale.
Writing prompts that work consistently
A prompt is not a one-liner. Treat it like code: version it, test it against multiple inputs, and document why specific instructions are necessary. A good prompt for a production system typically includes:
- Role assignment: tell the model what kind of assistant it is
- Task instruction: describe precisely what needs to happen
- Output format: specify whether you expect JSON, plain text, or structured markdown
- Constraints: what the model must not do, such as speculating beyond the provided context
Store prompts in config files or a dedicated directory, not hardcoded in service classes. That way you can adjust them without rewriting application code.
Error handling and fallback behaviour
LLM APIs have rate limits, transient outages, and occasionally unexpected response formats. Your application needs to handle all of this without crashing:
- Wrap calls in try-catch and log failed attempts
- Implement exponential backoff for retries on rate limit errors
- Define a fallback: what does the user see if the AI feature is unavailable?
- Validate the response structure before processing it, especially for JSON output
The AI feature is an enhancement on top of your application, not its foundation. If it goes down, the core functionality must keep working.
Monitoring costs
Without monitoring, API costs can grow quickly. Log the token count on every call (most APIs return this in the response) and track it per user, feature, or tenant. Set budget alerts with your API provider and consider a soft per-user cap if you are running a multi-tenant environment.
Caching is your most powerful cost control tool. Even a simple cache keyed on a hash of the input can reduce API usage by tens of percent for repeated or similar requests.
When it is worth doing
AI integration is worth doing when there is a repetitive task that people currently do manually, and where an imperfect but fast result is more valuable than a perfect but slow one. Text summarisation, categorisation, first-draft generation, and data extraction are all good candidates.
It is less suitable for tasks where accuracy matters more than speed, where every error has a business consequence, or where the user cannot tell the difference between good and bad generated output. In those cases, a rule-based approach is more reliable.