Engineering

How We Built a Memory System That Thinks Like a Brain

Alexander Bering
Alexander Bering
March 29, 2026 · 8 min read

Updated on 31 October 2026. Test counts of the open packages brought to v0.6.1.

Added on 11 October 2026. This text tells how memory was built in ZenAI, the production system ZenBrain came out of. What the open library itself does today is set out in the README table What runs on its own, and what you call yourself. Corrected: the claim that no competitor has sleep consolidation, an unmeasured 30 % improvement, a sentence on internal testing that contradicts our correction notice, and outdated package figures.

The Starting Point: A Vector Store

Like every AI project in 2024, we started with a simple pattern: embed text, store vectors, retrieve by similarity. It took about two weeks to have a working prototype.

And about three weeks to realize it wasn't enough.

The problem wasn't retrieval accuracy. Cosine similarity works surprisingly well for finding relevant text. The problem was everything else:

  • The system couldn't forget. Irrelevant notes from months ago polluted every query.
  • It had no sense of time. A fact learned yesterday had the same weight as one from last year.
  • It couldn't connect ideas. Two related concepts stored separately stayed separate forever.
  • It didn't get better with use. The more you stored, the noisier it got.

That is what happened to our first prototype: it grew in storage and degraded in quality. More data didn't make it smarter. It made it louder.

The Neuroscience Detour

I spent two months reading neuroscience papers instead of writing code. It was the best investment of the entire project.

The human brain doesn't have one memory system. It has at least five, depending on how you count. Each one serves a different purpose, operates on different timescales, and fails in different ways. The interplay between them is what creates intelligent memory.

Three papers changed everything:

Stickgold & Walker (2013) showed that memory consolidation during sleep is critical for learning. The brain doesn't just store memories while you sleep — it actively replays them, strengthens important connections, and prunes weak ones. This is why you sometimes wake up with a solution to a problem you were stuck on.

Ebbinghaus (1885) quantified forgetting. The forgetting curve is exponential, but each review resets and flattens it. This became the foundation for our decay model.

Hebb (1949) proposed that neurons that fire together wire together. This simple principle — co-activation strengthens connections — became our knowledge graph model.

Building Layer by Layer

We didn't build all seven layers at once. We started with three (Working, Episodic, Long-Term) and added the others as we understood the problems they solved.

The Working Memory Problem

Working memory is the easiest to conceptualize and the hardest to implement correctly. In humans, working memory holds approximately 7 items and constantly refreshes. In an AI system, the question is: what counts as an "item"?

Our solution in ZenAI: working memory was session-scoped, capacity-limited, and priority-ordered. When the system was processing a query, working memory held the most relevant recent context. When you switched topics, it cleared and reloaded. This prevented the "topic contamination" problem where context from one conversation bleeds into another.

The Episodic vs. Semantic Split

This was Tulving's key insight: "I had pasta for lunch" is fundamentally different from "pasta is an Italian food." The first is a personal experience tied to time and place. The second is abstract knowledge.

We implemented this distinction. When you store "we decided to use PostgreSQL in yesterday's meeting" through ZenBrain's MemoryCoordinator, it becomes an episode with its time, context and emotional weight if that weight is above 0.5, otherwise a fact. Each call to consolidate() turns each emotionally weighted episode among the 100 most recent into a semantic fact, once; in the human brain, repeated episodes crystallize into general knowledge in a similar way.

The Forgetting Breakthrough

Counter-intuitively, the clearest difference we perceived came from implementing forgetting.

Before active forgetting, every retrieval query had to wade through all stored memories. Signal-to-noise ratio got worse as storage grew. After we implemented Ebbinghaus decay in ZenAI, memories that hadn't been accessed faded. Not deleted, just deprioritized; accessed again, they strengthened. In the open-source packages, Ebbinghaus decay is a function in @zensation/algorithms; in the MemoryCoordinator only working memory decays, and recall does not rank by retrievability.

We perceived the effect as clear; we never measured or published it. In the paper's NoDecay ablation, forgetting costs hardly any retrieval precision (ΔP@5 = 0.002 in favour of the variant without decay).

Sleep Consolidation in ZenAI

This is the feature I'm most proud of. Idle-time consolidation is pursued elsewhere too, for example as “dreaming” in Letta Code.

In ZenAI, during idle periods (when the system wasn't processing queries), the Sleep Compute Engine activated:

  1. Selection: Identifies recent memories with high importance that haven't been consolidated
  2. Replay: Re-evaluates each memory in the context of everything else the system knows
  3. Strengthening: Uses Hebbian dynamics to reinforce connections between related memories
  4. Pruning: Weakens connections that haven't been activated, eventually removing them

The aim: a memory system that improves over time, not because it stores more but because it organizes better. So far that is shown only in the paper's simulation; the only run of the sleep loop on real data in the paper's result inventory showed a significant loss in retrieval precision (correction notice of 18 September 2026). In the open-source packages, replay and pruning are functions in @zensation/algorithms that you call yourself; consolidate() in the MemoryCoordinator does not call them.

The RAG Pipeline

Memory is only half the equation. The other half is retrieval — how you find the right memory at the right time.

The RAG pipeline of ZenAI, the predecessor of Eigenraum, evolved through 145 phases; it is not part of the open-source packages. It included:

  • HyDE (Hypothetical Document Embeddings): Instead of searching for the query directly, we generate a hypothetical answer and search for that. This bridges the vocabulary gap between questions and stored knowledge.
  • Cross-Encoder Re-ranking: After initial retrieval, a cross-encoder model re-ranks results for semantic relevance.
  • Self-RAG Critique: If confidence is below 0.5, the system automatically reformulates the query and tries again.
  • Contextual Retrieval: Using the method described in Anthropic's research, which they report reduces retrieval failures by up to 67%.
  • Confidence Scoring: A 4-component score (topScore, avgScore, variance, diversity) that tells you how reliable the retrieved context is.

The Numbers

After 145 development phases:

  • 11,589+ tests as of April 2026 (backend + frontend + CLI, 0 failures)
  • 539 tests for the algorithm package alone, 948 across all open-source packages (both as of v0.6.1)
  • 20 algorithm modules (10 core + 10 advanced) in the open-source core
  • 7 memory layers; recall() searches up to five of them at once
  • 60 AI tools across 14 categories (in ZenAI)
  • 4 context schemas with complete isolation (in ZenAI)
  • Uncertainty bands for FSRS-style retrievability (heuristic, not calibrated intervals), as a function in @zensation/algorithms (getRetrievabilityWithCI)

What We Got Wrong

Not everything worked on the first try.

Over-engineering episodic memory: Our first implementation stored too much context per episode. Processing time was slow and storage bloated. We learned to capture the essential elements (what, when, who, outcome) and let semantic memory handle the rest.

Consolidation frequency: Initially, we ran consolidation every hour. This was too aggressive — memories didn't have time to accumulate access patterns. In ZenAI we settled on daily consolidation with event-triggered micro-consolidations for high-importance items; in @zensation/core, consolidate() runs whenever your application calls it.

Knowledge graph normalization: Without normalization, frequently co-activated concepts would dominate the graph. A single topic could end up connected to everything, making the graph useless. Hebbian normalization with a logarithmic scale fixed this.

Open Source

The algorithms are published as @zensation/algorithms and @zensation/core on npm. Zero external dependencies, TypeScript-native, Apache 2.0 licensed.

npm install @zensation/algorithms @zensation/core

GitHub: github.com/zensation-ai/zenbrain

We believe that memory is too fundamental to be proprietary. If you're building AI applications, you shouldn't have to reinvent forgetting curves and spaced repetition from scratch.

What's Next

Already shipped: retention curves as generateRetentionCurve in @zensation/algorithms/visualization, and cross-context merge detection as layer 7 of @zensation/core (a building block, not yet part of recall).

We're working on:

  • Adaptive consolidation — adjust consolidation frequency based on usage patterns
  • Multi-agent shared memory — let specialized AI agents share a common knowledge base

The gap between AI memory and human memory is still enormous. But it's shrinking.

If you're working on AI memory, we'd love to hear from you. Find us on GitHub, Twitter, or GitHub Discussions.