OrkaJS
Orka.JS

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 ClassStrategic GoalInfrastructure
SummaryMemoryRecursive condensation of long threadsLLM Orchestrator
VectorMemoryRAG-based semantic retrievalEmbeddings + VectorDB
KGMemoryEntity 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 normally
await 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 summarized
const history = memory.getHistory();
// Returns: [{ role: 'system', content: '[Summary]...' }, ...recent messages]
 
// Get just the summary
const summary = memory.getSummary();
 
// Force summarization even below threshold
await memory.forceSummarize();

- SummaryMemory Config

Summary Engine Configuration

Recursive Distillation & Token Economy

llmIntelligence

BaseLLM

The backend model responsible for semantic condensation of historical data.

progressiveCompressionStrategy

boolean

Iterative updates. Appends new context to the existing summary to save tokens.

compressionRatioThreshold

number

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 messages
const results = await memory.search('outdoor activities');
// Returns messages about hiking, mountains, trails
 
// Get relevant history with surrounding context
const context = await memory.searchWithContext('What did I say about mountains?');
 
// Get messages relevant to a query
const relevant = await memory.getRelevantHistory('hiking trails');

Strategic Context Retrieval

Optimization: Cost vs. Relevance

VectorMemory 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.

RAGEmbeddingsSemantic SearchCost Reduction

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 extracted
await 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 entities
const entities = memory.getEntities();
// [{ name: 'User', type: 'PERSON' }, { name: 'Acme Corp', type: 'ORGANIZATION' }, ...]
 
// Get relations
const relations = memory.getRelations();
// [{ subject: 'User', predicate: 'works at', object: 'Acme Corp' }, ...]
 
// Query the knowledge graph
const 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 query
const messages = await memory.getContextForQuery('What programming languages?');
// Returns KG context + recent messages

KGMemory Engine Interface

Graph Extraction & Semantic Querying

getEntities()Entities

returns: Entity[]

Retrieves the taxonomy of extracted actors, places, or concepts with their associated metadata.

getRelations()Topology

returns: Relation[]

Extracts the semantic backbone (S-P-O triples) that defines how entities interact.

queryKnowledge()Inference

returns: Promise<string>

Performs a graph traversal to synthesize a factual answer based on the stored ontology.

getGraphSummary()Telemetry

returns: object

Provides a high-level audit of the graph's density, node counts, and relationship depth.

Choosing the Right Memory Type

Context ScenarioRecommended EngineCognitive Load
Ephemeral or short-burst chat (<50 msgs)Memory (Sliding Window)Minimal
Deep history with context retentionSummaryMemoryModerate
Need to retrieve facts by similarityVectorMemory (RAG)High
Complex ontological mapping (Facts/Rules)KGMemory (Graph)Expert

Tree-shaking Imports

// Import only what you need
import { 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-package
import { SummaryMemory, VectorMemory, KGMemory } from '@orka-js/memory-store';