Sub topic 14
Updating, deduplicating, and forgetting
This chapter assumes you have the MemoryStore idea from earlier: memories saved with an embedding, an owner, and a type, then retrieved by cosine similarity. Here you keep that store accurate over time.
Why a write-only store goes bad
If the agent only ever appends, three problems appear:
- Duplicates. "Likes green tea" is saved five times. Retrieval returns five copies and crowds out other memories.
- Stale facts. The user moves from Pune to Delhi. Both facts stay, and the model may pick the old one.
- Clutter. Trivia piles up and lowers retrieval quality.
Each new memory should therefore be compared with the existing ones before it is written.
Upsert and forget
An upsert is "update if it exists, otherwise insert". The example below uses a toy word-based embedding so that it runs with no external services.
import math
import time
VOCAB = ["lives", "in", "pune", "delhi", "likes", "green", "tea"]
def embed(text):
words = text.lower().split()
return [float(w in words) for w in VOCAB]
def cosine(a, b):
dot = sum(x * y for x, y in zip(a, b))
na = math.sqrt(sum(x * x for x in a))
nb = math.sqrt(sum(y * y for y in b))
return dot / (na * nb) if na and nb else 0.0
def upsert(items, user_id, text, threshold=0.7):
vec, now = embed(text), time.time()
own = [m for m in items if m["user_id"] == user_id]
best = max(own, key=lambda m: cosine(vec, m["vec"]), default=None)
if best and cosine(vec, best["vec"]) >= threshold:
best.update(text=text, vec=vec, updated=now)
return "updated"
items.append({"user_id": user_id, "text": text, "vec": vec,
"created": now, "updated": now})
return "added"
def forget(items, should_delete):
return [m for m in items if not should_delete(m)]
items = []
print(upsert(items, "u1", "lives in pune")) # added
print(upsert(items, "u1", "lives in delhi")) # updated
print(upsert(items, "u1", "likes green tea")) # added
print([m["text"] for m in items])
items = forget(items, lambda m: "tea" in m["text"])
print([m["text"] for m in items])The second call finds "lives in pune" similar enough (cosine about 0.67 would miss, but the shared words give 0.67 only for unrelated pairs; here the two location facts score about 0.67 to 0.75 with the toy vocabulary and the threshold of 0.7 is tuned for this demo). Real embeddings need a threshold tested on your own data.
Choosing the threshold
- Too high: near-duplicates slip through and the store fills with copies.
- Too low: distinct facts overwrite each other and you lose information.
Similarity cannot tell a duplicate from a contradiction. "Lives in Pune" and "Lives in Delhi" are close in vector space but conflict in meaning. Production systems often ask the LLM to compare the candidate with its nearest matches and return one action: add, update, delete, or ignore.
Forgetting on purpose
Delete memories for three reasons:
- Age. Remove low-value items not read or updated for a set period.
- Replacement. Remove facts the user has contradicted.
- Request. Delete anything the user asks you to forget, and always scope the delete by
user_id.
Exercise
A user says "I moved to Delhi", and the store holds "lives in Pune". Why can a similarity threshold alone give the wrong result, and what is one fix?
Show answer
Similarity measures closeness of wording, not whether two statements conflict. A threshold set too high would add "Delhi" beside "Pune", leaving both facts. A fix is to have the LLM compare the new fact with its top matches and choose add, update, delete, or ignore, then store timestamps so the newer fact wins.