Engineering

Sleep Consolidation: Teaching AI to Dream

Alexander Bering
Alexander Bering
March 31, 2026 · 10 min read

Corrected on 3 November 2026. The Fiedler row is no evidence of better connectivity: per version 9 of the paper, its rise of at least 30 % is fixed by construction.

Corrected on 11 October 2026. Sleep consolidation in ZenBrain is a set of functions in @zensation/algorithms; the MemoryCoordinator does not call them. Also corrected: the sentence about other frameworks (Letta Code consolidates at idle as "dreaming") and "solves all three problems": on real data, the only run showed a loss in retrieval precision.

The Problem: Memory Without Maintenance

Store everything. Forget nothing. Retrieve by similarity.

That is how many AI memories work in 2026. Some add recency weighting, some add graph structure, some overwrite contradicting facts. Idle-time consolidation is pursued elsewhere too, for example as “dreaming” in Letta Code. The question of this article is what it means to maintain memories the way a brain does.

Human memory does not work like a database. Every night, while you sleep, your brain runs a sophisticated maintenance process. Neuroscientists call it hippocampal replay — the brain literally replays the day's experiences, strengthening important memories and pruning weak connections.

We built this for AI.

What Sleep Consolidation Does in the Brain

In 2013, Stickgold and Walker published a landmark paper in Nature Neuroscience describing sleep-dependent memory triage. The key insight: sleep is not passive rest. It is active memory maintenance.

During sleep, the hippocampus replays recent experiences. But not all experiences equally:

  • Emotionally significant events get priority replay — your brain knows that the argument with your colleague matters more than what you had for lunch
  • Frequently accessed memories are reinforced — knowledge you use regularly gets strengthened
  • Weak connections between ideas are pruned (synaptic downscaling) — half-formed associations that never solidified are removed
  • Episodic memories are gradually consolidated into lasting semantic knowledge — "I learned X at the conference on Tuesday" becomes simply "X is true"

Tononi and Cirelli's Synaptic Homeostasis Hypothesis (2006) adds a crucial piece: during waking hours, synaptic connections accumulate as we learn. During sleep, the brain performs net downscaling — reducing overall connection strength while preserving the relative differences. Without this, the neural network would eventually saturate. Sleep is the brain's garbage collector.

Why This Matters for AI

Consider an AI assistant that has been running for six months. It has accumulated thousands of memories: user preferences, project details, meeting notes, research findings, casual conversations. Without maintenance:

  • Retrieval degrades. The vector store fills with near-duplicates and outdated information. Search quality drops as noise increases.
  • Contradictions accumulate. The user's preference changed three months ago, but both the old and new preference exist with equal weight.
  • Storage grows unbounded. Every interaction adds more data, but nothing is ever removed or consolidated.

Sleep consolidation targets all three problems. In the paper's simulation it helped; the only run on real data showed a loss in retrieval precision instead (more on that below).

Our Implementation

In ZenBrain, sleep consolidation ships as functions in @zensation/algorithms that you call yourself, for example from a background job during idle time. consolidate() in the MemoryCoordinator does not call them; it turns emotionally weighted episodes into facts, once each. The functions implement three core mechanisms from the neuroscience literature:

1. Selective Replay (selectForReplay)

Not all memories deserve replay. We score each memory using four weighted factors:

priority = normalizedAccessCount * 0.3 + emotionalWeight * 0.3 + recency * 0.2 + instability * 0.2

Recency uses exponential decay: exp(-daysSinceAccess * 0.1). This means a memory from yesterday has recency ~0.9, while one from a month ago drops to ~0.05. The most recent, emotionally charged, frequently accessed, and unstable memories get replayed first. Maximum 20 memories per cycle to bound compute time.

The weighting draws on neuroscience findings on the role of emotional significance in sleep (Cairney et al., 2014).

2. Stability Boosting (simulateReplay)

Selected memories receive a stability multiplier of 1.5 (a 50% boost). Emotionally significant memories (emotional weight > 0.5) receive an additional 20% bonus via EMOTIONAL_BONUS = 1.2. This means that after replay:

  • A neutral memory with stability 10 days becomes 15 days
  • An emotional memory with stability 10 days becomes 18 days
  • A highly emotional memory (weight 0.9) still gets the same 18 days — the bonus is binary, not graded

The maximum stability cap is 365 days — no memory is permanently immune to decay. Connected edges in the knowledge graph are also strengthened by a factor of 1.1, reinforcing the associative network around replayed memories.

3. Pruning Weak Connections (pruneWeakConnections)

Connections in the knowledge graph with Hebbian weight below 0.2 are pruned. This is a threshold rule modelled on Tononi and Cirelli's synaptic homeostasis: weak, unreinforced connections are sorted out so that the graph does not fill up with noise.

The function returns both kept and pruned arrays, making the process fully auditable. You can inspect exactly which connections were removed and why.

The result: the function separates well-reinforced connections from weak ones; whether the weak ones are deleted is up to your application. ZenBrain's MemoryCoordinator deletes nothing from long-term memory on its own.

The Full Consolidation Cycle

In production (ZenAI), the sleep consolidation engine ran as a BullMQ worker with these steps:

  1. Check idle status — only runs when the system has been idle
  2. Acquire distributed lock — Redis-based, prevents parallel execution on multiple instances
  3. Select memories for replay — priority scoring across all episodic memories
  4. Simulate replay — stability boosting + edge strengthening
  5. Prune weak connections — Hebbian weight threshold
  6. Promote stable episodics — move to semantic long-term storage
  7. Detect contradictions — flag conflicting memories for resolution
  8. Queue Hebbian decay + Bayesian propagation — cascade effects through the knowledge graph

How It Differs from Letta's Sleep-Time Compute

Letta (formerly MemGPT) introduced "sleep-time compute" in 2025 — a dual-agent architecture where a background LLM agent reorganizes memory blocks during idle time. In Letta's experiments, sleep-time agents reached comparable accuracy with fewer tokens.

Our approach is fundamentally different:

AspectLetta Sleep-Time ComputeZenBrain Sleep Consolidation
MechanismLLM rewrites memory blocksAlgorithmic replay simulation
Based onEngineering optimizationStickgold & Walker (2013)
DeterministicNo (LLM outputs vary)Yes (same inputs = same outputs)
CostLLM inference per cyclePure computation (no LLM calls)
PruningLLM decides what to keepThreshold-based (Hebbian weight < 0.2)
AuditableDifficult (LLM reasoning)Fully (kept/pruned arrays)

Neither approach is universally better. LLM-driven reorganization can reason about content semantics — it can understand why two memories contradict and rewrite them intelligently. Algorithmic consolidation is deterministic, reproducible, free of inference costs, and fully auditable. We chose the neuroscience-modeled approach because we believe memory maintenance should be predictable and inspectable.

The Broader Architecture

Sleep consolidation does not operate in isolation. It is one of ZenBrain's nine foundational algorithms, with a six-component Predictive Memory Architecture (PMA) on top in production. The open-source package ships those algorithms as 20 modules; the two components marked "(PMA)" below are not part of it:

  • vmPFC-coupled FSRS schedules when memories need review — gated by prediction-error signals from the knowledge graph
  • Two-Factor Synaptic Model strengthens co-activated KG edges (with an EWC-style penalty, I_ij = 1/σ²_ij standing in for the Fisher information)
  • Ebbinghaus decay models forgetting over time with personalized curves
  • NeuromodulatorEngine (PMA) modulates decay and learning rate via DA / NE / 5-HT / ACh tonic levels
  • Bayesian confidence propagation updates certainty across related facts
  • Simulation-Selection Sleep picks replay candidates by a TAG score from TD-error, reward, novelty and emotional weight and strengthens or weakens them
  • TripleCopyMemory (PMA) stores each event in three copies with divergent decay (Fast 4 h, Medium 14 d, Deep 7 d log-growth)

In the paper's model, these together form a memory lifecycle: encode, reinforce, consolidate, decay, review, forget. This is how biological memory works, and we believe AI memory should work that way too. In the open-source packages they are separate functions you connect yourself; the MemoryCoordinator uses FSRS-style scheduling and emotional tagging from them.

What the paper shows

Added on 9 October 2026. The figures in this section come from the paper's simulation. Our correction notice of 18 September 2026 records two further findings: the only run of the sleep loop on real data showed a significant loss in retrieval precision. And under stress, 10 of 15 components reach the printed criterion in the simulation, nine with a loss and PriorityMap with a gain; the figure is corrected below.

In the ZenBrain paper we measured sleep consolidation against a "no-sleep" baseline under controlled conditions — 7 simulated days, 350 facts, 10 seeds, paired Wilcoxon test:

  • +37 % average stability (simulation on synthetic facts) — memories with a sleep cycle have more than a third longer half-life than without
  • −47.4 % storage estimate through synaptic downscaling and pruning of weak edges, without a reinforcement-learning step (simulation on synthetic facts)
  • 24.2 new associative edges per run of seven cycles, created by REM interleaving between emotionally weighted memories that sit close together in the replay ranking (simulation)
  • Fiedler value (algebraic connectivity) +30 % after sleep — fixed by construction, not a finding: the suite's sleep step multiplies every edge by 1.3, so the row checks the spectral computation, not consolidation (version 9 of the paper)

In the full 15-component ablation, sleep is the single strongest individual contribution under moderate conditions (ΔQ = −34.4 % when removed). Under stress (60 days, decay=0.25), the picture sharpens: in the simulation, 10 of 15 components reach the printed criterion, nine with a loss — sleep stays in the survival tier.

References

  • Stickgold, R., & Walker, M. P. (2013). Sleep-dependent memory triage. Nature Neuroscience, 16(2), 139-145.
  • Tononi, G., & Cirelli, C. (2006). Sleep function and synaptic homeostasis. Sleep Medicine Reviews, 10(1), 49-62.
  • Cairney, S. A., et al. (2014). Complementary roles of slow-wave sleep and rapid eye movement sleep in emotional memory consolidation. Cerebral Cortex, 25(6), 1565-1575.
  • Ye, J., Su, J., & Cao, Y. (2022). A Stochastic Shortest Path Algorithm for Optimizing Spaced Repetition Scheduling. KDD 2022.

Try It

ZenBrain's sleep consolidation is open source and zero-dependency:

npm install @zensation/algorithms @zensation/core
import { selectForReplay, simulateReplay, pruneWeakConnections } from '@zensation/algorithms/sleep-consolidation';

// Select top memories for replay
const candidates = selectForReplay(memories, { maxReplaysPerCycle: 20 });

// Simulate hippocampal replay (edges are part of each memory object)
const result = simulateReplay(candidates, {
  replayStrengthMultiplier: 1.5,
});
// result.replayed, result.strengthened, result.pruned

// Prune weak connections (default threshold: 0.2)
const { kept, pruned } = pruneWeakConnections(edges);

All functions are pure TypeScript with zero external dependencies. Use them in your own AI systems.

Source: github.com/zensation-ai/zenbrain Paper: ZenBrain Technical Report