Use this file to discover all available pages before exploring further.
Memory gives agents the ability to retain and recall information across sessions, enabling personalized interactions, context continuity, and intelligent decision-making based on historical data.
The Memory primitive empowers agents with persistent storage and retrieval of information across multiple sessions and interactions. Unlike session-based state that exists only within a single conversation, memory provides long-term retention of facts, preferences, conversation history, and learned patterns.Memory is essential for:
Personalization: Remember user preferences, habits, and historical interactions
Context Continuity: Maintain conversation context across multiple sessions
Knowledge Accumulation: Build up domain knowledge over time
Relationship Building: Create more natural, personalized user experiences
Efficient Workflows: Avoid asking users to repeat information
Pattern Recognition: Learn from past interactions to improve future responses
Automatic Storage
Store important information automatically or explicitly during agent execution
Semantic Retrieval
Query memories using natural language with vector-based semantic search
Scoped Memory
Organize memories by user, session, or custom namespaces for multi-tenant applications
Temporal Awareness
Memories include timestamps for time-based retrieval and context
Storage: Agent stores important facts, preferences, and context during execution
Indexing: Memories are indexed using vector embeddings for semantic search
Retrieval: Agent automatically recalls relevant memories based on current context
Integration: Retrieved memories are injected into agent context for informed responses
Updates: Memories can be updated, deleted, or marked as outdated over time
Scoping: Memories are isolated by namespace (user ID, workspace, etc.)
Privacy & Control: Memories are scoped per user/namespace and can be deleted at any time. Agentbase provides full GDPR compliance for memory management.
// User preferences and facts{ type: "user", namespace: "user_12345", memories: [ "User prefers concise responses", "User's timezone is PST", "User works in healthcare industry", "User last ordered Product X on 2024-01-15" ]}
// Conversation context{ type: "conversation", namespace: "session_abc123", memories: [ "User asked about pricing on 2024-01-10", "User mentioned a bug in the mobile app", "Promised to follow up by end of week" ]}
import { Agentbase } from '@agentbase/sdk';const agentbase = new Agentbase({ apiKey: process.env.AGENTBASE_API_KEY});// Enable memory for a userconst result = await agentbase.runAgent({ message: "Remember that I prefer morning meetings and I'm allergic to shellfish", memory: { namespace: "user_12345", enabled: true }});// Agent automatically stores this information// Future conversations will recall these preferences
// Agent automatically retrieves relevant memoriesconst result = await agentbase.runAgent({ message: "Schedule a meeting with the team", memory: { namespace: "user_12345", enabled: true }});// Agent recalls "I prefer morning meetings" and suggests morning times
Create support agents that remember customer history:
const support = await agentbase.runAgent({ message: "I'm having issues with my account", memory: { namespace: `customer_${customerId}`, enabled: true }, system: `You are a customer support agent. Use memory to: - Recall previous support tickets and resolutions - Remember customer preferences and communication style - Track ongoing issues and promises made - Personalize responses based on customer history`});// Agent automatically recalls:// - "Customer prefers email over phone"// - "Had billing issue resolved on 2024-01-10"// - "Premium tier customer since 2023"
Build assistants that remember user preferences and context:
const assistant = await agentbase.runAgent({ message: "Schedule meetings for next week", memory: { namespace: `user_${userId}`, enabled: true }, system: `You are a personal executive assistant. Remember and use: - User's calendar preferences (morning meetings, lunch blocks) - Recurring commitments and constraints - Preferred meeting locations - Communication preferences with different contacts - Travel schedules and time zones`});// Agent recalls:// - "User blocks 12-1pm for lunch"// - "Prefers video calls for external meetings"// - "Traveling to NYC Jan 15-17"
const coach = await agentbase.runAgent({ message: "How did my workout go this week?", memory: { namespace: `user_${userId}`, enabled: true }, system: `You are a health and wellness coach. Track: - Fitness goals and milestones - Workout history and progress - Dietary preferences and restrictions - Sleep patterns - Energy levels and mood`});// Agent recalls:// - "Goal: Run 5K by March"// - "Completed 3 workouts this week"// - "Vegetarian diet"// - "Best workout time is morning"
Be Specific: Store concrete, actionable information rather than vague generalizations. “User prefers meetings at 10am” is more useful than “User likes mornings.”
Store Facts, Not Conversations
// Good: Extract and store factsawait agentbase.storeMemory({ namespace: "user_123", memories: [ { content: "User allergic to peanuts" }, { content: "User's birthday is March 15" }, { content: "User prefers dark mode in UI" } ]});// Avoid: Storing full conversation textawait agentbase.storeMemory({ namespace: "user_123", memories: [ { content: "User: I'm allergic to peanuts. Agent: I'll remember that." } ]});
PII Handling: Be mindful of storing personally identifiable information (PII). Implement proper data retention policies and provide users with memory deletion capabilities.
// Implement user data deletionasync function deleteUserData(userId: string) { // Delete all user memories for GDPR compliance await agentbase.deleteMemories({ namespace: `user_${userId}`, deleteAll: true }); console.log(`All data deleted for user ${userId}`);}// Provide users with memory viewingasync function getUserMemories(userId: string) { const memories = await agentbase.queryMemories({ namespace: `user_${userId}`, query: "*", // Get all memories limit: 1000 }); return memories;}// Allow users to delete specific memoriesasync function deleteSpecificMemory(userId: string, memoryId: string) { await agentbase.deleteMemories({ namespace: `user_${userId}`, memoryIds: [memoryId] });}
// Don't retrieve too many memories at onceconst memories = await agentbase.queryMemories({ namespace: "user_123", query: "user preferences", limit: 10 // Top 10 most relevant});// Too many memories can dilute context and increase costs
Use Specific Queries
// Good: Specific queryconst foodPrefs = await agentbase.queryMemories({ namespace: "user_123", query: "dietary restrictions and food allergies"});// Avoid: Overly broad queryconst everything = await agentbase.queryMemories({ namespace: "user_123", query: "everything about the user"});
Filter by Category
// Use metadata to filter memoriesconst preferences = await agentbase.queryMemories({ namespace: "user_123", query: "user preferences", filter: { category: "preferences" }});
const result = await agentbase.runAgent({ message: "Plan my week", memory: { namespace: `user_${userId}`, enabled: true }, system: `You are a personal assistant. ALWAYS recall from memory: - User's work schedule and commitments - Personal preferences for meeting times - Recurring tasks and habits - Goals and priorities Store in memory: - New commitments mentioned - Updated preferences - Completed tasks and milestones`});
Remember: Memory is most powerful when it stores specific, actionable facts rather than general information. Focus on what will make future interactions more personalized and efficient.