Skip to content
AgenticAIWiki
  1. Learn
  2. Part 10
  3. 57. Managing memory over time
  4. 57.3

Lesson 57.3 · 3 of 5 · 4 min read · Reviewed 24 Nov 2026

Consolidation and summarization

By the end, you can

  • Consolidate and summarise memories over time.
  • Merge duplicates and resolve conflicts.

Why consolidate

Without upkeep, memory stores fill with:

  • Duplicates: “prefers short answers” saved six times
  • Contradictions: old and new addresses side by side
  • Fragments: twenty tiny episodes about one long project

All of these waste context and confuse the model.

Consolidation steps

Step Example
Deduplicate Six copies of a preference become one
Resolve conflicts Newer explicit statements replace older ones
Summarise episodes Twenty sessions on “migration project” become one summary with key decisions
Re-rate importance Promote memories that keep being used

When to run it

  • At the end of a session: merge new memories with existing ones.
  • On a schedule: nightly or weekly per user or project.
  • When a store passes a size limit.

A model can do the merging with a clear prompt; code checks the result (same owner, no invented facts, sources kept).

Provenance

A merged memory should list what it came from:

{"text": "Lives in Munich", "since": "2026-02", "sources": ["chat 2026-02-11"], "replaces": ["m-118", "m-204"]}

If the merge was wrong, you can undo it.

Key points

  • Memory stores grow messy: duplicates, near-duplicates and contradictions pile up.
  • Run consolidation: merge duplicates, update changed facts, and summarise many small episodes into fewer larger ones.
  • Keep provenance so a merged memory can be traced back to its sources.
Hands-on labRuns in the Playground

Consolidate a messy memory store

Merge duplicates, resolve conflicts with the newest explicit statement, and keep provenance so every merged memory can be traced and undone.

You will have: A consolidate function and the before and after store, with sources and replaced ids on each merged entry.

Steps

  1. Read the STORE list: id, key, text and date for each memory.
  2. Group memories by key; within a group, normalise text to find exact duplicates.
  3. For each key keep the newest value, and record the ids it replaces and the ids it came from.
  4. Print the store size before and after, and each consolidated entry.

Done when

  • Three copies of 'prefers short answers' become one entry with three sources.
  • lives_in resolves to Munich and lists the Berlin ids as replaced.
  • The store shrinks from 6 entries to 2.

Open in the Playground No API key needed.

Starter code (Python)
STORE = [
    {"id": "m-101", "key": "style", "text": "Prefers short answers", "date": "2025-01-05"},
    {"id": "m-107", "key": "style", "text": "prefers short answers.", "date": "2025-03-02"},
    {"id": "m-112", "key": "style", "text": "Prefers  short answers", "date": "2025-06-19"},
    {"id": "m-118", "key": "lives_in", "text": "Lives in Berlin", "date": "2024-05-01"},
    {"id": "m-204", "key": "lives_in", "text": "Moved to Munich", "date": "2026-02-11"},
    {"id": "m-205", "key": "lives_in", "text": "Lives in Berlin", "date": "2025-08-30"},
]

def consolidate(store):
    # TODO: one entry per key: newest text, "sources" (ids with the same text as the winner),
    # and "replaces" (ids with a different, older text)
    return store

after = consolidate(STORE)
print("before:", len(STORE), "after:", len(after))
for e in after:
    print(e)
Show a solution
STORE = [
    {"id": "m-101", "key": "style", "text": "Prefers short answers", "date": "2025-01-05"},
    {"id": "m-107", "key": "style", "text": "prefers short answers.", "date": "2025-03-02"},
    {"id": "m-112", "key": "style", "text": "Prefers  short answers", "date": "2025-06-19"},
    {"id": "m-118", "key": "lives_in", "text": "Lives in Berlin", "date": "2024-05-01"},
    {"id": "m-204", "key": "lives_in", "text": "Moved to Munich", "date": "2026-02-11"},
    {"id": "m-205", "key": "lives_in", "text": "Lives in Berlin", "date": "2025-08-30"},
]

def norm(t):
    return " ".join(t.lower().strip(" .").split())

def consolidate(store):
    groups = {}
    for m in store:
        groups.setdefault(m["key"], []).append(m)
    out = []
    for key, ms in groups.items():
        ms.sort(key=lambda m: m["date"])
        winner = ms[-1]
        same = [m["id"] for m in ms if norm(m["text"]) == norm(winner["text"])]
        replaced = [m["id"] for m in ms if norm(m["text"]) != norm(winner["text"])]
        out.append({"key": key, "text": winner["text"].strip(" ."), "since": winner["date"], "sources": same, "replaces": replaced})
    return out

after = consolidate(STORE)
print("before:", len(STORE), "after:", len(after))
for e in after:
    print(e)
Quick checkA store has three memories: 'lives in Berlin' (2024), 'moved to Munich' (2026), 'lives in Berlin' (2025). What should consolidation produce?
How sure are you?
Pick an answer and how sure you are