How to Implement Long-Term Memory for AI Companions: Pinning, Compaction, and Lessons from Mem0 and Letta
Description and use
The article explains how to implement long‑term memory for AI companions using pinning, compaction, and lessons from Mem0 and Letta. It presents a four‑layer context where users pin critical facts and the system automatically extracts and stores summaries and memory cards.
Problem
AI companions often forget user facts when conversations exceed the model’s context window, leading to incorrect or irrelevant responses.
Outcome
The four‑layer context strategy reduces memory‑related failures, ensuring user‑pinned facts are always present and that long‑term memories are accurately extracted and retrieved.
Workflow
- 1. User pins important facts (e.g., allergies) which are stored in a pinned memory layer.
- 2. Every turn the system injects pinned memory into the model’s context.
- 3. Recent messages are kept in a sliding window for immediate context.
- 4. When the conversation window closes, an extraction step creates a medium-term summary and long-term memory cards from the closed window.
- 5. The agent retrieves relevant cards on demand based on the current query.
- 6. Unfinished plans that span beyond the current window are promoted to cards to avoid loss during compaction.
Evidence
The model suggests peanut butter cookies.
Mem0 keeps memory at user, session, and agent level, so the same store can hold "Alice prefers dark mode" and "this support session is about billing".
Letta answers the three questions very differently from Mem0. Memory belongs to the agent, and the agent edits it itself.
Our context for each reply has four layers: Pinned Memory, Recent messages, Medium‑term summary, Long‑term memory cards.
- Record date: 2026-11-03T06:13:52.797Z