Architecture · Write path

How AI Agents Write and Store Memories

Agents write memory by extracting salient facts from conversations, deduplicating against existing stores, embedding them and persisting to a vector database, graph or buffer — the first step in the AI memory loop.

Write pipeline

1
Trigger
Turn end / tool
2
Extract
Facts + entities
3
Dedup
Merge / conflict
4
Embed
Vector index
5
Persist
Store write

Triggers

What triggers a memory write?

End-of-turn extraction, explicit memory.add() tool calls, salience thresholds, user commands and session-end batch jobs.

  • End-of-turn — automatic extraction after agent responds (Engram, Mem0 default)
  • Explicit tool call — agent invokes memory.add() or Letta core/archival write
  • Salience threshold — only high-importance facts promoted from working memory
  • User command — “remember that I prefer window seats”
  • Session-end batch — consolidate transcript into durable memories

Memory as a tool

Extraction

Memory extraction: what gets stored

Extraction pipelines identify facts, preferences and entities — not usually raw transcript.

An LLM prompt or rules pipeline turns “I moved to Berlin and I’m vegetarian” into two clean memories. Quality filters: relevance, novelty, PII redaction. Mem0’s LOCOMO LLM-as-a-Judge score is 66.9 (68.4 with graph memory) vs 72.9 for full-context baseline at roughly 15× the token cost (Chhikara et al., 2025).

How AI memory works

Dedup

Deduplication and merging

Before write, check existing memories for duplicates and near-duplicates — merge, update or invalidate instead of bloating the store.

Vector similarity search against existing user memories catches “user lives in Munich” when “user lives in Berlin” arrives — trigger update or supersede. Zep’s Graphiti invalidates bi-temporal edges on contradiction (Rasmussen et al., 2025).

Conflicting memory updates

Indexing

Embedding and indexing

Extracted text → embedding model → vector index with metadata for filtering and ranking.

Standard metadata fields: user_id, agent_id, timestamp, memory_type (episodic/semantic/procedural), source_turn, importance. Mem0 median search latency: 0.148 s on LOCOMO eval (Chhikara et al., 2025).

Embeddings for memory

Backends

Storage backends for writes

BackendWrite patternFrameworks
Vector collectionEmbed + upsertEngram, Mem0, DIY pgvector
Graph nodes/edgesEntity + relationship extractZep (Graphiti), Cognee
KV / RedisKey-value writeCustom pipelines
LangGraph storeCheckpoint + store.putLangMem
Tiered pagingCore + archival writeLetta (MemGPT)

Vector databases · Knowledge graphs · Storage backends

Frameworks

Framework write implementations

Each framework implements the same write pipeline with different defaults and backends.

Engram (Weaviate)

Per-interaction pipeline on Weaviate — fire-and-forget writes with run_id status polling (Weaviate docs, 2026). Unified vector + memory on one platform.

Engram explained →

Mem0

Managed extraction API — memory.add() or auto-extract after each turn. LOCOMO J 66.9 (Chhikara et al., 2025).

Mem0 alternatives →

Zep (Graphiti)

Ingest conversations → extract entities/relationships → bi-temporal graph write with invalidation on supersede.

Zep alternatives →

Letta (MemGPT)

Agent-controlled core memory (in-context) and archival memory (paged store) writes via memory tools.

Letta alternatives →

LangMem

LangGraph store.put() at node boundaries with checkpointer integration.

LangMem explained →

FAQ

Frequently asked questions

When should agents write memory?

After each turn when extraction finds durable facts, on explicit user request, or at session end for batch consolidation. Don't write ephemeral greetings or one-off clarifications. See triggers section above.

Should agents store the full conversation transcript?

Usually no — extraction pipelines store salient facts and events, not every token. Raw logs may live in a separate audit store. Transcript-only storage bloats retrieval and raises cost. See extraction section above.

What model should extract memories?

Typically the same LLM family as the agent (GPT-4, Claude, etc.) with a structured extraction prompt. Smaller models work for simple fact extraction; quality affects LOCOMO-style recall benchmarks.

How do you handle PII in memory writes?

Redact or hash PII at extraction time, scope memories by user ID, encrypt at rest, and support deletion on request. Many teams filter SSNs, card numbers and passwords before write. See compliance notes in storage backends.

Does memory write latency affect user experience?

Writes can be async (fire-and-forget) so the user sees the response immediately. Engram pipelines run in background with run_id polling. Mem0 p95 total latency 1.44 s includes retrieval + generation on LOCOMO (Chhikara et al., 2025).

Batch vs realtime memory writes?

Realtime (per turn) keeps memory fresh for the next message. Batch (session end) reduces write cost but misses mid-session retrieval. Production agents typically write salient facts per turn and batch-consolidate summaries.

How does Engram write memories?

Per-interaction pipeline on Weaviate — extract, embed and persist to a memory collection. Fire-and-forget with async status via run_id. See Engram explained.

How does Mem0 write memories?

Auto-extract after each turn or explicit memory.add() — embed and store in vector backend scoped by user ID. LOCOMO J 66.9 (Chhikara et al., 2025). See Mem0 alternatives.

Graph vs vector write — which backend?

Vector writes for semantic similarity search (Engram, Mem0). Graph writes when relationships and temporal validity matter (Zep Graphiti, Cognee). See vector vs knowledge graph memory.

What happens when a new fact contradicts an old memory?

Dedup step should detect near-duplicates; on contradiction, invalidate or supersede the old entry. Zep does this natively via bi-temporal graph edges. See conflicting memories.