Laravel AI SDK
Agent Workflow (MANDATORY)
Before ANY implementation, spawn 3 agents in parallel, one Agent call each with a name:
- fuse-ai-pilot:explore-codebase - Map existing AI usage (custom HTTP clients, OpenAI PHP, etc.) to migrate
- fuse-ai-pilot:research-expert - Verify provider model IDs and pricing on the official Laravel AI SDK docs
- mcp__context7__query-docs - Pull latest
laravel.com/docs/13.x/ai-sdk examples
After implementation, run fuse-ai-pilot:sniper for validation.
Overview
| Feature |
Description |
| Unified API |
Same code surface for 14+ providers via Lab enum |
| Agents |
Class-based with Agent contract + Promptable trait |
| Tool calling |
First-party FileSearch + custom tools per agent |
| Embeddings |
Embeddings::for([...])->generate() + Str::toEmbeddings() |
| Streaming |
Native SSE + Vercel AI SDK protocol compatibility |
| Structured output |
agent(schema: fn ($s) => ...) with JsonSchema |
Critical Rules
- Use the
Lab enum - Never hard-code provider strings; use Lab::Anthropic, Lab::OpenAI, etc.
- Configure keys in
.env - One env var per provider (OPENAI_API_KEY, ANTHROPIC_API_KEY, ...) read by config/ai.php
- Agents are classes - Always implement
Laravel\Ai\Contracts\Agent and use Promptable trait
- Declare tools explicitly - Override
tools(): iterable to expose tool calls; never assume implicit registration
- Stream via routes - Return
$agent->stream(...) directly from a route; do not buffer in memory
Architecture
app/
├── Ai/
│ ├── Agents/
│ │ └── SalesCoach.php # implements Agent, uses Promptable
│ ├── Tools/
│ │ └── SearchProducts.php # custom tool class
│ └── Services/
│ └── EmbeddingService.php # Embeddings::for() wrapper
config/
└── ai.php # providers, default models
.env # *_API_KEY entries
→ See Agent.php.md for a full agent
Reference Guide
| Topic |
Reference |
When to Consult |
| Installation |
installation.md |
Setting up laravel/ai and providers |
| Agents |
agents.md |
Building agent classes with attributes |
| Tools |
tools.md |
Tool calling, FileSearch, custom tools |
| Embeddings |
embeddings.md |
Generating vectors for semantic search |
| Streaming |
streaming.md |
SSE + Vercel AI SDK protocol |
| Structured output |
structured-output.md |
agent() helper + JSON Schema |
Templates
| Template |
When to Use |
| Agent.php.md |
Net new agent class |
| Tool.php.md |
Custom tool implementation |
| EmbeddingService.php.md |
Batch embedding generation |
| StreamingController.php.md |
SSE streaming endpoint |
Quick Reference
Generate text
use App\Ai\Agents\SalesCoach;
$response = (new SalesCoach)->prompt('Summarize this call');
Generate embeddings
use Illuminate\Support\Str;
$embedding = Str::of('Napa Valley wine')->toEmbeddings();
Stream
Route::get('/coach', fn () => (new SalesCoach)->stream('Analyze this'));
→ See Agent.php.md for complete example
Best Practices
DO
- Use class-level attributes (
#[Provider], #[Model], #[MaxSteps]) to lock agent configuration
- Cache embeddings in the DB - regenerating is expensive
- Set explicit
#[Timeout] to avoid runaway long generations
- Use
usingVercelDataProtocol() for Next.js / SvelteKit frontends
DON'T
- Don't declare an AI Agent without
#[Tool] declarations if it needs to call functions - tools must be registered explicitly
- Don't store API keys in
config/ai.php directly; use env() so values aren't committed
- Don't use
Lab::OpenAI strings - use the enum for type safety
- Don't loop manually over
Embeddings::for() items; pass the full array - the SDK batches efficiently
1---2name: laravel-ai-sdk3description: Use when integrating AI agents, tool calling, embeddings, structured output, or streaming in Laravel 13 via the `laravel/ai` package.4---56<objective>7Covers the `laravel/ai` package for Laravel 13: building class-based Agents8(Agent contract + Promptable trait), tool calling (FileSearch + custom tools),9generating and storing embeddings, structured output via JSON Schema, and10streaming (SSE + Vercel AI SDK protocol). Supports 14+ providers through the11unified `Lab` enum — OpenAI, Anthropic, Gemini, Azure, Groq, DeepSeek, Ollama,12Mistral, xAI, Cohere, ElevenLabs, Jina, VoyageAI, OpenRouter.13</objective>1415# Laravel AI SDK1617## Agent Workflow (MANDATORY)1819Before ANY implementation, spawn 3 agents in parallel, one `Agent` call each with a `name`:20211. **fuse-ai-pilot:explore-codebase** - Map existing AI usage (custom HTTP clients, OpenAI PHP, etc.) to migrate222. **fuse-ai-pilot:research-expert** - Verify provider model IDs and pricing on the official Laravel AI SDK docs233. **mcp__context7__query-docs** - Pull latest `laravel.com/docs/13.x/ai-sdk` examples2425After implementation, run **fuse-ai-pilot:sniper** for validation.2627---2829## Overview3031| Feature | Description |32|---------|-------------|33| **Unified API** | Same code surface for 14+ providers via `Lab` enum |34| **Agents** | Class-based with `Agent` contract + `Promptable` trait |35| **Tool calling** | First-party `FileSearch` + custom tools per agent |36| **Embeddings** | `Embeddings::for([...])->generate()` + `Str::toEmbeddings()` |37| **Streaming** | Native SSE + Vercel AI SDK protocol compatibility |38| **Structured output** | `agent(schema: fn ($s) => ...)` with `JsonSchema` |3940---4142## Critical Rules43441. **Use the `Lab` enum** - Never hard-code provider strings; use `Lab::Anthropic`, `Lab::OpenAI`, etc.452. **Configure keys in `.env`** - One env var per provider (`OPENAI_API_KEY`, `ANTHROPIC_API_KEY`, ...) read by `config/ai.php`463. **Agents are classes** - Always implement `Laravel\Ai\Contracts\Agent` and use `Promptable` trait474. **Declare tools explicitly** - Override `tools(): iterable` to expose tool calls; never assume implicit registration485. **Stream via routes** - Return `$agent->stream(...)` directly from a route; do not buffer in memory4950---5152## Architecture5354```55app/56├── Ai/57│ ├── Agents/58│ │ └── SalesCoach.php # implements Agent, uses Promptable59│ ├── Tools/60│ │ └── SearchProducts.php # custom tool class61│ └── Services/62│ └── EmbeddingService.php # Embeddings::for() wrapper63config/64└── ai.php # providers, default models65.env # *_API_KEY entries66```6768→ See [Agent.php.md](references/templates/Agent.php.md) for a full agent6970---7172## Reference Guide7374| Topic | Reference | When to Consult |75|-------|-----------|-----------------|76| **Installation** | [installation.md](references/installation.md) | Setting up `laravel/ai` and providers |77| **Agents** | [agents.md](references/agents.md) | Building agent classes with attributes |78| **Tools** | [tools.md](references/tools.md) | Tool calling, `FileSearch`, custom tools |79| **Embeddings** | [embeddings.md](references/embeddings.md) | Generating vectors for semantic search |80| **Streaming** | [streaming.md](references/streaming.md) | SSE + Vercel AI SDK protocol |81| **Structured output** | [structured-output.md](references/structured-output.md) | `agent()` helper + JSON Schema |8283### Templates8485| Template | When to Use |86|----------|-------------|87| [Agent.php.md](references/templates/Agent.php.md) | Net new agent class |88| [Tool.php.md](references/templates/Tool.php.md) | Custom tool implementation |89| [EmbeddingService.php.md](references/templates/EmbeddingService.php.md) | Batch embedding generation |90| [StreamingController.php.md](references/templates/StreamingController.php.md) | SSE streaming endpoint |9192---9394## Quick Reference9596### Generate text9798```php99use App\Ai\Agents\SalesCoach;100101$response = (new SalesCoach)->prompt('Summarize this call');102```103104### Generate embeddings105106```php107use Illuminate\Support\Str;108109$embedding = Str::of('Napa Valley wine')->toEmbeddings();110```111112### Stream113114```php115Route::get('/coach', fn () => (new SalesCoach)->stream('Analyze this'));116```117118→ See [Agent.php.md](references/templates/Agent.php.md) for complete example119120---121122## Best Practices123124### DO125- Use class-level attributes (`#[Provider]`, `#[Model]`, `#[MaxSteps]`) to lock agent configuration126- Cache embeddings in the DB - regenerating is expensive127- Set explicit `#[Timeout]` to avoid runaway long generations128- Use `usingVercelDataProtocol()` for Next.js / SvelteKit frontends129130### DON'T131- Don't declare an AI Agent without `#[Tool]` declarations if it needs to call functions - tools must be registered explicitly132- Don't store API keys in `config/ai.php` directly; use `env()` so values aren't committed133- Don't use `Lab::OpenAI` strings - use the enum for type safety134- Don't loop manually over `Embeddings::for()` items; pass the full array - the SDK batches efficiently