Persistent long-term memory for Laravel AI agents. Two tools — search and save — that let agents remember and recall information across conversations, backed by pgvector.
composer require giantpeach/laravel-ai-memoryRun the migration:
php artisan migrateOptionally publish the config:
php artisan vendor:publish --tag=ai-memory-configAdd Memory::tools() to any agent's tools() method:
use GiantPeach\AiMemory\Facades\Memory; use Laravel\Ai\Contracts\Agent; use Laravel\Ai\Contracts\HasTools; use Laravel\Ai\Promptable; class PersonalAssistant implements Agent, HasTools { use Promptable; public function __construct(private User $user) {} public function instructions(): string { return 'You are a personal assistant. Use memory to recall past preferences.'; } public function tools(): iterable { return Memory::tools($this->user); } }The agent now has two tools available:
- Search memory — vector similarity search over stored memories (uses the SDK's
SimilaritySearch) - Save memory — stores a new memory with an auto-generated embedding
For the common single-owner case, the HasMemories trait provides sugar:
use GiantPeach\AiMemory\Concerns\HasMemories; class PersonalAssistant implements Agent, HasTools { use Promptable, HasMemories; public function __construct(private User $user) {} public function memoryOwner(): Model { return $this->user; } public function tools(): iterable { return $this->memoryTools(); } }Memories are associated with Eloquent models via a polymorphic relationship. Memory::tools() accepts flexible scoping to handle different scenarios.
Search and save both target the same model:
Memory::tools($this->user);Search across multiple owners, save to a specific one:
Memory::tools( search: [$this->team, $this->user], save: $this->user, );Scope to an org or team only:
Memory::tools($this->team);if ($this->user) { return Memory::tools(search: [$this->team, $this->user], save: $this->user); } return Memory::tools($this->team);Add the Memorable trait to any Eloquent model to access its memories directly:
use GiantPeach\AiMemory\Concerns\Memorable; class User extends Model { use Memorable; } $user->memories; $user->memories()->where('source', 'agent')->count();- Save — when the agent calls the save tool, a
Memoryrecord is created with the content and an embedding generated inline viaStr::of($content)->toEmbeddings() - Search — when the agent calls the search tool, the SDK's
SimilaritySearchauto-embeds the query and performs a pgvector similarity search, filtered to the scoped owner(s)
Memories are atomic — each one is a single row with its own embedding. No chunking is needed since agent-created memories are short, focused pieces of information.
// config/ai-memory.php return [ // Must match your embedding model (1536 for text-embedding-3-small) 'dimensions' => 1536, 'search' => [ 'max_results' => 10, 'min_similarity' => 0.4, ], ];