Laravel 13 introduced a first-party AI SDK (laravel/ai) that provides a unified, provider-agnostic interface for building AI features directly within your Laravel application, eliminating the need for separate Python microservices or unmaintained third-party wrappers . It supports major providers like OpenAI, Anthropic, Gemini, and Ollama, and handles text generation, agents, tool-calling, embeddings, image/audio generation, and vector search.
Installation and Configuration
To get started, install the package via Composer and run the installation command to publish the configuration and migration files (which set up tables for conversational memory) :
composer require laravel/ai
php artisan ai:install
php artisan migrate
You can then configure your default provider and API keys in config/ai.php and your .env file.
Building a Basic Agent (Example)
In the Laravel AI SDK, AI interactions are encapsulated within "Agents." You can create a dedicated agent class using an Artisan command, which serves as a controller for your LLM logic, holding system prompts and configurations.
1. Generate the Agent:
php artisan make:agent SupportAssistant
2. Define the Instructions (app/Ai/Agents/SupportAssistant.php):
namespace AppAiAgents;
use LaravelAiAgent;
use LaravelAiContractsPromptable;class SupportAssistant extends Agent implements Promptable
{
/**
* Define the system instructions for this agent.
*/
public function instructions(): string
{
return You are an expert customer support analyst. Analyze the provided text and extract key frustrations. ;
}
}
3. Use the Agent in Your Application:
You can call the agent fluently from your controllers or routes:
use AppAiAgentsSupportAssistant;
$transcript = "I ve been trying to reset my password for three days and the email never arrives!";
$response = SupportAssistant::make()->prompt($transcript);
echo $response->text();
Key Capabilities
1.Conversational Memory: By using the RemembersConversations trait in your agent, Laravel automatically persists chat history to your database and injects the context into subsequent API requests, making it easy to build chatbots.
2.Tools (Function Calling): You can bind standard PHP methods to the AI as tools using the #[Tool] attribute. The SDK manages the round-trip, allowing the model to decide when to call your Eloquent queries or business logic.
3.Structured Output: You can force the agent to return predictable data structures (like JSON) by defining a schema, which is useful for enterprise applications that need to process the AI is response programmatically.
4.Multi-Agent Workflows: The SDK supports complex patterns like Prompt Chaining (where one agent is output feeds another) and Routing, which can be orchestrated using Laravel is Pipeline or Concurrency facades.