Skip to main content
AG-Kit provides a sophisticated dual-layer memory management system that enables intelligent context management and knowledge storage for AI agents through coordinated short-term and long-term memory operations.

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
Use Cases:
  • 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
Use Cases:
  • 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

  1. Normal Operation (0-80% of threshold): Store all messages in full detail
  2. Reversible Compaction (80-95% of threshold): Compress old messages while preserving reconstruction ability
  3. 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

  1. Short-Term Retrieval: Fetch relevant context from conversation history
  2. Long-Term Recall: Semantic search for user profiles and important facts
  3. Context Integration: Combine both memory layers for comprehensive context
  4. Response Generation: LLM generates response with full context
  5. Smart Extraction: Automatically identify and store important information
  6. 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

AspectShort-Term MemoryLong-Term Memory
PurposeConversation historyPersistent knowledge
ScopeSingle sessionCross-session
StorageRecent messagesExtracted facts
RetrievalChronological/SearchSemantic search
LifespanSession durationIndefinite
Size LimitToken-basedUnlimited
Data TypeRaw messagesStructured facts
Update FrequencyEvery messageOn extraction
Primary UseContext windowPersonalization

Implementation Comparison

FeatureInMemoryTDAICloudBaseMongoDBMySQLTypeORM
Persistence
SetupNoneMediumMediumLowLowMedium
PerformanceExcellentGoodGoodGoodExcellentGood
Best ForDev/TestEnterpriseServerlessDocumentsTraditionalMulti-DB

Storage Backend Guide

Quick Overview

BackendBest ForSetupPersistence
InMemoryDevelopment, TestingZero config
TDAIEnterprise, ProductionAPI key
CloudBaseServerless, Tencent CloudCloud config
MongoDBDocument-heavy, Flexible schemaDatabase setup
MySQLTraditional apps, ACID complianceDatabase setup
TypeORMMulti-database, Type safetyORM 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

// Development
const memory = new InMemoryMemory({ sessionId: 'dev' });

// Production Cloud
const memory = new TDAIMemory({
  sessionId: 'user-123',
  clientOptions: { apiKey: process.env.TDAI_API_KEY }
});

// Self-hosted MySQL
const memory = MySQLMemory.create({
  host: 'localhost',
  database: 'agkit',
  sessionId: 'user-123'
});

Core Concepts

Session Management

Sessions provide isolated conversation contexts for different users or conversation threads.
// Create isolated sessions for different users
const aliceSession = 'user-alice-session-1';
const bobSession = 'user-bob-session-1';

// Each session has independent memory
await memory.add({sessionId: aliceSession, message, state});
await memory.add({sessionId: bobSession, message, state});
Learn more: Session Management Guide

Token Management

Automatic token counting and trimming to manage LLM context windows. For advanced strategies, see the Context Engineering Guide.
// Retrieve events within token limit
const events = await memory.list({
  maxTokens: 4000,  // Fits within GPT-4 context
  sessionId: 'session-123'
});
Learn more: Short-term Memory Guide

Memory Strategies

Organize long-term memories by strategy for better categorization.
// Different memory strategies
await longTermMemory.record({
  strategy: 'facts',        // Objective information
  content: 'User graduated from MIT'
});

await longTermMemory.record({
  strategy: 'preferences',  // User likes/dislikes
  content: 'User prefers dark mode'
});
Learn more: Long-term Memory Guide Find relevant memories using natural language queries.
// Search across all memories
const results = await longTermMemory.semanticSearch(
  'What are the user\'s food preferences?'
);
// Returns: "User loves Italian food", "User is allergic to shellfish", etc.
Learn more: Long-term Memory Guide

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.

Next Steps