Real-world examples of using Laravel AI SDK
The Laravel team recently released the Laravel AI SDK that provides a simple and elegant way to integrate AI capabilities into your Laravel applications. The SDK offers a clean and intuitive API that allows developers to easily interact with AI models and services.
Essentially, the Laravel AI SDK abstracts away the complexities of working with AI models (such as OpenAI, Anthropic, Gemini, Mistral, Ollama, and more) and provides a seamless experience for developers to leverage AI in their applications. It supports various AI services, including natural language processing, image recognition, and more.
You can check this page to learn how to get started with the Laravel AI SDK and explore its features. In this article, though, I want to share some real-world examples of how you can use the Laravel AI SDK in your applications.
- Mining user data for insights
- Code Review Bot for PRs (GitHub / GitLab Integration)
- Personalized Learning / Quiz System for an EdTech App
- In closing
Mining user data for insights
The simplest use case of the Laravel AI SDK is to mine user data for insights. For instance, you can use the SDK to analyze user models and related data to extract valuable insights.
For instance, you can use the User + Order Eloquent models as context for your AI queries.
Here’s an AccountAssistant assistant class that uses the Laravel AI SDK to analyze user data and provide insights:
// app/Ai/Agents/AccountAssistant.php
namespace App\Ai\Agents;
use App\Models\User;
use App\Models\Order;
use Laravel\Ai\Contracts\Agent;
use Laravel\Ai\Contracts\Conversational;
use Laravel\Ai\Promptable;
use Stringable;
class AccountAssistant implements Agent, Conversational
{
use Promptable;
public function __construct(public User $user) {}
public function instructions(): Stringable|string
{
return 'You are an assistant helping a customer with their account and recent orders.';
}
public function messages(): iterable
{
// You might combine past chat messages from a History model,
// but you can also “prime” the conversation with DB data:
$orders = Order::where('user_id', $this->user->id)
->latest()
->limit(5)
->get()
->map(fn ($order) => "- #{$order->id} ({$order->status}) total {$order->total}");
return [
// System / context message built from Eloquent:
new \Laravel\Ai\Messages\Message(
'system',
"Here are the user's 5 most recent orders:\n".$orders->implode("\n")
),
];
}
}
And then use it like this:
$assistant = \App\Ai\Agents\AccountAssistant::make(user: $user);
$response = $assistant->prompt('Where is my last order?');
return (string) $response;
In this example, the AccountAssistant class implements the Agent and Conversational interfaces provided by the Laravel AI SDK. The instructions method provides context for the AI model, while the messages method retrieves the user’s recent orders from the database and formats them as a system message.
When you call the prompt method with a user query (e.g., “Where is my last order?”), the AI model will analyze the provided context and generate a response based on the user’s recent orders.
Code Review Bot for PRs (GitHub / GitLab Integration)
You can use the SDK to build an internal “AI reviewer” that comments on pull requests in your Laravel monorepo.
At a high level:
-
When a PR is opened or updated, your CI pipeline (or a small Laravel service) fetches the diff and key files.
-
You create an agent like
CodeReviewerwhoseinstructions()explain your team’s standards: coding style, architecture rules, security expectations, performance guidelines, etc. -
In
prompt(), you pass a structured prompt: a short summary of the PR, the diff, and maybe a list of changed files as attachments (e.g.Files\Document::fromString($diff, 'text/plain')). -
The agent uses structured output to return something like:
-
summary: overall explanation of changes -
issues: list of problems with severity and file/line hints -
suggestions: refactors or tests to add
-
-
Your Laravel app then maps that output to actual comments on the PR via the Git hosting API.
This leverages agents, attachments, and structured output, but in a domain (code review automation) not explicitly shown in the docs.
Personalized Learning / Quiz System for an EdTech App
Imagine you’re building an online course platform and want smarter, adaptive practice for students.
You could use the AI SDK to:
- Generate personalized quizzes from course content
When a student finishes a lesson, call an agent like QuizGenerator with:
-
Instructions: “You are an exam coach. Generate questions only from the given lesson content, aligned to this student’s weaknesses.”
-
Messages: include a summary of what the student just studied, plus their past quiz results.
-
Attachments: lesson notes or slides as
Files\Document::fromStorage(...).The agent uses structured output to return a schema like:You then persist these questions in yourquizzesandquiz_questionstables. -
Adapt difficulty using conversation context
Implement Conversational or RemembersConversations so the agent can see previous attempts, topics the student struggled with, and feedback given. The next quiz can intentionally focus on weak areas or gradually increase difficulty.
- Explain answers on demand
A second agent, AnswerExplainer, takes the student’s wrong answer and the original question as prompt, and returns a tailored explanation (“why B is wrong and C is right”), again via structured output so you can show explanations, hints, and follow‑up questions cleanly in your UI.
-
Enhance with embeddings and reranking
-
Use Embeddings +
whereVectorSimilarToto find the most relevant paragraphs in your course material for each question. -
Use Reranking to reorder candidate questions so the most relevant and appropriate ones are presented first.
This gives you an adaptive learning system built entirely on top of the Laravel AI SDK primitives: agents, structured output, files/attachments, conversation memory, embeddings, and reranking.
In closing
These are just a few examples of how you can use the Laravel AI SDK in real-world applications. The SDK provides a lot of flexibility and power (such as remembering conversations and middleware), allowing you to build intelligent features that can enhance user experience and provide valuable insights.
Whether you’re mining user data, automating code reviews, or creating personalized learning experiences, the Laravel AI SDK has you covered.
👋 Hi there! This is Amit, again. I write articles about all things web development. If you enjoy my work (the articles, the open-source projects, my general demeanour... anything really), consider leaving a tip & supporting the site. Your support is incredibly appreciated!