$npx -y skills add fusengine/agents --skill laravel-ai-sdkUse when integrating AI agents, tool calling, embeddings, structured output, or streaming in Laravel 13 via the laravel/ai package. Covers 14+ providers (OpenAI, Anthropic, Gemini, Azure, Groq, DeepSeek, Ollama, Mistral, xAI, Cohere, ElevenLabs, Jina, VoyageAI, OpenRouter).
| 1 | # Laravel AI SDK |
| 2 | |
| 3 | ## Agent Workflow (MANDATORY) |
| 4 | |
| 5 | Before ANY implementation, use `TeamCreate` to spawn 3 agents: |
| 6 | |
| 7 | 1. **fuse-ai-pilot:explore-codebase** - Map existing AI usage (custom HTTP clients, OpenAI PHP, etc.) to migrate |
| 8 | 2. **fuse-ai-pilot:research-expert** - Verify provider model IDs and pricing on the official Laravel AI SDK docs |
| 9 | 3. **mcp__context7__query-docs** - Pull latest `laravel.com/docs/13.x/ai-sdk` examples |
| 10 | |
| 11 | After implementation, run **fuse-ai-pilot:sniper** for validation. |
| 12 | |
| 13 | --- |
| 14 | |
| 15 | ## Overview |
| 16 | |
| 17 | | Feature | Description | |
| 18 | |---------|-------------| |
| 19 | | **Unified API** | Same code surface for 14+ providers via `Lab` enum | |
| 20 | | **Agents** | Class-based with `Agent` contract + `Promptable` trait | |
| 21 | | **Tool calling** | First-party `FileSearch` + custom tools per agent | |
| 22 | | **Embeddings** | `Embeddings::for([...])->generate()` + `Str::toEmbeddings()` | |
| 23 | | **Streaming** | Native SSE + Vercel AI SDK protocol compatibility | |
| 24 | | **Structured output** | `agent(schema: fn ($s) => ...)` with `JsonSchema` | |
| 25 | |
| 26 | --- |
| 27 | |
| 28 | ## Critical Rules |
| 29 | |
| 30 | 1. **Use the `Lab` enum** - Never hard-code provider strings; use `Lab::Anthropic`, `Lab::OpenAI`, etc. |
| 31 | 2. **Configure keys in `.env`** - One env var per provider (`OPENAI_API_KEY`, `ANTHROPIC_API_KEY`, ...) read by `config/ai.php` |
| 32 | 3. **Agents are classes** - Always implement `Laravel\Ai\Contracts\Agent` and use `Promptable` trait |
| 33 | 4. **Declare tools explicitly** - Override `tools(): iterable` to expose tool calls; never assume implicit registration |
| 34 | 5. **Stream via routes** - Return `$agent->stream(...)` directly from a route; do not buffer in memory |
| 35 | |
| 36 | --- |
| 37 | |
| 38 | ## Architecture |
| 39 | |
| 40 | ``` |
| 41 | app/ |
| 42 | ├── Ai/ |
| 43 | │ ├── Agents/ |
| 44 | │ │ └── SalesCoach.php # implements Agent, uses Promptable |
| 45 | │ ├── Tools/ |
| 46 | │ │ └── SearchProducts.php # custom tool class |
| 47 | │ └── Services/ |
| 48 | │ └── EmbeddingService.php # Embeddings::for() wrapper |
| 49 | config/ |
| 50 | └── ai.php # providers, default models |
| 51 | .env # *_API_KEY entries |
| 52 | ``` |
| 53 | |
| 54 | → See [Agent.php.md](references/templates/Agent.php.md) for a full agent |
| 55 | |
| 56 | --- |
| 57 | |
| 58 | ## Reference Guide |
| 59 | |
| 60 | | Topic | Reference | When to Consult | |
| 61 | |-------|-----------|-----------------| |
| 62 | | **Installation** | [installation.md](references/installation.md) | Setting up `laravel/ai` and providers | |
| 63 | | **Agents** | [agents.md](references/agents.md) | Building agent classes with attributes | |
| 64 | | **Tools** | [tools.md](references/tools.md) | Tool calling, `FileSearch`, custom tools | |
| 65 | | **Embeddings** | [embeddings.md](references/embeddings.md) | Generating vectors for semantic search | |
| 66 | | **Streaming** | [streaming.md](references/streaming.md) | SSE + Vercel AI SDK protocol | |
| 67 | | **Structured output** | [structured-output.md](references/structured-output.md) | `agent()` helper + JSON Schema | |
| 68 | |
| 69 | ### Templates |
| 70 | |
| 71 | | Template | When to Use | |
| 72 | |----------|-------------| |
| 73 | | [Agent.php.md](references/templates/Agent.php.md) | Net new agent class | |
| 74 | | [Tool.php.md](references/templates/Tool.php.md) | Custom tool implementation | |
| 75 | | [EmbeddingService.php.md](references/templates/EmbeddingService.php.md) | Batch embedding generation | |
| 76 | | [StreamingController.php.md](references/templates/StreamingController.php.md) | SSE streaming endpoint | |
| 77 | |
| 78 | --- |
| 79 | |
| 80 | ## Quick Reference |
| 81 | |
| 82 | ### Generate text |
| 83 | |
| 84 | ```php |
| 85 | use App\Ai\Agents\SalesCoach; |
| 86 | |
| 87 | $response = (new SalesCoach)->prompt('Summarize this call'); |
| 88 | ``` |
| 89 | |
| 90 | ### Generate embeddings |
| 91 | |
| 92 | ```php |
| 93 | use Illuminate\Support\Str; |
| 94 | |
| 95 | $embedding = Str::of('Napa Valley wine')->toEmbeddings(); |
| 96 | ``` |
| 97 | |
| 98 | ### Stream |
| 99 | |
| 100 | ```php |
| 101 | Route::get('/coach', fn () => (new SalesCoach)->stream('Analyze this')); |
| 102 | ``` |
| 103 | |
| 104 | → See [Agent.php.md](references/templates/Agent.php.md) for complete example |
| 105 | |
| 106 | --- |
| 107 | |
| 108 | ## Best Practices |
| 109 | |
| 110 | ### DO |
| 111 | - Use class-level attributes (`#[Provider]`, `#[Model]`, `#[MaxSteps]`) to lock agent configuration |
| 112 | - Cache embeddings in the DB - regenerating is expensive |
| 113 | - Set explicit `#[Timeout]` to avoid runaway long generations |
| 114 | - Use `usingVercelDataProtocol()` for Next.js / SvelteKit frontends |
| 115 | |
| 116 | ### DON'T |
| 117 | - Don't declare an AI Agent without `#[Tool]` declarations if it needs to call functions - tools must be registered explicitly |
| 118 | - Don't store API keys in `config/ai.php` directly; use `env()` so values aren't committed |