Advanced Memory Types
Advanced storage with Summary, Vector, and KG Memory for long-form context andsemantic intelligence.
Overview
OrkaJS provides three advanced memory types for handling long conversations more intelligently than simple sliding window:
| Memory Class | Strategic Goal | Infrastructure |
|---|---|---|
| SummaryMemory | Recursive condensation of long threads | LLM Orchestrator |
| VectorMemory | RAG-based semantic retrieval | Embeddings + VectorDB |
| KGMemory | Entity relationship mapping (Ontology) | LLM + GraphDB |
# SummaryMemory
SummaryMemory automatically compresses old messages into a summary using an LLM. This preserves context while reducing token usage for very long conversations.
import { SummaryMemory } from '@orka-js/memory-store';import { OpenAIAdapter } from '@orka-js/openai'; const llm = new OpenAIAdapter({ apiKey: process.env.OPENAI_API_KEY! }); const memory = new SummaryMemory({ llm: { generate: (prompt) => llm.generate(prompt) }, maxMessages: 20, // Keep at most 20 recent messages summaryThreshold: 10, // Summarize when 10+ messages overflow summaryMaxLength: 1000, // Max summary length in characters progressiveCompression: true, // Update summary incrementally}); // Add messages normallyawait memory.addMessage({ role: 'user', content: 'My name is Alice.' });await memory.addMessage({ role: 'assistant', content: 'Hello Alice!' });// ... more messages ... // When messages exceed maxMessages, old ones are summarizedconst history = memory.getHistory();// Returns: [{ role: 'system', content: '[Summary]...' }, ...recent messages] // Get just the summaryconst summary = memory.getSummary(); // Force summarization even below thresholdawait memory.forceSummarize();- SummaryMemory Config
Summary Engine Configuration
Recursive Distillation & Token Economy
llmIntelligenceBaseLLM
The backend model responsible for semantic condensation of historical data.
progressiveCompressionStrategyboolean
Iterative updates. Appends new context to the existing summary to save tokens.
compressionRatioThresholdnumber
Target reduction scale. Defines what percentage of the buffer is condensed.
# VectorMemory
VectorMemory stores conversation history as embeddings in a vector database, enabling semantic search to retrieve relevant past messages based on the current query.
import { VectorMemory } from '@orka-js/memory-store';import { OpenAIAdapter } from '@orka-js/openai';import { MemoryVectorAdapter } from '@orka-js/memory'; const openai = new OpenAIAdapter({ apiKey: process.env.OPENAI_API_KEY! });const vectorDB = new MemoryVectorAdapter(); const memory = new VectorMemory({ embeddings: { embed: (text) => openai.embed(text), embedBatch: (texts) => openai.embedBatch(texts), }, vectorDB: { add: (vectors, docs, meta) => vectorDB.add(vectors, docs, meta), search: (vector, topK) => vectorDB.search(vector, topK), delete: (ids) => vectorDB.delete(ids), }, maxMessages: 100, searchTopK: 5, similarityThreshold: 0.7,}); // Add messages (automatically embedded and stored)await memory.addMessage({ role: 'user', content: 'I love hiking in the mountains.' });await memory.addMessage({ role: 'assistant', content: 'That sounds great! Do you have a favorite trail?' });await memory.addMessage({ role: 'user', content: 'Yes, I often go to Mount Rainier.' }); // Later in the conversation...// Search for relevant past messagesconst results = await memory.search('outdoor activities');// Returns messages about hiking, mountains, trails // Get relevant history with surrounding contextconst context = await memory.searchWithContext('What did I say about mountains?'); // Get messages relevant to a queryconst relevant = await memory.getRelevantHistory('hiking trails');Strategic Context Retrieval
Optimization: Cost vs. RelevanceVectorMemory bridges the gap between massive historical data and limited context windows. It enables long-term recall without the linear cost of processing entire chat histories.
The Vector Advantage
- Infinite History
- Semantic Relevance
- Token Efficiency
# KGMemory (Knowledge Graph)
KGMemory extracts entities and relationships from conversations to build a knowledge graph. This enables structured queries about what was discussed.
import { KGMemory } from '@orka-js/memory-store';import { OpenAIAdapter } from '@orka-js/openai'; const llm = new OpenAIAdapter({ apiKey: process.env.OPENAI_API_KEY! }); const memory = new KGMemory({ llm: { generate: (prompt) => llm.generate(prompt) }, maxMessages: 100, maxTriples: 500, extractionBatchSize: 5, // Extract after every 5 messages}); // Add messages - entities and relations are automatically extractedawait memory.addMessage({ role: 'user', content: 'I work at Acme Corp as a software engineer.' });await memory.addMessage({ role: 'assistant', content: 'Nice! What technologies do you use at Acme?' });await memory.addMessage({ role: 'user', content: 'We mainly use TypeScript and Python.' }); // Get extracted entitiesconst entities = memory.getEntities();// [{ name: 'User', type: 'PERSON' }, { name: 'Acme Corp', type: 'ORGANIZATION' }, ...] // Get relationsconst relations = memory.getRelations();// [{ subject: 'User', predicate: 'works at', object: 'Acme Corp' }, ...] // Query the knowledge graphconst context = await memory.queryKnowledge('Where does the user work?');// Returns: "The user works at Acme Corp as a software engineer." // Get context for a new queryconst messages = await memory.getContextForQuery('What programming languages?');// Returns KG context + recent messagesKGMemory Engine Interface
Graph Extraction & Semantic Querying
getEntities()Entitiesreturns: Entity[]
Retrieves the taxonomy of extracted actors, places, or concepts with their associated metadata.
getRelations()Topologyreturns: Relation[]
Extracts the semantic backbone (S-P-O triples) that defines how entities interact.
queryKnowledge()Inferencereturns: Promise<string>
Performs a graph traversal to synthesize a factual answer based on the stored ontology.
getGraphSummary()Telemetryreturns: object
Provides a high-level audit of the graph's density, node counts, and relationship depth.
Choosing the Right Memory Type
| Context Scenario | Recommended Engine | Cognitive Load |
|---|---|---|
| Ephemeral or short-burst chat (<50 msgs) | Memory (Sliding Window) | Minimal |
| Deep history with context retention | SummaryMemory | Moderate |
| Need to retrieve facts by similarity | VectorMemory (RAG) | High |
| Complex ontological mapping (Facts/Rules) | KGMemory (Graph) | Expert |
Tree-shaking Imports
// Import only what you needimport { SummaryMemory } from '@orka-js/memory-store';import { VectorMemory } from '@orka-js/memory-store';import { KGMemory } from '@orka-js/memory-store'; // Or import from orkajs meta-packageimport { SummaryMemory, VectorMemory, KGMemory } from '@orka-js/memory-store';