LLM Agent Memory: The Complete Engineering Guide
Everything required to build production memory for LLM agents: the four memory operations, hybrid retrieval scoring, write control, history compression, person-entity modeling, and how 2026 benchmarks actually compare.

Every LLM agent shipped in 2026 faces the same decision: what does it do when the conversation ends? Whatever your answer, that's your memory architecture — even if the answer is "nothing."
This guide covers everything required to engineer memory for LLM agents in production: the operations it must perform, the scoring that decides what gets retrieved, the write-control problem most systems ignore, and how the current benchmarks actually compare. It distills what I've learned building iRemember, an LLM assistant with persistent structured memory.
Why context windows didn't kill memory
Context windows grew from thousands to millions of tokens. The predictable take was "memory is solved." Production experience says otherwise.
Three failure modes survive unlimited context:
- Retrieval degradation — attention quality drops as relevant evidence spreads across hundreds of turns
- Cost — re-reading an entire history on every turn scales linearly with usage; memory retrieval does not
- Structure — a transcript is not knowledge. "My sister lives in Rabat" said in March and "I'm visiting family" said in July are connected facts; raw context never connects them
Benchmarks confirm this: on LoCoMo's very long conversations, models still fail at time-sensitive questions ("what did we decide last spring?") even when the answer sits inside the window.
The four operations every memory system needs
Strip away vendor terminology and a memory system is four operations:
- Extraction — separate durable facts from ephemeral context, mid-conversation, without slowing the response loop
- Consolidation — merge updates and contradictions over time ("she moved to Casa" replaces "she lives in Rabat")
- Retrieval — surface the right memory at answer time; the hard part is right, not top-k similar
- Forgetting — decay relevance so the system doesn't drown in its own history
Most off-the-shelf systems implement 1 and half of 3. Consolidation and forgetting are where custom engineering happens — and where accuracy gains live.
Hybrid retrieval scoring
Semantic similarity alone retrieves memories that sound related. You need related and recent and important:
score = α · semantic_similarity(q, m)
+ β · recency_decay(m.timestamp)
+ γ · importance_weight(m)
Tuning notes from production:
- Recency decay should be per-memory-type. A preference decays slower than a plan.
- Importance weights can be learned from extraction-time signals: emotional intensity, explicit markers ("remember this"), and reference frequency — how often later memories point back to this fact.
- Normalize scores before combining; raw cosine similarities cluster near the top of their range and starve β and γ of dynamic range.
Write control: learn what to remember
The biggest shift in 2026 memory research is reframing extraction as a write-control problem. Uniform remember-everything policies (the Mem0 default) cause memory bloat: irrelevant trivia crowds out useful facts and steadily erodes QA accuracy.
Two 2026 papers quantify the fix:
- AdaMem learns a role-specific Memory Policy from weekly feedback loops — daily extraction under policy, weekly evaluation via QA, patch-style reflection updating the policy. Result: up to +9% QA accuracy over uniform Mem0 while shrinking memory volume by 9%.
- PerMem-Bench formalizes personalized storage gating — skipping memory writes entirely for transient sessions. With perfect gating, retention gains are large; the open problem is that real gating accuracy is still too low to capture them.
Practical takeaway: your extraction prompt is a policy document. Treat it as versioned, evaluated code, not a one-time prompt.
Architecture patterns compared
| Approach | Strength | Weakness | Fit |
|---|---|---|---|
| Full context | Zero infrastructure | Cost + degradation on long horizons | Demos, <20-turn bots |
| Uniform extraction (Mem0-style) | Simple, good baseline | Bloat, no personalization of what's stored | Fast MVPs |
| Managed memory APIs (Zep etc.) | Turnkey, decent recall | Opaque policies, per-call cost, less control | Products without AI teams |
| Custom LangGraph pipeline | Full control of all four operations + policies | Engineering cost | This is where iRemember landed |
The custom path earns its cost once you need any of: entity-structured memory, budget-aware routing, domain-specific decay, or tenant-bounded scopes (non-negotiable inside multitenant SaaS — prompt-level isolation is not isolation).
History compression
Long conversations need compression, but naive summarization destroys exactly the details users ask about. What works:
- Compress episodes, keep facts. Summaries carry narrative; extracted facts carry retrievable precision.
- Compress progressively, oldest-first, gated by whether a fact still exists in structured form.
- Preserve temporal anchors during compression ("last spring", "after the Rabat trip") — relative dates rot fast.
Person-entity modeling
The differentiator behind iRemember: model people as first-class entities with evolving profiles and sentiment over time.
Without entity structure, "How has my relationship with my co-founder been trending?" is unanswerable — the evidence is scattered across dozens of memories. With it, the question becomes a graph traversal plus sentiment aggregation.
This also fixes retrieval: mentions of the same person under different names consolidate into one node, and queries about that person route to everything touching them.
Benchmarks, practitioner's view
- LoCoMo — very long multi-session dialogues; good for temporal reasoning gaps
- LongMemEval — systematic ablations show retrieval-stage tuning (depth, formatting, query design) outweighs ingestion-stage changes
- AdaMem-Bench — golden-memory annotations; best current proxy for "did the system keep the right things"
- PerMem-Bench — multi-domain personas; measures storage-gating ability
Run at least two. Single-benchmark results overfit to a benchmark's conversation style — I learned this the hard way with early iRemember evaluations.
Start here
- Memory Is the Missing Layer — the core argument
- Building blocks referenced above live in the iRemember project page
Next posts in this cluster will cover benchmark comparisons with published numbers from iRemember's evaluation harness, and LangGraph pipeline teardowns. Subscribe via the blog RSS or reach me directly.
FAQ
What is long-term memory for LLM agents?
A persistent, structured store of facts, events and entities extracted from past interactions — with retrieval that surfaces the right memory at answer time. It is not the same as a long context window: memory controls what is kept, how it decays, and what gets retrieved.
Is Mem0 enough for production agent memory?
Mem0 is a solid baseline, but its uniform remember-everything extraction causes memory bloat on long horizons. 2026 research (AdaMem, PerMem-Bench) shows write-control policies — learning what to remember per user — improve QA accuracy while shrinking memory volume. Many teams end up with a custom LangGraph pipeline instead.
How do you score which memories to retrieve?
Hybrid scoring: a weighted mix of semantic similarity, recency decay, and importance weight. Importance can be learned from extraction-time signals such as emotional intensity, explicit retention markers, and how often other memories reference a fact.
Which benchmarks measure LLM memory?
LoCoMo (very long multi-session conversations), LongMemEval (time-sensitive and multi-session QA), AdaMem-Bench (personalized long-horizon interaction with golden-memory annotations), and PerMem-Bench (personalized storage policies across user personas).