Adding AI to an existing Laravel application is less about the model and more about engineering around it. This is the pattern I use when clients ask for AI features in their apps.
1. Wrap the provider in your own service
Do not scatter API calls through controllers. Create one service class with a small interface, so you can switch between Claude and OpenAI, or use both.
interface LlmClient
{
public function complete(array $messages, array $options = []): LlmResult;
}
Implement it for each provider using Laravel's HTTP client. Keep API keys in .env and config, never in code.
2. Run AI work in queues
Model calls can take several seconds. Doing them inside a web request makes your app feel slow and causes timeouts. Dispatch a job instead:
SummarizeTicket::dispatch($ticket)->onQueue('ai');
Run a dedicated ai queue worker with sensible timeouts and retries, and broadcast the result to the browser with Laravel Reverb or a WebSocket when it is ready.
3. Ground the model in your data
A model answering from general knowledge will make things up about your business. Before calling it:
- Fetch the relevant records (order, customer, product, help article).
- Put them into the prompt as clearly labeled context.
- Instruct the model to answer only from that context and to say when it cannot.
For larger knowledge bases, store embeddings and retrieve the top matches for each question.
4. Use tool calling for actions
Both Claude and OpenAI support tool calling: you describe functions such as get_order_status or create_refund_request, and the model asks to call them with arguments. Your code runs the function, returns the result, and the model continues.
Rules I follow:
- Validate every argument the model sends, exactly like user input.
- Read-only tools can run automatically; tools that change data or money need approval.
- Log every tool call with its inputs and outputs.
5. Ask for structured output
When you need data, not prose, ask for JSON with a defined schema and validate it with Laravel's validator before saving anything. Retry once with the validation errors if it fails.
6. Control cost from day one
- Log input and output tokens per feature and per tenant.
- Use a smaller, cheaper model for classification and routing.
- Cache answers to repeated questions.
- Set monthly spending limits in the provider dashboard and in your own code.
7. Test it like any other feature
Mock the LlmClient in unit tests. Keep a small set of real example inputs and expected behaviors, and run them whenever you change prompts or models.
Ideas that work well in Laravel apps
- Ticket and email summaries for support teams.
- Smart search over your own documents.
- Drafting product descriptions or invoices.
- Classifying leads and routing them to the right person.
- Natural-language reports: "show me unpaid invoices from last month".
Want AI features in your Laravel product? Send me a short description and I will suggest the first feature worth building.