#185 — Your AI agent has a memory problem. Here's how to fix it.
June 29, 2026·6 min read

Contents
Why it matters: AI agents that forget context mid-conversation aren't just annoying — they're a product liability. The fix isn't a bigger context window. It's architecture.
The core diagnosis
Long conversations contain four fundamentally different types of information — and they all have different shelf lives:
- Temporary details that only matter for the next response
- Durable decisions that should be remembered across sessions
- User preferences, project facts, and task state that need to be queryable and updatable
- Tool results, failed attempts, successful outcomes, and follow-up actions that agents need to avoid repeating mistakes
Treating all of it as one long transcript doesn't scale. The model either drowns in irrelevant context, misses older details, or depends on a summary that quietly dropped something important.
Why a bigger context window won't save you
More tokens buys you time. It doesn't buy you a memory policy. It still doesn't answer:
- Which facts should survive across sessions?
- Which decisions are authoritative when two records disagree?
- Which prior attempts should not be repeated?
- Which memory belongs to this user, this project, or this task?
The policy has to come from your application architecture — not from the model provider.
The six memory layers and what each one does
No single approach is enough. Each layer has a specific job:
| Layer | Best for | Weakness |
|---|---|---|
| Sliding window | Recent turns and immediate coherence | Older context falls out by design |
| Rolling summaries | Compressing older dialogue to save tokens | Can lose detail, merge ideas, or drift over time |
| Vector retrieval | Semantic recall when users paraphrase past context | Similarity ≠ relevance; can return outdated chunks |
| Structured memory | Stable facts, preferences, decisions, entities, task state | Requires explicit extraction rules and update logic |
| Episodic memory | What happened, what was tried, what failed, and why | Needs retention and importance-flagging rules |
| Memory manager | Coordinating what gets stored, retrieved, summarized, and injected per turn | Adds application logic that must be designed and tested |
What "episodic" actually means — and why it's underrated
A fact says what is true. An episode says what happened, what changed, and why it mattered.
If a user asks "what should I try next?", your agent needs to know the global patch already failed and the EU-only patch passed staging. A rolling summary likely won't capture that. Structured memory won't either. Episodic memory is the layer that makes task resumption reliable — and it's the one most teams skip.
When memory layers conflict
Layered memory introduces a new engineering question: what happens when your layers disagree?
Your memory manager needs explicit freshness and conflict rules:
- Prefer structured decisions over summaries when both reference the same fact
- Prefer newer memory when two records share the same authority level
- Prefer scoped memory (project-specific, region-specific) over generic memory
- Downgrade retrieved chunks that are old, superseded, or weakly related to the current task
- Tag every memory record with source, timestamp, scope, and type — don't delete stale context, mark it as
superseded,rejected, orarchived
This makes your system debuggable. If the agent gives the wrong answer, you can trace which memory layer supplied the evidence and why.
How the memory manager assembles each turn
The memory manager is not just a collector — it's the policy layer. For every turn, it should decide:
- Which recent turns to include
- Whether the rolling summary is still current
- Which structured facts and decisions are relevant to this task
- Which episodic events apply (especially failed paths)
- Which vector-retrieved chunks pass a similarity threshold and match the current thread scope
- What should be written or updated after the response
Priority order for context assembly:
- Current user message
- Recent conversation turns
- Active structured decisions and project state
- Relevant episodic events
- Rolling summary
- Vector-retrieved chunks
- Archived/superseded memory (only if it explains why a path shouldn't be repeated)
The goal: the smallest useful context package that is current, scoped, and explainable.
The right pattern by use case
Don't over-engineer early. Match the layer to the problem:
| Scenario | What to use |
|---|---|
| Short chats | Sliding window only |
| Long linear chats | Sliding window + rolling summary |
| Cross-session recall | Add vector retrieval |
| Preferences and profile facts | Add structured memory |
| Task resumption / "what failed before" | Add episodic memory |
| Production-grade, multi-user continuity | Full hybrid stack + memory manager |
How to roll it out (in order)
- Store every message — you'll want the audit trail before you need it
- Add a bounded recent-context window + rolling summary for older dialogue
- Extract structured memory for decisions, preferences, task state, and project facts
- Log episodic events for anything that happened, changed, or failed
- Layer in vector retrieval for semantic recall across sessions
- Wire a memory manager to assemble a clean, scoped context package per turn
- Move to a database-backed layer (SQL + vector + JSON metadata) when memory needs to be durable, queryable, shared, access-controlled, or auditable
The production bar
Memory isn't done when it works in a demo. It's done when it's:
- Inspectable — you can see what was stored and why
- Retrievable — you can pull the right record for any given turn
- Updateable — decisions can be superseded, not just appended
- Scoped — memories belong to a user, thread, or task — not to everyone
- Governed — access controls exist; not every agent can read every memory
- Debuggable — when the answer is wrong, you can trace which layer failed and fix it
That's the same bar you'd hold any other production system to. Memory should be no different.
Frequently asked questions
How is agent memory different from RAG?
RAG retrieves external documents to answer a question. Agent memory stores and retrieves what happened inside the conversation itself — decisions made, facts established, attempts that failed, and user preferences. RAG is about knowledge; agent memory is about continuity and state. In production, you'll almost certainly need both: RAG for grounding answers in your knowledge base, agent memory for preserving what was decided in the thread.
What happens when my memory layers give conflicting answers?
This is one of the most underrated failure modes in production agents. When layers disagree — say, your rolling summary preserves an old plan but structured memory has the final decision — you need explicit conflict rules: prefer structured decisions over summaries, prefer newer records over older ones, and prefer scoped memory (user- or project-specific) over generic memory. Tag every memory record with source, timestamp, scope, and status (active, superseded, rejected, archived). Without this, your agent will confidently hallucinate based on stale evidence.
When should I start storing episodic memory?
The moment your agent needs to resume a task, avoid repeating a failed approach, or answer 'what did we try before?' — which happens earlier than most founders expect. A coding assistant that forgets the global patch already failed will suggest it again. A customer success bot that doesn't remember a client escalated last month will miss the tone entirely. Episodic memory is what makes an agent feel like it tracks progress, not just text. Start logging it as soon as your agent takes actions or makes recommendations across multiple turns.
Can I just use LangChain's built-in memory classes and call it done?
LangChain memory classes are useful for demos and short sessions. For production, they fall short on durability, conflict handling, scoping, and governance — they don't give you queryable structured facts, they don't preserve episodic history across deployments, and they don't let you audit which memory surfaced a wrong answer. Use LangChain for orchestration and retriever interfaces, but back your memory with a real database that supports per-user scoping, access controls, and timestamp-aware freshness policies.
How do I debug an AI agent that keeps giving the wrong answer despite having context?
The answer is almost always in the context package assembled for that specific turn. Inspect it directly: Was the rolling summary stale? Did vector retrieval return an outdated chunk? Was the authoritative structured decision missing? Did episodic memory omit a failed attempt? The single most valuable architectural decision you can make is to log the full context package on every turn — what was assembled, from which layer, and with what priority. Without that, you're debugging a black box and guessing.
How much does AI agent memory add to my token costs?
If done right, it should reduce your token costs, not increase them. The whole point of layered memory is to replace a giant transcript with a minimal, targeted context package. A sliding window of 6 turns plus a 200-token rolling summary plus 3 structured facts is almost always smaller and more useful than 40 raw conversation turns. The expensive failure mode is stuffing everything into one prompt — layered memory with a good memory manager is the fix, not the cause.
What's the minimum viable memory architecture for a B2B SaaS AI agent?
For a B2B SaaS agent serving real users across sessions, the practical floor is: (1) store every message per user and thread, (2) rolling summary for threads over 10 turns, (3) structured memory for account preferences, decisions, and configuration state, and (4) episodic logs for any action the agent took or recommended. Vector retrieval can come later. The goal at MVP is to never make a customer repeat themselves and to never recommend something you already tried.
Should memory be scoped per user, per thread, or per project?
All three — and mixing them up is one of the most common production bugs in multi-tenant AI systems. A user's tone preferences belong to their profile. A specific debugging decision belongs to that thread. A product roadmap fact belongs to the project. Without explicit scoping on every memory record, you'll bleed context between customers or between sessions — which is both a product failure and a potential data privacy liability. Enforce scope at write time, not retrieval time.
How do I handle memory for multi-agent systems where more than one agent shares context?
Shared memory in multi-agent systems requires a centralized, governed memory layer — not per-agent in-process state. Each agent should read from and write to the same structured and episodic memory store, with clear ownership rules: which agent can update which memory type, and which records are read-only once committed. Without this, agents will contradict each other or act on stale facts. This is where a database-backed memory layer with access controls becomes non-negotiable rather than just convenient.
How do I know when my memory architecture is ready for production?
Apply the same bar you'd hold any other production system to. Memory is production-ready when it's inspectable (you can see what was stored and why), updateable (decisions can be superseded, not just appended), scoped (records belong to a user, thread, or task — not globally), governed (access controls exist), and debuggable (when the agent gives the wrong answer, you can trace which memory layer was responsible). If you can't trace a bad answer back to its source, your memory system is not production-ready.
What is the difference between conversation summary memory and episodic memory?
Conversation summary memory compresses what was said — it's a lossy representation of the dialogue over time. Episodic memory records what happened and why it mattered — specific events, outcomes, and failed attempts with timestamps. A rolling summary might tell you the team discussed a deployment issue. Episodic memory tells you the global patch was explicitly rejected, the EU-only patch passed staging, and the rollout was agreed for Thursday. For task resumption and avoiding repeated mistakes, episodic memory is the layer summaries cannot replace.
How does vector retrieval fail, and when should I not rely on it?
Vector retrieval fails when semantic similarity doesn't equal relevance or correctness. A retrieved chunk can be topically related but outdated, from a different project scope, or superseded by a later decision. It also struggles with precise structured facts — asking 'what timeout value did we set for EU checkout?' is better answered by structured memory than a similarity search. Use vector retrieval for paraphrased follow-ups and long-range semantic recall, but always rank it below explicit structured decisions in your memory manager's priority order.
How do I store memory so my agent can resume a long task after days or weeks?
The key is separating state from dialogue. Dialogue can be summarized and compressed. State — current task phase, open decisions, pending actions, known constraints — needs to be stored as structured memory with a clear schema and update rules. Episodic memory should log every meaningful event and outcome with a timestamp. When the user returns after two weeks, your memory manager should be able to reconstruct 'where we left off' from structured state and recent episodes — not by replaying the entire transcript.
What database schema should I use to store agent memory?
At minimum, you need four tables: conversation_memory (thread_id, role, content, turn_id, timestamp), structured_memory (thread_id, memory_type, memory_key, memory_value, scope_json, status), episodic_memory (thread_id, event_type, description, outcome, timestamp, status), and vector_memory (thread_id, chunk_id, text, embedding, source, timestamp). Every record should carry scope, timestamp, source, and a status field (active, superseded, rejected, archived) so your memory manager can enforce freshness and conflict rules without deleting historical context.
How should a memory manager prioritize what goes into each prompt turn?
A reliable priority order is: (1) current user message, (2) recent conversation turns, (3) active structured decisions and project state, (4) relevant episodic events, (5) rolling summary, (6) vector-retrieved chunks, (7) archived/superseded memory only when needed to explain why a path should not be repeated. The goal is the smallest useful context package that is current, scoped, and explainable — not the largest context window you can fill. Structured decisions should always rank above summaries and vector results when they conflict.
Keep reading

#186 — The 95:5 rule: Why most of your GTM is wasted
95% of buyers are not in the market for many goods and services at any one time. This is a deceptively simple fact, but it has profound implications.

#187 — Your AI agents are only as good as the tools you give them
Learn how to write high-quality tools and evaluations, and how you can boost performance by using AI to optimize its tools for itself.

#188 — Why your AI agent is broken and you don't know it yet
The capabilities that make agents useful also make them difficult to evaluate. The companies shipping AI agents fastest aren't guessing. They're running evals.