Consolidation and forgetting in memory systems
Be able to implement rules for when episodes become facts and when facts should be forgotten.
Prerequisites
- FSemantic memory and consolidationrequired
Intuition
Episodic memory grows without limit. Without consolidation it becomes a heap of events nobody can use.
Consolidation is the job that periodically reads the new episodes and updates a short list of facts: «prefers code examples», «is working towards the LoRA project by December», «knows linear algebra».
Three rules that decide the quality:
- Support: a fact requires at least k episodes (or an explicit statement from the user). Otherwise isolated coincidences become «truths».
- Confidence: every fact carries a number that is raised on new support and falls with time.
- Validity: facts that are time-bound («revising for the May exam») get an expiry date.
Forgetting is not a shortcoming — it is what keeps the list usable.
Code
import time, math
HALF_LIFE_DAYS = {"preference": 180, "knowledge": 365, "goal": 90, "temporary": 14}
MIN_CONFIDENCE, MIN_SUPPORT = 0.3, 2
def consolidate(facts: list[dict], new_episodes: list[dict], llm) -> list[dict]:
candidates = llm.extract_facts(new_episodes) # [{text, type, sources}]
index = {f["text"].lower(): f for f in facts}
now = time.time()
for c in candidates:
key = c["text"].lower()
if key in index: # reinforce an existing one
f = index[key]
f["sources"] = sorted(set(f["sources"]) | set(c["sources"]))
f["confidence"] = min(1.0, f["confidence"] + 0.25)
f["last_supported"] = now
elif len(c["sources"]) >= MIN_SUPPORT: # a new fact requires support
index[key] = {**c, "confidence": 0.5, "last_supported": now}
return forget(list(index.values()), now)
def forget(facts, now):
kept = []
for f in facts:
age_d = (now - f["last_supported"]) / 86400
hl = HALF_LIFE_DAYS.get(f.get("type", "preference"), 180)
f["confidence"] *= 0.5 ** (age_d / hl)
if f["confidence"] >= MIN_CONFIDENCE:
kept.append(f)
return sorted(kept, key=lambda f: -f["confidence"])[:50] # a hard ceiling
Measure the precision of the list of facts: take a sample of 30 facts and let a human being (or the user themselves) mark them correct / incorrect / out of date. Below 90 % precision the memory does more harm than good — then the thresholds should be raised.
Mastery means
- Implements rules for when episodes become facts
- Introduces forgetting with confidence and time
- Measures the precision of the list of facts
Sign in to do the exercises and build your mastery up.
Sources
- arXiv — MemGPT: Towards LLMs as Operating Systems — arXiv (open access; licence per article)
- arXiv — Generative Agents: Interactive Simulacra of Human Behavior — arXiv (open access; licence per article)