ai-agentsagentic-memoryepisodic-memoryvector-databasetutorial

Hierarchical Agent Memory: Cognitive Architecture Guide

Hierarchical Agent Memory Architecture: Working Memory Scratchpad, Episodic Event Stream, and Consolidated Semantic Knowledge Graph

The Problem: The Failure of Monolithic Context Windows

As developers build autonomous AI agents designed to operate over weeks, months, or years, they encounter a critical architecture trap: relying on expanding context windows as a substitute for memory.

While modern foundation models boast context windows spanning 1 million tokens, dumping raw conversation histories and tool execution logs into a monolithic prompt breaks down in production:

  • Attention Dilution and “Lost-in-the-Middle”: As context lengths balloon, neural needle-in-a-haystack retrieval degrades. The model struggles to locate subtle user preferences or critical instructions buried beneath 500,000 tokens of intermediate tool traces.
  • Runaway Latency and Cost: Passing massive context prompts on every single agent interaction introduces 5-to-15-second prefill latencies and generates exorbitant API bills.
  • Context Amnesia Across Sessions: When an agent restarts, changes tasks, or transitions between devices, flat context windows are wiped clean. The agent forgets prior user corrections, learned environmental heuristics, and historical task outcomes.

Human intelligence does not function by re-reading an exhaustive log of every sensory experience from birth. Instead, the human brain deploys a structured cognitive memory hierarchy.

The Solution: The Three-Tier Cognitive Memory Hierarchy

To grant autonomous agents durable long-term intelligence without context window bloat, modern agentic systems implement a Three-Tier Hierarchical Memory Architecture (drawing from cognitive psychology and frameworks like Synapse and Mem0).

Hierarchical Cognitive Memory Pipeline: Working Memory Buffer, Episodic Stream with Associative Decay, and Reflection Consolidation into Semantic Graphs

The Three Cognitive Tiers

  1. Working Memory (Fast Active Context): The active scratchpad loaded into the current LLM prompt. Contains the immediate task objective, current system prompt constraints, available tool schemas, and intermediate variables. Working memory is ephemeral and wiped upon task completion.
  2. Episodic Memory (The Experience Stream): An append-only chronological log of specific autobiographical experiences, tool interactions, and conversational turns. When new tasks arrive, the agent queries episodic memory using an associative triple-scoring equation that balances relevance, recency, and salience:

$$\operatorname{Score}(m) = \alpha \cdot S_{\text{relevance}}(m, q) + \beta \cdot S_{\text{recency}}(m) + \gamma \cdot S_{\text{importance}}(m)$$

  • $S_{\text{relevance}}$: Cosine similarity between the query embedding and the episodic memory embedding.
  • $S_{\text{recency}}$: Exponential temporal decay over time: $e^{-\lambda \cdot \Delta t}$, ensuring recent interactions carry higher baseline weight.
  • $S_{\text{importance}}$: An integer score (1–10) evaluated by a lightweight LLM at the moment of memory creation, designating how critical the event is to the agent’s long-term behavior.
  1. Semantic Memory (The Consolidated Belief Graph): Abstract, generalized knowledge synthesized from repeated episodic experiences through Background Memory Reflection (Dreaming). If an episodic log reveals that a user corrected date formatting five times over three weeks, the reflection worker consolidates these episodes into a permanent semantic rule: “User strictly prefers ISO 8601 YYYY-MM-DD dates across all exports.”

Comparing Memory Tiers in Autonomous Agent Systems

Feature DimensionWorking MemoryEpisodic MemorySemantic Memory
Cognitive AnalogyShort-Term RAM / ScratchpadAutobiographical DiaryLong-Term Knowledge & Beliefs
Underlying StorageIn-Context LLM PromptVector DB + SQLite LogGraph DB / Key-Value Store
LifespanSingle task execution cycleWeeks to Months (Decays over time)Permanent (Until explicitly overwritten)
Retrieval MechanismDirect context injectionAssociative Triple ScoringEntity & Concept Lookup
Update MechanismOverwritten per turnReal-time append on tool executionBackground periodic reflection loops

Implementation: Building an Episodic Memory Engine in TypeScript

Let us implement a production-ready Episodic Memory engine in TypeScript featuring exponential recency decay, importance scoring, and associative retrieval.

Step 1: Define Memory Event Schemas with Zod

Establish strict typing for episodic memory records and retrieval queries:

import { z } from 'zod'; export const EpisodicMemoryItemSchema = z.object({ id: z.string().uuid(), timestamp: z.number(), // Unix millisecond timestamp content: z.string(), importanceScore: z.number().min(1).max(10), // 1 = trivial, 10 = critical embedding: z.array(z.number()), tags: z.array(z.string()), }); export type EpisodicMemoryItem = z.infer<typeof EpisodicMemoryItemSchema>; export const ScoredMemorySchema = z.object({ memory: EpisodicMemoryItemSchema, totalScore: z.number(), relevance: z.number(), recency: z.number(), importance: z.number(), }); export type ScoredMemory = z.infer<typeof ScoredMemorySchema>;

Step 2: Implement Multi-Factor Associative Retrieval Scoring

Calculate composite associative scores combining cosine similarity, exponential time decay, and pre-computed importance:

export class EpisodicMemoryEngine { private memories: EpisodicMemoryItem[] = []; private decayConstantLambda: number; // e.g., 0.0000001 per ms (approx 7-day half-life) constructor(halfLifeDays = 7) { // Calculate exponential decay constant lambda = ln(2) / half-life in ms const halfLifeMs = halfLifeDays * 24 * 60 * 60 * 1000; this.decayConstantLambda = Math.log(2) / halfLifeMs; } public addMemory(memory: EpisodicMemoryItem): void { this.memories.push(memory); } public retrieve( queryEmbedding: number[], alpha = 0.5, // Weight for relevance beta = 0.3, // Weight for recency gamma = 0.2, // Weight for importance topK = 5, ): ScoredMemory[] { const now = Date.now(); const scored = this.memories.map((mem) => { // 1. Semantic Relevance: Cosine Similarity const relevance = this.computeCosineSimilarity(queryEmbedding, mem.embedding); // 2. Temporal Recency: Exponential Decay S = e^(-lambda * delta_t) const deltaT = Math.max(0, now - mem.timestamp); const recency = Math.exp(-this.decayConstantLambda * deltaT); // 3. Salience / Importance normalized to [0, 1] const importance = mem.importanceScore / 10.0; // Composite Associative Score const totalScore = alpha * relevance + beta * recency + gamma * importance; return { memory: mem, totalScore, relevance, recency, importance, }; }); return scored.sort((a, b) => b.totalScore - a.totalScore).slice(0, topK); } private computeCosineSimilarity(a: number[], b: number[]): number { let dot = 0; let normA = 0; let normB = 0; for (let i = 0; i < a.length; i++) { dot += a[i] * b[i]; normA += a[i] * a[i]; normB += b[i] * b[i]; } return dot / (Math.sqrt(normA) * Math.sqrt(normB) || 1); } }

Step 3: Implement the Periodic Memory Reflection Loop

Consolidate high-frequency episodic observations into durable semantic rules:

import { generateObject } from 'ai'; import { openai } from '@ai-sdk/openai'; export const ConsolidatedSemanticRuleSchema = z.object({ concept: z.string(), generalizedRule: z.string(), supportingEvidenceCount: z.number(), confidence: z.number().min(0).max(1), }); export async function runMemoryReflection( recentEpisodes: EpisodicMemoryItem[], ): Promise<z.infer<typeof ConsolidatedSemanticRuleSchema>[]> { console.log( `[Reflection Loop] Analyzing ${recentEpisodes.length} episodic records for patterns...`, ); const episodesContext = recentEpisodes .map((e, idx) => `[${idx + 1}] (${new Date(e.timestamp).toISOString()}) ${e.content}`) .join('\n'); const { object } = await generateObject({ model: openai('gpt-4o'), schema: z.object({ consolidatedRules: z.array(ConsolidatedSemanticRuleSchema), }), prompt: `You are an agentic memory consolidation daemon. Examine the following stream of recent episodic experiences. Identify recurring patterns, persistent user preferences, and generalized environment truths. Extract high-level semantic rules to be permanently stored in Semantic Memory. [Recent Episodes]: ${episodesContext}`, }); return object.consolidatedRules; }

Production Memory Compaction and Privacy Controls in 2026

Deploying cognitive agent memory across enterprise workflows requires rigorous governance:

  • Automated Memory Compaction (Pruning): Episodic memory logs cannot grow unbounded. Implement background garbage collection that purges memories whose total associative score drops below a minimal baseline threshold and that have not been referenced in 60 days.
  • Zero-Trust PII Masking at the Ingestion Layer: Before any conversational turn or tool trace enters episodic storage, pass the text through deterministic sanitizers (as outlined in our guardrails architecture) to ensure passwords, auth tokens, and sensitive PII are never persisted to long-term vector stores.
  • User-Facing Memory Ledgers: Provide users with transparent inspection dashboards where they can view, edit, or delete specific semantic memories and episodic traces, ensuring compliance with GDPR “Right to be Forgotten” mandates.

Conclusion

Hierarchical memory transforms AI agents from stateless prompt reactors into durable, evolving collaborators. By decoupling fast working memory, associative episodic event streams, and reflected semantic knowledge graphs, engineering teams build autonomous systems that learn from past actions, respect personal preferences, and operate indefinitely without context degradation.