Laravel AI SDK
Comprehensive guide for building AI-powered features with the Laravel AI SDK (laravel/ai). Contains 17 rules across 7 categories covering agents, tools, media generation, embeddings, vector stores, and testing.
When to Apply
Reference these guidelines when:
- Creating AI agents with instructions, tools, and structured output
- Prompting agents with conversation context
- Streaming or queueing agent responses
- Generating images, audio, or transcriptions
- Creating and querying vector embeddings
- Building RAG (retrieval-augmented generation) features
- Testing AI features with fakes and assertions
Rule Categories by Priority
| Priority |
Category |
Impact |
Prefix |
| 1 |
Agents |
CRITICAL |
agent- |
| 2 |
Tools |
HIGH |
tool- |
| 3 |
Embeddings & Search |
HIGH |
embed- |
| 4 |
Media Generation |
MEDIUM |
media- |
| 5 |
Files & Storage |
MEDIUM |
files- |
| 6 |
Infrastructure |
MEDIUM |
infra- |
| 7 |
Testing |
HIGH |
test- |
Quick Reference
1. Agents (CRITICAL)
agent-create-configure - Create agents with artisan, PHP attribute configuration
agent-prompting - Prompt agents, conversation context, RemembersConversations
agent-structured-output - Structured output with JSON schema
agent-streaming-async - Streaming, broadcasting, and queueing responses
agent-middleware - Agent middleware pipeline
agent-anonymous - Anonymous agents for quick interactions
2. Tools (HIGH)
tool-create - Create custom tools with schema and handle method
tool-provider - Provider tools: WebSearch, WebFetch, FileSearch, SimilaritySearch
3. Embeddings & Search (HIGH)
embed-generate-cache - Generate, store, and cache vector embeddings
embed-rerank - Rerank documents and collections by relevance
4. Media Generation (MEDIUM)
media-images - Generate, store, and queue images
media-audio-transcription - Text-to-speech and speech-to-text
5. Files & Storage (MEDIUM)
files-vector-stores - File storage and vector stores for RAG
6. Infrastructure (MEDIUM)
infra-failover - Automatic provider failover for resilience
7. Testing (HIGH)
test-agents - Fake agents, assert prompts, prevent stray prompts
test-media - Fake images, audio, and transcriptions
test-data - Fake embeddings, reranking, files, and vector stores
Essential Patterns
Creating an Agent
<?php
namespace App\Ai\Agents;
use Laravel\Ai\Contracts\Agent;
use Laravel\Ai\Promptable;
class SalesCoach implements Agent
{
use Promptable;
public function instructions(): string
{
return 'You are a sales coach, analyzing transcripts and providing feedback.';
}
}
Prompting
use App\Ai\Agents\SalesCoach;
$response = SalesCoach::make()->prompt('Analyze this sales transcript...');
return (string) $response;
Generating Images
use Laravel\Ai\Image;
$image = Image::of('A donut sitting on the kitchen counter')
->landscape()
->generate();
$path = $image->store();
Generating Embeddings
use Illuminate\Support\Str;
$embeddings = Str::of('Napa Valley has great wine.')->toEmbeddings();
Testing
use App\Ai\Agents\SalesCoach;
SalesCoach::fake(['First response', 'Second response']);
SalesCoach::make()->prompt('Analyze this...');
SalesCoach::assertPrompted('Analyze this...');
How to Use
Read individual rule files for detailed explanations and code examples.
Each rule file contains:
- YAML frontmatter with metadata (title, impact, tags)
- Brief explanation of why it matters
- Bad Example with explanation
- Good Example with explanation
- Laravel 13 and PHP 8.3 specific context and references
Full Compiled Document
For the complete guide with all rules expanded: AGENTS.md
1---2name: laravel-ai-sdk3description: Laravel AI SDK for building AI-powered features. Use when creating agents, generating images or audio, working with embeddings, vector search, or testing AI features. Triggers on tasks involving laravel/ai, AI agents, tool-calling, structured output, streaming, embeddings, reranking, or AI faking in tests.4license: MIT5---67# Laravel AI SDK89Comprehensive guide for building AI-powered features with the Laravel AI SDK (`laravel/ai`). Contains 17 rules across 7 categories covering agents, tools, media generation, embeddings, vector stores, and testing.1011## When to Apply1213Reference these guidelines when:14- Creating AI agents with instructions, tools, and structured output15- Prompting agents with conversation context16- Streaming or queueing agent responses17- Generating images, audio, or transcriptions18- Creating and querying vector embeddings19- Building RAG (retrieval-augmented generation) features20- Testing AI features with fakes and assertions2122## Rule Categories by Priority2324| Priority | Category | Impact | Prefix |25|----------|----------|--------|--------|26| 1 | Agents | CRITICAL | `agent-` |27| 2 | Tools | HIGH | `tool-` |28| 3 | Embeddings & Search | HIGH | `embed-` |29| 4 | Media Generation | MEDIUM | `media-` |30| 5 | Files & Storage | MEDIUM | `files-` |31| 6 | Infrastructure | MEDIUM | `infra-` |32| 7 | Testing | HIGH | `test-` |3334## Quick Reference3536### 1. Agents (CRITICAL)3738- `agent-create-configure` - Create agents with artisan, PHP attribute configuration39- `agent-prompting` - Prompt agents, conversation context, RemembersConversations40- `agent-structured-output` - Structured output with JSON schema41- `agent-streaming-async` - Streaming, broadcasting, and queueing responses42- `agent-middleware` - Agent middleware pipeline43- `agent-anonymous` - Anonymous agents for quick interactions4445### 2. Tools (HIGH)4647- `tool-create` - Create custom tools with schema and handle method48- `tool-provider` - Provider tools: WebSearch, WebFetch, FileSearch, SimilaritySearch4950### 3. Embeddings & Search (HIGH)5152- `embed-generate-cache` - Generate, store, and cache vector embeddings53- `embed-rerank` - Rerank documents and collections by relevance5455### 4. Media Generation (MEDIUM)5657- `media-images` - Generate, store, and queue images58- `media-audio-transcription` - Text-to-speech and speech-to-text5960### 5. Files & Storage (MEDIUM)6162- `files-vector-stores` - File storage and vector stores for RAG6364### 6. Infrastructure (MEDIUM)6566- `infra-failover` - Automatic provider failover for resilience6768### 7. Testing (HIGH)6970- `test-agents` - Fake agents, assert prompts, prevent stray prompts71- `test-media` - Fake images, audio, and transcriptions72- `test-data` - Fake embeddings, reranking, files, and vector stores7374## Essential Patterns7576### Creating an Agent7778```php79<?php8081namespace App\Ai\Agents;8283use Laravel\Ai\Contracts\Agent;84use Laravel\Ai\Promptable;8586class SalesCoach implements Agent87{88 use Promptable;8990 public function instructions(): string91 {92 return 'You are a sales coach, analyzing transcripts and providing feedback.';93 }94}95```9697### Prompting9899```php100use App\Ai\Agents\SalesCoach;101102$response = SalesCoach::make()->prompt('Analyze this sales transcript...');103104return (string) $response;105```106107### Generating Images108109```php110use Laravel\Ai\Image;111112$image = Image::of('A donut sitting on the kitchen counter')113 ->landscape()114 ->generate();115116$path = $image->store();117```118119### Generating Embeddings120121```php122use Illuminate\Support\Str;123124$embeddings = Str::of('Napa Valley has great wine.')->toEmbeddings();125```126127### Testing128129```php130use App\Ai\Agents\SalesCoach;131132SalesCoach::fake(['First response', 'Second response']);133134SalesCoach::make()->prompt('Analyze this...');135136SalesCoach::assertPrompted('Analyze this...');137```138139## How to Use140141Read individual rule files for detailed explanations and code examples.142143Each rule file contains:144- YAML frontmatter with metadata (title, impact, tags)145- Brief explanation of why it matters146- Bad Example with explanation147- Good Example with explanation148- Laravel 13 and PHP 8.3 specific context and references149150## Full Compiled Document151152For the complete guide with all rules expanded: `AGENTS.md`