Give your agents a memory that lasts. Learn how to build agents that remember conversations, learn from interactions, and provide personalized experiences over time.
Sessions provide conversation context while memory enables long-term knowledge retention. Together, they create intelligent, personalized agent interactions that improve over time!
Let’s see how easy it is to maintain conversations across multiple interactions!
app/Http/Controllers/ChatController.php
Copy
use App\Agents\CustomerSupportAgent;// Start a conversation with a session$response = CustomerSupportAgent::run('I need help with my order') ->forUser($user) ->withSession($sessionId) ->go();// Continue in the same session - agent remembers!$response2 = CustomerSupportAgent::run('The order number is #12345') ->forUser($user) ->withSession($sessionId) ->go();
The AgentContext is your Swiss Army knife for session management!
app/Agents/MyAgent.php
Copy
// AgentContext provides session state managementpublic function run(string $input, AgentContext $context): mixed{ // Get session ID $sessionId = $context->getSessionId(); // Store state in session $context->setState('order_id', '#12345'); // Retrieve state $orderId = $context->getState('order_id'); // Get conversation history $history = $context->getConversationHistory();}
Give your agents the gift of memory! Vizra provides persistent memory across sessions through the powerful AgentMemory model and MemoryManager service:
Session State
Short-term context stored in AgentContext during a conversation - like working memory!
Agent Memory
Long-term memory with learnings, facts, and session summaries - like permanent storage!
The agent memory API provides specialized methods for different types of information:
Copy
// Inside any agent method, access memory via $this->memory()// Learnings - Insights gained from interactions$this->memory()->addLearning('User works in healthcare', [ 'confidence' => 'high', 'source' => 'direct_mention']);// Facts - Immutable truths about the user$this->memory()->addFact('Located in New York', 1.0); // confidence score// Preferences - User likes and dislikes$this->memory()->addPreference('Detailed explanations', 'communication_style');// Summary - Overall user profile (replaces previous)$this->memory()->updateSummary('Tech-savvy healthcare professional in NYC');// Recall - Search through memories$learnings = $this->memory()->getLearnings();$facts = $this->memory()->getFacts();$preferences = $this->memory()->getPreferences('communication_style');$summary = $this->memory()->getSummary();// Context - Get formatted memory for prompts$context = $this->memory()->getContext(1500); // max tokens
For advanced use cases, you can still access the Memory Manager directly:
app/Services/AgentService.php
Copy
use Vizra\VizraADK\Services\MemoryManager;// Get or create memory for an agent$memory = $memoryManager->getOrCreateMemory('support_agent', $userId);// Add a learning$memoryManager->addLearning( 'support_agent', 'User prefers email communication', $userId);// Add a fact$memoryManager->addFact( 'support_agent', 'preferred_contact', 'email', $userId);// Update memory summary$memoryManager->updateSummary( 'support_agent', 'Customer is a premium member who prefers email contact', $userId);
Agents can manage their own memory - how cool is that?
app/Agents/SupportAgent.php
Copy
// Agents can use the MemoryTool to manage their own memoryclass SupportAgent extends BaseLlmAgent{ protected array $tools = [ MemoryTool::class, ]; protected string $instructions = '... Use the manage_memory tool to: - Add important learnings about the user - Store facts for future reference - Retrieve context from previous conversations ';}
Inject past memories into your agent’s instructions for truly personalized interactions!
Copy
// Get memory context to include in agent instructions$memoryContext = $memoryManager->getMemoryContext( 'support_agent', $userId, 1000 // max length);// Memory context includes:// - Previous Knowledge (summary)// - Key Learnings (last 5)// - Important Facts (up to 10)
Working with memory data is a breeze! Check out these handy methods:
Copy
use Vizra\VizraADK\Models\AgentMemory;// Find memory for agent and user$memory = AgentMemory::where('agent_name', 'support_agent') ->where('user_id', $userId) ->first();// Get context summary for agent instructions$contextSummary = $memory->getContextSummary(1000);// Add a learning$memory->addLearning('User works in healthcare industry');// Update memory data$memory->updateMemoryData([ 'industry' => 'healthcare', 'timezone' => 'EST']);// Record a new session$memory->recordSession();
Watch as Vizra intelligently extracts insights from conversations!
Copy
// MemoryManager automatically extracts insights from sessions$memoryManager->updateMemoryFromSession($session);// This automatically:// - Records the session in memory// - Extracts preferences ("I like/prefer...")// - Extracts personal info ("My name is...")// - Updates session count and timestamps
Keep memories fresh and relevant with intelligent summarization!
Copy
// Summarize recent sessions into memory$memoryManager->summarizeMemory( $memory, 10 // Number of recent sessions to summarize);// Get recent conversations$recentConversations = $memoryManager->getRecentConversations( 'support_agent', $userId, 5 // Limit);
Keep your database tidy with smart cleanup strategies!
Copy
// Clean up old memories$deletedCount = $memoryManager->cleanupOldMemories( 90, // Days old 1000 // Max sessions);// Clean up old sessions while preserving memory$deletedSessions = $memoryManager->cleanupOldSessions( 'support_agent', 30 // Days old);