Documentation Index Fetch the complete documentation index at: https://docs.ag-kit.dev/llms.txt
Use this file to discover all available pages before exploring further.
Short-term memory in AG-Kit manages conversation history and context within active sessions. It provides efficient storage and retrieval of recent messages, enabling agents to maintain coherent conversations with context awareness.
Overview
Short-term memory (also called session memory or conversation memory) stores:
Recent conversation messages - User and assistant exchanges
Tool call history - Records of tool invocations and results
Session state - Custom metadata and context information
Temporal context - Time-based message ordering and filtering
Memory Implementations
AG-Kit provides multiple short-term memory implementations for different use cases and deployment scenarios:
InMemoryMemory
Volatile in-memory storage ideal for development, testing, and single-instance deployments.
Features:
Fast read/write operations
Content-based similarity search
Token-aware message trimming
Multi-session support
Zero external dependencies
Use Cases:
Development and testing
Single-server applications
Temporary sessions
Prototyping
TDAIMemory
Cloud-based persistent storage with production-grade scalability through TDAI services.
Features:
Persistent cloud storage
Advanced semantic search
Distributed session management
Optional local caching
Production-ready reliability
Use Cases:
Production deployments
Multi-server applications
Long-running sessions
Enterprise applications
TypeORMMemory
Flexible ORM-based storage supporting multiple databases with customizable schema.
Features:
Multi-database support (MySQL, PostgreSQL, SQLite, etc.)
Custom entity definitions
Document conversion system
Branch and summary architecture
TypeScript type safety
Migration support
Use Cases:
Custom database schemas
Enterprise database integration
Complex data relationships
Type-safe development
MySQLMemory
Optimized MySQL implementation extending TypeORMMemory with MySQL-specific features.
Features:
MySQL-specific optimizations
Connection pooling
Transaction support
Index optimization
Performance monitoring
Use Cases:
MySQL-based applications
High-performance requirements
Enterprise MySQL deployments
MongoDBMemory
NoSQL document storage with flexible schema and horizontal scaling capabilities.
Features:
Document-based storage
Flexible schema design
Auto-connection setup - No manual client creation required
Connection string configuration
Horizontal scaling
Aggregation pipelines
GridFS support for large data
Connection pooling and options
Use Cases:
NoSQL applications
Flexible data structures
Horizontal scaling needs
Document-oriented workflows
Rapid prototyping with minimal setup
CloudBaseMemory
Tencent CloudBase integration for serverless cloud storage.
Features:
Serverless architecture
Automatic scaling
Tencent Cloud integration
Real-time synchronization
Built-in security
Use Cases:
Tencent Cloud deployments
Serverless applications
Chinese market applications
Automatic scaling needs
Quick Start
Basic Usage with InMemoryMemory
import { InMemoryMemory } from '@ag-kit/agents' ;
// Create memory instance
const memory = new InMemoryMemory ();
// Add a conversation event
await memory . add ({
message: {
id: 'msg-1' ,
role: 'user' ,
content: 'What is the weather today?' ,
timestamp: new Date ()
},
state: { userId: 'user-123' , location: 'San Francisco' }
});
// Add assistant response
await memory . add ({
message: {
id: 'msg-2' ,
role: 'assistant' ,
content: 'The weather in San Francisco is sunny, 72°F.' ,
timestamp: new Date ()
},
state: { userId: 'user-123' }
});
// Retrieve conversation history
const events = await memory . list ({ limit: 10 });
console . log ( events );
// [
// { message: { id: 'msg-1', role: 'user', content: '...' }, state: {...} },
// { message: { id: 'msg-2', role: 'assistant', content: '...' }, state: {...} }
// ]
Using TDAIMemory for Production
import { TDAIMemory } from '@ag-kit/agents' ;
// Create TDAI memory instance
const memory = new TDAIMemory ({
sessionId: 'user-session-123' ,
clientOptions: {
apiKey: process . env . TDAI_API_KEY ! ,
endpoint: 'https://api.tdai.example.com'
},
useCache: true // Enable local caching for better performance
});
// Add conversation events
await memory . add ({
message: {
id: 'msg-1' ,
role: 'user' ,
content: 'Tell me about AG-Kit' ,
timestamp: new Date ()
},
state: { source: 'web-chat' }
});
// List recent messages
const recentMessages = await memory . list ({
limit: 20 ,
order: 'desc' // Most recent first
});
Using TypeORMMemory with Custom Schema
import { DataSource } from 'typeorm' ;
import { TypeORMMemory } from '@ag-kit/agents' ;
// Create DataSource with custom configuration
const dataSource = new DataSource ({
type: 'mysql' ,
host: 'localhost' ,
port: 3306 ,
username: 'agkit_user' ,
password: 'password' ,
database: 'agkit_memory' ,
synchronize: true , // Auto-create tables in development
logging: false
});
await dataSource . initialize ();
// Create TypeORM memory instance
const memory = new TypeORMMemory ({
dataSource ,
sessionId: 'user-session-123' ,
eventTableName: 'custom_memory_events' ,
stateTableName: 'custom_memory_state' ,
summaryTableName: 'custom_memory_summaries' ,
enableContextManagement: true
});
// Use with branch and summary support
await memory . add ({
message: {
id: 'msg-1' ,
role: 'user' ,
content: 'Hello with custom schema' ,
timestamp: new Date ()
},
state: { customField: 'value' }
});
Using MongoDBMemory
import { MongoDBMemory } from '@ag-kit/agents' ;
// Recommended: Direct configuration (no manual client setup)
const memory = new MongoDBMemory ({
connectionString: 'mongodb://localhost:27017' ,
databaseName: 'agkit_memory' ,
sessionId: 'user-session-123' ,
collectionName: 'memory_events' ,
stateCollectionName: 'memory_state' ,
summaryCollectionName: 'memory_summaries' ,
enableContextManagement: true
});
// Advanced configuration with connection options
const memory2 = new MongoDBMemory ({
connectionString: 'mongodb://username:password@localhost:27017' ,
databaseName: 'agkit_memory' ,
sessionId: 'user-session-456' ,
clientOptions: {
maxPoolSize: 10 ,
serverSelectionTimeoutMS: 5000 ,
socketTimeoutMS: 45000 ,
},
enableContextManagement: true
});
// Alternative: Use existing MongoDB connection (if needed)
import { MongoClient } from 'mongodb' ;
const client = new MongoClient ( 'mongodb://localhost:27017' );
await client . connect ();
const db = client . db ( 'agkit_memory' );
const memory3 = new MongoDBMemory ({
db ,
sessionId: 'user-session-789' ,
collectionName: 'memory_events'
});
// Add events with flexible document structure
await memory . add ({
message: {
id: 'msg-1' ,
role: 'user' ,
content: 'MongoDB flexible storage' ,
timestamp: new Date (),
metadata: {
source: 'mobile-app' ,
version: '1.2.0'
}
},
state: {
userId: 'user-123' ,
preferences: {
theme: 'dark' ,
language: 'en'
}
}
});
Using CloudBaseMemory
import cloudbase from '@cloudbase/node-sdk' ;
import { CloudBaseMemory } from '@ag-kit/agents' ;
// Initialize CloudBase app
const app = cloudbase . init ({
env: 'your-env-id' ,
secretId: 'your-secret-id' ,
secretKey: 'your-secret-key'
});
// Create CloudBase memory instance
const memory = new CloudBaseMemory ({
app ,
sessionId: 'user-session-123' ,
collectionName: 'memory_events' ,
summaryCollectionName: 'memory_summaries' ,
stateCollectionName: 'memory_state' ,
enableContextManagement: true
});
// Add events with serverless storage
await memory . add ({
message: {
id: 'msg-1' ,
role: 'user' ,
content: 'Serverless memory storage' ,
timestamp: new Date ()
},
state: {
region: 'ap-shanghai' ,
deviceType: 'mobile'
}
});
// Automatic scaling and synchronization
const events = await memory . list ({ limit: 50 });
Unified Architecture
Branch and Summary System
All memory implementations now support a unified branch and summary architecture that enables:
Conversation Branching : Create alternative conversation paths for experimentation
Automatic Summarization : Compress long conversations while preserving context
Context Engineering : Intelligent token management and context window optimization (Learn more )
State Management : Persistent session state across branches and summaries
// Create a new branch from a specific event
const branchPath = await memory . createBranch ( 'experiment-1' , 'msg-10' );
// Switch to the branch (checkout)
await memory . checkout ( 'experiment-1' );
// Add messages to the current branch
await memory . add ({
message: {
id: 'msg-11-alt' ,
role: 'assistant' ,
content: 'Alternative response for testing' ,
timestamp: new Date ()
}
});
// List all branches with metadata
const branches = await memory . listBranches ();
console . log ( branches );
// [
// {
// name: 'main',
// createdAt: Date,
// fromEventId: undefined,
// isActive: false
// },
// {
// name: 'experiment-1',
// createdAt: Date,
// fromEventId: 'msg-10',
// isActive: true
// }
// ]
// Switch back to main branch
await memory . checkout ( 'main' );
Custom Entity and Document Conversion
TypeORM-based implementations support custom entity definitions and document conversion for flexible schema design:
import { Entity , Column , PrimaryGeneratedColumn } from 'typeorm' ;
import { TypeORMMemory } from '@ag-kit/agents' ;
// Define custom entity
@ Entity ( 'custom_memory_events' )
export class CustomMemoryEvent {
@ PrimaryGeneratedColumn ( 'uuid' )
id !: string ;
@ Column ()
sessionId !: string ;
@ Column ( 'json' )
message !: any ;
@ Column ( 'json' )
state !: any ;
@ Column ()
branchPath !: string ;
@ Column ({ type: 'timestamp' , default : () => 'CURRENT_TIMESTAMP' })
createdAt !: Date ;
// Custom fields
@ Column ({ nullable: true })
priority ?: number ;
@ Column ( 'json' , { nullable: true })
tags ?: string [];
@ Column ({ nullable: true })
category ?: string ;
}
// Custom document converter
class CustomDocumentConverter {
toDocument ( event : IMemoryEvent , sessionId : string , branchPath : string ) {
return {
sessionId ,
message: event . message ,
state: event . state ,
branchPath ,
createdAt: new Date (),
// Custom fields
priority: event . state ?. priority || 0 ,
tags: event . state ?. tags || [],
category: event . state ?. category || 'general'
};
}
fromDocument ( doc : any ) : IMemoryEvent {
return {
message: doc . message ,
state: {
... doc . state ,
priority: doc . priority ,
tags: doc . tags ,
category: doc . category
}
};
}
}
// Use custom entity and converter
const memory = new TypeORMMemory ({
dataSource ,
sessionId: 'user-123' ,
customEntity: CustomMemoryEvent ,
documentConverter: new CustomDocumentConverter ()
});
Core Operations
Adding Events
Add single or multiple conversation events to memory.
// Add single event
await memory . add ({
message: {
id: 'msg-1' ,
role: 'user' ,
content: 'Hello, how are you?' ,
timestamp: new Date ()
},
state: { mood: 'friendly' }
});
// Add multiple events efficiently
await memory . addList ([
{
message: { id: 'msg-1' , role: 'user' , content: 'First message' },
state: {}
},
{
message: { id: 'msg-2' , role: 'assistant' , content: 'Response' },
state: {}
}
]);
Listing Events
Retrieve events with filtering, pagination, and token limiting.
// Get all events
const allEvents = await memory . list ();
// Get recent 10 events
const recentEvents = await memory . list ({ limit: 10 });
// Get events with pagination
const page2 = await memory . list ({ limit: 10 , offset: 10 });
// Get events in descending order (newest latest)
const newestFirst = await memory . list ({ order: 'desc' });
// Limit by token count (useful for LLM context windows)
const tokenLimited = await memory . list ({ maxTokens: 2000 });
// Returns events that fit within 2000 tokens
Searching Events
Search for events based on content similarity.
// Search for events containing specific content
const results = await memory . retrieve ( 'weather forecast' );
// Returns events sorted by relevance score
// InMemoryMemory uses content-based similarity scoring
// TDAIMemory uses semantic search capabilities
Deleting Events
Remove specific events from memory.
// Delete by message ID
await memory . delete ( 'msg-1' );
// Delete by index (InMemoryMemory only)
await memory . delete ( 0 ); // Delete first event
// Note: TDAIMemory only supports deletion by ID
Clearing Memory
Remove all events from storage.
// Clear all events
await memory . clear ();
// Check if memory is empty
const isEmpty = await memory . isEmpty (); // true
// Get event count
const count = await memory . getCount (); // 0
Multi-Session Support
Both memory implementations support multiple isolated sessions.
const memory = new InMemoryMemory ();
// Add events to different sessions
await memory . add (
{ message: { id: '1' , role: 'user' , content: 'Hello' }, state: {} },
{ sessionId: 'session-A' }
);
await memory . add (
{ message: { id: '2' , role: 'user' , content: 'Hi there' }, state: {} },
{ sessionId: 'session-B' }
);
// List events from specific session
const sessionAEvents = await memory . list ({ sessionId: 'session-A' });
const sessionBEvents = await memory . list ({ sessionId: 'session-B' });
// Get all session IDs (InMemoryMemory only)
const sessionIds = memory . getSessionIds (); // ['session-A', 'session-B']
// Check if session exists (InMemoryMemory only)
const hasSession = memory . hasSession ( 'session-A' ); // true
// Clear specific session
await memory . clear ({ sessionId: 'session-A' });
Token Management
AG-Kit provides automatic token counting and trimming to manage LLM context windows.
import { InMemoryMemory , TiktokenTokenizer } from '@ag-kit/agents' ;
// Use default tokenizer (Tiktoken)
const memory = new InMemoryMemory ();
// Or provide custom tokenizer
const customTokenizer = new TiktokenTokenizer ( 'gpt-4' );
const memoryWithCustom = new InMemoryMemory ( customTokenizer );
// Retrieve events within token limit
const events = await memory . list ({ maxTokens: 4000 });
// Automatically trims oldest messages to fit within 4000 tokens
Integration with Agents
Short-term memory integrates seamlessly with AI agents for automatic context management.
Learn more: Complete Agent Integration Guide
Session Branching
Session branching enables conversation experimentation and time-travel capabilities. Create experimental conversation paths, test different responses, and rollback to previous states.
Session branching is currently supported by InMemoryMemory. Other implementations will throw an error if branching methods are called.
Creating Branches
Create experimental conversation paths without losing the original:
import { InMemoryMemory } from '@ag-kit/agents' ;
const memory = new InMemoryMemory ();
// Add some conversation history
await memory . add ({
message: { id: 'msg-1' , role: 'user' , content: 'Help me write an email' },
state: {}
});
await memory . add ({
message: { id: 'msg-2' , role: 'assistant' , content: 'I can help with that!' },
state: {}
});
// Create a branch to try a different approach
const branchId = await memory . branch ( 'polite-version' );
console . log ( `Created branch: ${ branchId } ` );
// Switch to the new branch
await memory . checkout ( 'polite-version' );
// Add experimental content
await memory . add ({
message: {
id: 'msg-3' ,
role: 'assistant' ,
content: 'I would be delighted to assist you with composing your email!'
},
state: {}
});
// List all branches
const branches = await memory . listBranches ();
console . log ( branches );
// [
// { name: 'main', createdAt: ..., eventCount: 2, isActive: false },
// { name: 'polite-version', createdAt: ..., eventCount: 3, isActive: true }
// ]
Time Travel with Event Checkout
Checkout to a specific event and delete all events after it:
// Add multiple messages
await memory . add ({ message: { id: 'msg-1' , role: 'user' , content: 'Hello' }, state: {} });
await memory . add ({ message: { id: 'msg-2' , role: 'assistant' , content: 'Hi!' }, state: {} });
await memory . add ({ message: { id: 'msg-3' , role: 'user' , content: 'How are you?' }, state: {} });
await memory . add ({ message: { id: 'msg-4' , role: 'assistant' , content: 'Good!' }, state: {} });
// Checkout to msg-2 (deletes msg-3 and msg-4)
await memory . checkout ( 'msg-2' , { type: 'event' });
const events = await memory . list ();
console . log ( events . length ); // 2 (only msg-1 and msg-2 remain)
// Auto-detection: if target is not a branch name, treated as event
await memory . checkout ( 'msg-1' ); // Automatically detected as event
Advanced Branch Operations
// A/B Testing Different Responses
await memory . createBranch ( 'friendly-agent' , 'msg-5' );
await memory . createBranch ( 'professional-agent' , 'msg-5' );
// Test friendly version
await memory . checkout ( 'friendly-agent' );
await memory . add ({
message: { id: 'resp-1' , role: 'assistant' , content: 'Hey! Sure thing!' },
state: {}
});
// Test professional version
await memory . checkout ( 'professional-agent' );
await memory . add ({
message: { id: 'resp-2' , role: 'assistant' , content: 'Certainly, I can assist.' },
state: {}
});
// Compare results and choose the better branch
const friendlyBranch = await memory . listBranches ();
await memory . checkout ( 'friendly-agent' ); // Keep this one
// Undo/Rollback to specific event
await memory . checkout ( 'msg-3' ); // Rollback to message ID
// All events after msg-3 are no longer in current path
// Conversation Checkpoints
await memory . add ( importantMessage1 );
const checkpoint1 = await memory . createBranch ( 'checkpoint-1' );
await memory . add ( importantMessage2 );
const checkpoint2 = await memory . createBranch ( 'checkpoint-2' );
// Later, return to a checkpoint
await memory . checkout ( 'checkpoint-1' );
Advanced Patterns
Context Window Management
Automatically manage LLM context windows with intelligent thresholds and token limits. For comprehensive context engineering strategies, see the Context Engineering Guide .
// Strategy 1: Automatic threshold-based management (Recommended)
const memory = new InMemoryMemory ({
enableContextManagement: true ,
thresholds: {
preRotThreshold: 8000 , // Token limit before triggering management
compactionTrigger: 0.8 , // Trigger compaction at 80% (6,400 tokens)
summarizationTrigger: 0.95 , // Trigger summarization at 95% (7,600 tokens)
recentToKeep: 10 // Always keep last N messages intact
}
});
// Dynamic threshold adjustment based on model context
const adjustThresholds = ( modelContextSize : number ) => {
const reservedForResponse = 2000 ;
const availableForMemory = modelContextSize - reservedForResponse ;
memory . updateThresholds ({
preRotThreshold: availableForMemory ,
compactionTrigger: 0.7 , // More aggressive for smaller contexts
summarizationTrigger: 0.9 ,
recentToKeep: Math . max ( 5 , Math . floor ( availableForMemory / 1000 ))
});
};
// Adjust for different models
adjustThresholds ( 32000 ); // GPT-4 Turbo
// adjustThresholds(8000); // GPT-4
// adjustThresholds(4000); // GPT-3.5
// The memory automatically manages context size
await memory . add ( event ); // Auto-triggers compaction/summarization if needed
const contextEvents = await memory . list (); // Always optimally sized
// Strategy 2: Fixed token limit
const events = await memory . list ({ maxTokens: 4000 });
// Strategy 3: Dynamic token allocation
const modelMaxTokens = 8000 ;
const reservedForResponse = 2000 ;
const availableForContext = modelMaxTokens - reservedForResponse ;
const contextEvents = await memory . list ({ maxTokens: availableForContext });
// Strategy 4: Sliding window with recent messages
const recentEvents = await memory . list ({
limit: 20 ,
order: 'desc' ,
maxTokens: 3000
});
Context Management Strategies:
Automatic Thresholds (Recommended) - Intelligent compression with configurable thresholds
Fixed Token Limits - Simple hard limits using maxTokens
Dynamic Token Allocation - Calculated limits based on model capacity
Sliding Window - Recent messages with token constraints
How Automatic Thresholds Work:
Compaction Phase : At 80% of preRotThreshold → Remove redundant/duplicate content
Summarization Phase : At 95% of preRotThreshold → Compress older messages into summaries
Preservation : Recent messages (specified by recentToKeep) are always kept in full
Complementary : Can be used together with maxTokens for additional protection
For detailed context engineering patterns and advanced threshold strategies, see the Context Engineering Documentation .
Tool Call History
Store and retrieve tool invocation history.
// Add tool call event
await memory . add ({
message: {
id: 'msg-3' ,
role: 'assistant' ,
content: 'Let me check the weather for you.' ,
toolCalls: [{
id: 'call-1' ,
type: 'function' ,
function: {
name: 'get_weather' ,
arguments: '{"location": "San Francisco"}'
}
}]
},
state: {}
});
// Add tool result event
await memory . add ({
message: {
id: 'msg-4' ,
role: 'tool' ,
content: '{"temperature": 72, "condition": "sunny"}' ,
toolCallId: 'call-1'
},
state: {}
});
Custom State Management
Store custom metadata with each event.
await memory . add ({
message: {
id: 'msg-1' ,
role: 'user' ,
content: 'Book a flight to Tokyo' ,
},
state: {
userId: 'user-123' ,
intent: 'flight_booking' ,
entities: {
destination: 'Tokyo' ,
travelClass: 'economy'
},
confidence: 0.95 ,
timestamp: new Date (). toISOString ()
}
});
// Retrieve and access custom state
const events = await memory . list ();
console . log ( events [ 0 ]. state . intent ); // 'flight_booking'
console . log ( events [ 0 ]. state . entities ); // { destination: 'Tokyo', ... }
Best Practices
1. Choose the Right Implementation
Use InMemoryMemory for development, testing, and single-instance applications
Use Others for production, distributed systems, and persistent storage needs
2. Manage Token Limits
Always consider LLM context window limits:
// Good: Limit tokens to fit model context
const events = await memory . list ({ maxTokens: 4000 });
// Bad: Loading all events without limit
const events = await memory . list (); // May exceed context window
3. Use Session IDs Consistently
Maintain session isolation for multi-user applications:
// Good: Use consistent session IDs
const sessionId = `user- ${ userId } - ${ conversationId } ` ;
await memory . add ( event , { sessionId });
// Bad: Mixing sessions
await memory . add ( event ); // Uses default session
4. Clean Up Old Sessions
Regularly clear inactive sessions to manage memory:
// Clear specific session after conversation ends
await memory . clear ({ sessionId: 'session-123' });
// Or clear all sessions periodically
await memory . clear ();
5. Handle Errors Gracefully
try {
await memory . add ( event );
} catch ( error ) {
console . error ( 'Failed to add event:' , error );
// Implement fallback or retry logic
}
InMemoryMemory
Fast : All operations are in-memory
Scalable : Handles thousands of events efficiently
Limitation : Data lost on process restart
Memory Usage : Grows with event count
TDAIMemory
Persistent : Data survives restarts
Distributed : Works across multiple servers
Caching : Optional local cache for better performance
Network : Requires network calls to TDAI service
Implementation Comparison
Feature InMemory TDAI TypeORM MySQL MongoDB CloudBase
Storage Type Volatile Cloud Database Database NoSQL Serverless Persistence ❌ ✅ ✅ ✅ ✅ ✅ Performance Fastest Fast Fast Fastest Fast Good Scalability Single High Medium High Very High Auto Custom Schema ❌ ❌ ✅ ✅ ✅ ❌ Branch Support ✅ ✅ ✅ ✅ ✅ ✅ Summary Support ✅ ✅ ✅ ✅ ✅ ✅ Multi-Database ❌ ❌ ✅ ❌ ❌ ❌ Setup Complexity None API Key Medium Medium Very Low Medium Best For Dev/Test Production Enterprise MySQL Apps NoSQL Apps Tencent Cloud
Next Steps
Long-term Memory Learn about persistent memory with vector stores
Session Management Manage multi-user sessions and context
Agent Integration Complete guide to memory-agent integration
API Reference Detailed API documentation