Configure default storage drivers for agent context persistence
Every agent in LarAgent has a Context that manages multiple storage instances β chat history, usage tracking, identity storage, and any custom storages you register. You can configure which storage drivers these use at the agent level.
For dynamic configuration, override the defaultStorageDrivers() method:
use LarAgent\Agent;use LarAgent\Context\Drivers\CacheStorage;use LarAgent\Context\Drivers\EloquentStorage;class SupportAgent extends Agent{ protected $instructions = 'You are a helpful support agent.'; protected function defaultStorageDrivers(): array { // Use database in production, cache in development if (app()->environment('production')) { return [ new EloquentStorage(), ]; } return [ new CacheStorage('redis'), ]; }}
Use $history property or historyStorageDrivers() method:
Property
Method Override
class SupportAgent extends Agent{ // Default for all storages protected $storage = [CacheStorage::class]; // Override for chat history only protected $history = 'database';}
class SupportAgent extends Agent{ protected $storage = [CacheStorage::class]; // Override for chat history with custom model protected function historyStorageDrivers(): string|array { return [ new EloquentStorage(\App\Models\ChatMessage::class), ]; }}
For chat history-specific configuration like truncation strategies, metadata storage, and force read/save flags, see the Chat History documentation.
Both $usageStorage and $history properties accept string aliases for convenience:
class SupportAgent extends Agent{ // Using alias for usage protected $usageStorage = 'cache'; // Using alias for history protected $history = 'database';}
Alias
Driver Class
'in_memory'
InMemoryStorage
'session'
SessionStorage
'cache'
CacheStorage
'file' or 'json'
FileStorage
'database'
EloquentStorage
'database-simple'
SimpleEloquentStorage
For detailed information about each driverβs constructor arguments, custom model examples, and the fallback pattern, see the Storage Drivers reference.
use LarAgent\Context\Drivers\CacheStorage;use LarAgent\Context\Drivers\EloquentStorage;class SupportAgent extends Agent{ protected $instructions = 'You are a helpful support agent.'; // All storages use cache and database drivers protected $storage = [ CacheStorage::class, EloquentStorage::class, ];}
use LarAgent\Context\Drivers\CacheStorage;use LarAgent\Context\Drivers\FileStorage;use LarAgent\Context\Drivers\EloquentStorage;class SupportAgent extends Agent{ protected $instructions = 'You are a helpful support agent.'; // Cache with file fallback for general storages protected $storage = [ CacheStorage::class, FileStorage::class, ]; // Database for chat history (no fallback needed) protected $history = [ EloquentStorage::class ];}
class SupportAgent extends Agent{ protected $instructions = 'You are a helpful support agent.'; protected function defaultStorageDrivers(): array { return match (app()->environment()) { 'testing' => [new InMemoryStorage()], 'local' => [new CacheStorage()], default => [ new CacheStorage('redis'), new FileStorage('s3', 'agent_backup'), ], }; } protected function historyStorageDrivers(): string|array { // Always use database for chat history in non-test environments if (app()->environment('testing')) { return [new InMemoryStorage()]; } return [new EloquentStorage()]; } protected function usageStorageDrivers(): string|array { // Always use database for chat history in non-test environments if (app()->environment('testing')) { return [new InMemoryStorage()]; } return [new EloquentStorage()]; }}
Beyond chat history and usage tracking, you can create custom storages for any data your agent needs to persist β customer notes, session metadata, conversation tags, and more.Custom storages use:
DataModel β Typed, structured data classes for your storage items
Storage β Abstract base class that handles persistence, lazy loading, and dirty tracking