Memory System Architecture
AG-Kit’s memory system consists of two complementary layers that work together to provide comprehensive memory capabilities:Short-Term Memory (Session Memory)
Stores conversation events and messages with efficient storage, retrieval, and search capabilities. Functions like traditional storage systems with precise querying and filtering based on various conditions. Key Characteristics:- Volatile or Persistent - Choose between in-memory or cloud storage
- Session-based - Isolated contexts for different conversations
- Token-aware - Automatic management of LLM context windows
- Context Engineering - Built-in compaction and summarization for long conversations (Learn more)
- Fast Access - Optimized for recent conversation history
- Maintaining conversation context within a session
- Managing multi-turn dialogues
- Tracking tool call history
- Handling concurrent user conversations
Long-Term Memory (Knowledge Memory)
Intelligently extracts important information from conversations to store user profiles, key facts, and preferences. Supports semantic similarity-based recall using vectorization technology for efficient similarity search. Key Characteristics:- Persistent - Information survives across sessions
- Semantic Search - Vector-based similarity matching
- Intelligent Extraction - Automatic memory extraction from conversations
- Knowledge Consolidation - Deduplication and merging of related information
- Remembering user preferences and facts
- Building user profiles over time
- Storing domain knowledge
- Personalizing agent responses
Context Engineering
AG-Kit includes built-in context engineering to solve the context degradation problem in long-running conversations. As conversations grow, they face challenges like token limits, attention dilution, and performance degradation.Three-Tier Management Strategy
- Normal Operation (0-80% of threshold): Store all messages in full detail
- Reversible Compaction (80-95% of threshold): Compress old messages while preserving reconstruction ability
- Structured Summarization (95%+ of threshold): Create structured summaries for dramatic token reduction
Key Benefits
- Unlimited Conversation Length: No practical limits on conversation duration
- Maintained Quality: Performance remains high even in long conversations
- Information Preservation: Critical context preserved through intelligent management
- Cost Efficiency: Dramatic reduction in token usage and associated costs
Context engineering happens automatically - no manual intervention required. The system monitors token usage and applies the appropriate strategy based on configurable thresholds.
Memory Workflow
Process Flow
- Short-Term Retrieval: Fetch relevant context from conversation history
- Long-Term Recall: Semantic search for user profiles and important facts
- Context Integration: Combine both memory layers for comprehensive context
- Response Generation: LLM generates response with full context
- Smart Extraction: Automatically identify and store important information
- Continuous Learning: Continuously accumulate and optimize user knowledge
Memory Implementations
Short-Term Memory Implementations
InMemoryMemory
In-memory storage for development and testing
- Fast read/write operations
- Multi-session support
- Content-based search
- Zero external dependencies
TDAIMemory
Cloud-based storage for production
- Persistent storage
- Distributed sessions
- Advanced search
- Enterprise reliability
CloudBaseMemory
Tencent CloudBase cloud database
- Serverless NoSQL storage
- Real-time synchronization
- Built-in authentication
- Auto-scaling capabilities
MongoDBMemory
MongoDB document database
- Flexible document storage
- Rich query capabilities
- Horizontal scaling
- ACID transactions
MySQLMemory
MySQL relational database
- ACID compliance
- Mature ecosystem
- High performance
- Enterprise features
TypeORMMemory
TypeORM multi-database support
- Database agnostic
- Type-safe queries
- Migration support
- Multiple DB backends
Long-Term Memory Implementations
Mem0LongTermMemory
Mem0 SDK integration with AI-powered features
- Automatic extraction
- Semantic search
- Graph relationships
- Intelligent consolidation
TDAILongTermMemory
TDAI cloud storage for enterprise
- Cloud persistence
- Strategy-based organization
- Scalable infrastructure
- Enterprise features
Memory Comparison
Short-Term vs Long-Term Memory
| Aspect | Short-Term Memory | Long-Term Memory |
|---|---|---|
| Purpose | Conversation history | Persistent knowledge |
| Scope | Single session | Cross-session |
| Storage | Recent messages | Extracted facts |
| Retrieval | Chronological/Search | Semantic search |
| Lifespan | Session duration | Indefinite |
| Size Limit | Token-based | Unlimited |
| Data Type | Raw messages | Structured facts |
| Update Frequency | Every message | On extraction |
| Primary Use | Context window | Personalization |
Implementation Comparison
| Feature | InMemory | TDAI | CloudBase | MongoDB | MySQL | TypeORM |
|---|---|---|---|---|---|---|
| Persistence | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Setup | None | Medium | Medium | Low | Low | Medium |
| Performance | Excellent | Good | Good | Good | Excellent | Good |
| Best For | Dev/Test | Enterprise | Serverless | Documents | Traditional | Multi-DB |
Storage Backend Guide
Quick Overview
| Backend | Best For | Setup | Persistence |
|---|---|---|---|
| InMemory | Development, Testing | Zero config | ❌ |
| TDAI | Enterprise, Production | API key | ✅ |
| CloudBase | Serverless, Tencent Cloud | Cloud config | ✅ |
| MongoDB | Document-heavy, Flexible schema | Database setup | ✅ |
| MySQL | Traditional apps, ACID compliance | Database setup | ✅ |
| TypeORM | Multi-database, Type safety | ORM config | ✅ |
Choosing the Right Backend
By Use Case
- Development/Testing: InMemoryMemory
- Enterprise Production: TDAIMemory, MySQLMemory
- Serverless Apps: CloudBaseMemory, TDAIMemory
- Document Storage: MongoDBMemory, CloudBaseMemory
- Multi-Database: TypeORMMemory
Quick Start Examples
Core Concepts
Session Management
Sessions provide isolated conversation contexts for different users or conversation threads.Token Management
Automatic token counting and trimming to manage LLM context windows. For advanced strategies, see the Context Engineering Guide.Memory Strategies
Organize long-term memories by strategy for better categorization.Semantic Search
Find relevant memories using natural language queries.Getting Started
1
Choose Storage Backend
Pick based on your needs: InMemory (dev), TDAI/CloudBase (cloud), or MySQL/MongoDB (self-hosted).
2
Initialize Memory Layers
Set up short-term memory for conversations and/or long-term memory for knowledge.
3
Integrate with Agent
Attach memory instances to your agent configuration.
4
Implement Session Management
Create session IDs and manage session lifecycle.
5
Test and Optimize
Monitor performance and adjust token limits, caching, and cleanup strategies.