Memory → What to write (the extraction step)
What it is
The naive implementation writes every turn to memory. After 200 conversations the store contains 3,000 fragments, most of them "user said thanks", "user asked about the weather", "user rephrased their question". Similarity search now returns noise, injection budget is consumed by trivia, and the one fact that mattered — "this user works on the German entity" — competes with 400 others.
The extraction step is a gate: after an interaction, a cheap model (or rules) decides what, if anything, is worth remembering, and in which form:
interaction ──► extract ──► {nothing} ← the common, correct answer
──► semantic fact (typed field) ← "default entity = Acme GmbH"
──► episodic record (summary) ← "asked about EMEA renewals; accepted answer"
──► procedural update ← "for renewal questions, check Schedule 3 first"
│
└─► validate → dedupe → supersede-or-insert
The default answer must be "nothing". Most conversation turns are not memory-worthy.
How to implement
- Define memory-worthiness explicitly in the extraction prompt: durable (true beyond this session), reusable (would change a future answer), specific (not a generic preference everyone has), and non-sensitive (or explicitly permitted).
- Constrain to a typed schema for semantic facts — a fixed field list, not free-form notes. The model chooses among known fields or returns nothing.
- Run asynchronously after the response, so extraction never adds user-facing latency.
- Validate: field in the allowed set, value type/format correct, PII policy satisfied (PII), confidence above a threshold.
- Dedupe and supersede rather than append: if the fact already exists with the same value, update
last_seen; if it differs, supersede (memory conflict resolution). - Prefer explicit signals over inference: "remember that I work on the German entity" is high confidence; inferring it from one question is not. Store
sourceand weight accordingly. - Cap writes per interaction (e.g. ≤ 2 semantic facts, 1 episodic record) — a hard limit on store growth.
Small example
EXTRACT_SYS = """Decide what, if anything, from this interaction should be remembered for future sessions.
Write a memory ONLY if it is:
- durable (true beyond this conversation),
- reusable (would change how you answer a future question),
- specific to this user or their work,
- not sensitive personal data unless the user explicitly asked you to remember it.
Do NOT write: greetings, acknowledgements, rephrasings, one-off factual questions,
anything already obvious from the user's profile, or speculation about the user.
Allowed semantic fields ONLY: {fields}
Output JSON:
{{"semantic": [{{"field": string, "value": string, "confidence": 0-1, "evidence": string}}],
"episodic": {{"summary": string, "outcome": string}} | null,
"nothing_worth_remembering": bool}}
Prefer returning nothing_worth_remembering=true. Maximum 2 semantic facts."""
ALLOWED_FIELDS = ["role", "team", "default_entity", "region", "reporting_currency",
"communication_style", "recurring_topics", "tools_used"]
def extract_memories(interaction, user) -> dict:
out = json.loads(llm("memory_extract", EXTRACT_SYS.format(fields=ALLOWED_FIELDS),
[{"role": "user", "content": f"<conversation>{render_turns(interaction.turns[-6:])}</conversation>"
f"<outcome>{interaction.outcome}</outcome>"}],
max_tokens=400).text)
if out.get("nothing_worth_remembering"):
log_memory_write(user.id, wrote=0, reason="not_worthy")
return {"written": 0}
written = 0
for fact in out.get("semantic", [])[:2]: # hard cap
if fact["field"] not in ALLOWED_FIELDS: continue # no invented fields
if fact["confidence"] < 0.7: continue # low-confidence inference is noise
if contains_sensitive_pii(fact["value"]) and not user_consented(user, fact["field"]): continue
upsert_semantic_fact(user, fact["field"], fact["value"],
source="inferred" if "said" not in fact["evidence"] else "user_stated",
confidence=fact["confidence"]) # supersede-or-insert (memory conflict resolution)
written += 1
if out.get("episodic") and interaction.outcome in ("accepted", "edited", "escalated"):
insert_episode(user, summary=out["episodic"]["summary"][:300],
outcome=out["episodic"]["outcome"],
artifacts=interaction.cited_chunk_ids, trace_id=interaction.trace_id)
written += 1
log_memory_write(user.id, wrote=written)
return {"written": written}
# Explicit user request bypasses the "worthiness" judgement but not validation
REMEMBER_INTENT = re.compile(r"\b(remember|note that|from now on|always|keep in mind)\b", re.I)
def handle_explicit_memory_request(turn, user):
if not REMEMBER_INTENT.search(turn):
return None
parsed = llm("memory_explicit", EXPLICIT_SYS, [{"role": "user", "content": turn}])
fact = json.loads(parsed.text)
if fact["field"] in ALLOWED_FIELDS:
upsert_semantic_fact(user, fact["field"], fact["value"], source="user_stated", confidence=1.0)
return f"Noted: {fact['field']} = {fact['value']}." # confirm back to the user
return "I can't store that kind of detail." # honest limitation
Pros
- Keeps recall precise — a small store of high-value facts beats a large store of noise.
- Bounded growth means bounded cost, bounded injection, and manageable privacy surface.
- Typed fields are auditable and correctable by the user (how to forget).
- Explicit user requests are honoured and confirmed, which builds trust.
- Cheap: one small async call per interaction, often returning "nothing".
Cons
- Extraction errors are persistent: a wrong fact written once affects every future session until corrected (failure modes: poisoning, distraction, confusion, clash poisoning with a long tail).
- Under-writing loses value — an overly strict gate means the memory feature does nothing.
- Inference is risky: deducing a user's role from one question and asserting it later feels wrong to users and is often incorrect.
- Async writes can race with the next interaction (a fact written after the user already corrected it).
- Judging "worthiness" is subjective and needs tuning against real usage.
When to use
Whenever you have long-term memory at all. Unfiltered writing is not a viable production design.
When NOT to use (aggressive extraction)
- Sensitive domains where storing inferences about people is inappropriate — restrict to explicit user statements only.
- Early in a product's life: start with explicit "remember this" requests only, then add cautious inference once you can measure its accuracy.
Production at scale
Default to nothing. Measure the write rate: healthy systems write on a small minority of interactions. A system writing on most turns has a broken gate and a store that will be useless in a month.
Explicit beats inferred, and mark which is which. Store source and surface it: the model should treat "user stated" as fact and "inferred" as a hypothesis, and never assert an inference back to the user as though they'd said it.
Confirm explicit writes in the response ("Noted: your default entity is Acme GmbH"). This gives the user an immediate correction opportunity and is the cheapest quality control available.
Async but ordered. Extraction runs after the response; ensure writes for a user are serialised so two concurrent interactions don't create conflicting facts. Use the user ID as a partition key.
PII policy at the write gate (PII): a fixed field list plus a sensitive-data check. Never store health, financial, or protected-characteristic inferences without explicit consent, and log consent when you do.
Review sample: audit a sample of written memories against the source conversation. Precision of memory writes is a metric worth tracking — a 20% wrong-fact rate is a product problem.
Growth caps per user with an eviction policy (how to forget): if the store hits a cap, the lowest-value entries (old, never-retrieved, low-confidence) go first.
Feed retrieval statistics back into the gate: memories never retrieved after N sessions were probably not worth writing. Use that to tune the worthiness criteria (data flywheel).
Prod checklist
- Explicit worthiness criteria (durable, reusable, specific, permitted) in the extraction prompt
- Typed field allow-list; invented fields rejected
- "Nothing worth remembering" as the expected default; write rate monitored
- Confidence threshold;
source(stated vs inferred) recorded and surfaced - Explicit "remember this" requests handled and confirmed back to the user
- Async extraction, serialised per user; never on the response path
- PII/consent check at the write gate; caps per interaction and per user
- Supersede-or-insert rather than append (memory conflict resolution)
- Sample audit of write precision; retrieval statistics feeding gate tuning
Metrics to watch
| Metric | Why |
|---|---|
| Write rate (interactions producing a memory) | Gate discipline |
| Memory write precision (audit sample) | Quality of what's stored |
| Retrieval rate of written memories | Whether writes were worth it |
| Store size per user over time | Growth control |
| User corrections/deletions | Wrong facts reaching users |
Related
- Memory types — where each write goes
- When to retrieve, How to forget
- Conflict resolution — supersede-or-insert
- Security — writes are an injection surface
- PII