Teach Your AI Agent to Remember Across Sessions

LLMs are stateless by design - every session starts from zero. Here's how AI agent memory actually works, from in-context working memory to persistent vector stores, with a concrete example.

Cover art for Teach Your AI Agent to Remember Across Sessions

Close Slack on a Friday. Open it Monday. Ask your AI agent to pick up where you left off. It stares back at you like you've never met.

That is not a model quality problem. It is an architecture problem. LLMs are stateless by design. Each inference call begins from zero, with no knowledge of what came before - the model processes a fresh context window and discards all intermediate computation when the call ends. No internal state carries forward. The agent did not forget. It never had anywhere to put the memory in the first place.

This is the actual problem AI agent memory solves, and it is worth understanding precisely, because the solutions range from simple to surprisingly complex.

Why a larger context window does not fix this

A common assumption in 2025 was that models with 128K or 200K token windows would simply "hold everything." That assumption collapsed quickly in practice. Two things went wrong.

First, there is the "lost in the middle" problem. Research by Liu et al. showed that models failed to attend to information placed in the middle of long prompts - facts buried deep in a massive context became functionally invisible, with accuracy dropping sharply for mid-sequence positions. Second, it is expensive. Every LLM call re-processes the entire context window, meaning a 50,000-token context does not cost less than a 10,000-token context just because only the last 500 tokens changed.

A more recent April 2026 study adds a further nuance: using the context window for long-term facts, cross-session preferences, or hard constraints produces failure modes that look like model problems but are not. The study "Omission Constraints Decay While Commission Constraints Persist in Long-Context LLM Agents" quantified this across 4,416 trials at six conversation depths.

So bigger is not better. You need a different structure entirely.

The four memory types, and what each one actually stores

The standard taxonomy covers four types: in-context (working) memory - the active context window the model reasons over; episodic memory - records of past events and interactions; semantic memory - factual knowledge, definitions, and accumulated world knowledge; and procedural memory - skills, rules, and behavioural instructions. The CoALA framework, published by Princeton researchers in 2023, formalised this taxonomy from cognitive science for language model agents.

It helps to make each one concrete.

In-context (working) memory is the context window right now - the system prompt, the conversation so far, tool outputs, retrieved chunks. It is the only memory the model directly reasons over. All other memory types must be retrieved into the context window to influence generation. Think of it as RAM: fast, immediately available, and gone the moment the session ends.

Episodic memory is the log of what happened and when. It is the agent's record of specific experiences or events, tied to a time and context - like recalling "last Tuesday, I helped a user debug code and got stuck on a syntax error."

In practice, episodic memory stores structured records of interactions: timestamps, user identifiers, actions taken, environmental conditions, and outcomes observed.

Semantic memory is different - it holds general facts, not specific events. Semantic memory stores general facts and definitions - what things are, independent of when or where they were learned. Episodic memory stores specific past events tied to time. The distinction matters architecturally: you query semantic memory when you need a definition or a rule; you query episodic memory when you need context from a prior session.

Procedural memory is the most often overlooked. In agentic AI, procedural memory is often embedded in programming or fine-tuned through training, enabling the agent to perform tasks automatically without rethinking each move. When an agent learns that your team's incident runbook has a specific format, and stops asking about it, that is procedural memory at work.

How production systems wire this together

The production consensus in 2025-2026 is a three-tier hierarchy: in-context working memory - the current session's raw message buffer, tool calls, and active state, which is ephemeral and cleared on session end; session-scoped compressed memory - summarised or extracted facts from the current session, readable on demand; and long-term persistent store - cross-session knowledge, user preferences, and learned workflows, persisted in vector or graph storage.

The most instructive example of this in practice is Letta (formerly MemGPT). Letta uses a three-tier model inspired by OS memory management: core memory (always in-context, like RAM), archival memory (external searchable vector store, like disk), and recall memory (conversation history). Agents do not passively receive context - they explicitly call memory management functions to move information between tiers. This makes agents active participants in their own memory management, not passive recipients of injected context.

That last point is worth sitting with. Most agent frameworks inject context at the top of a system prompt and call it done. Letta's approach is more like a file system call: the agent decides what to move into working memory and when.

On the retrieval side, memory systems are moving beyond pure vector similarity. Vector memory retrieves semantically similar facts. Graph-style memory retrieves facts through entities and relationships. Both are useful; neither is sufficient alone.

A concrete failure mode illustrates why: imagine an agent that helps your team triage incidents. A vector store will surface "similar past incidents" well. But "who owned the payments service when this incident happened six months ago, and what did they change the week before?" requires traversing relationships - ownership history, deployment events, timing - that a flat embedding search cannot reconstruct on its own.

The consolidation step everyone skips

CoALA distinguishes episodic (instance-specific, context-preserved) from semantic (abstracted, generalised) memory, and names the transformation between them - consolidation - as the mechanism that makes the pair productive. Consolidation converts episodic events into durable semantic knowledge; it is the most impactful and least-implemented stage.

In plain terms: after your agent handles fifty support tickets, each ticket is an episodic record. Consolidation is the step that turns those fifty records into a semantic fact - "the most common cause of login failures in this product is an expired OAuth token." The episodic raw material becomes reusable knowledge.

Current LLMs score below 0.300 on chronological-awareness benchmarks

  • meaning they struggle to reason about the sequence of past events even when those events are provided. Consolidation offloads that reasoning burden: rather than asking the model to reconstruct a timeline from 50 raw records, you give it a distilled fact.

Mem0 approaches this automatically. Its semantic layer extracts named entities and relationships from conversations via an LLM pipeline, stores them as nodes and edges in a graph database, and cross-links them to vector embeddings for fuzzy search. The conflict detection step - where new facts are compared against existing graph entries and merged, updated, or flagged for resolution - is the critical architectural innovation.

For teams building on LangChain, LangMem launched in early 2025 as the official memory layer for LangGraph. LangMem supports three memory types: episodic (past interactions), semantic (facts and preferences), and procedural (agents updating their own system instructions). The procedural piece is the interesting one - an agent that can rewrite its own instructions based on what it has learned is doing something meaningfully different from one that simply recalls facts.

A teammate like Beagle - living in Slack or Teams - runs into this boundary constantly. Without a persistent memory layer, every new channel it enters is a blank slate: no knowledge of prior runbooks, no record of which on-call patterns your team actually follows versus what the docs say. With one, the first question it answers in a new incident channel can draw on the last six months of resolved incidents, not just the current thread.

Appending every interaction to a vector store eventually produces retrieval noise, context dilution, and latency spikes. Memory consolidation is what prevents this. The engineering work is not in choosing a vector database - it is in deciding what gets distilled, what gets discarded, and when. That is the part most implementations still skip.

Keep reading