Skip to content

Learn AI · Foundations

Agent Memory Explained

How AI agents remember context across sessions — short-term window, long-term stores, vector memory, and when you do not need memory at all.

7 min readPublished Jun 2026Updated Jun 2026
AgentsMemoryRAGContext
Edited by The AIKnowHub team · Editorial team

Key takeaways

  • 1Short-term memory is the chat window and summarization of older turns.
  • 2Long-term memory is stored facts/preferences retrieved per session — not magic.
  • 3RAG over user documents is not agent memory — it is external knowledge.
  • 4Memory increases cost and privacy surface — justify before adding.
  • 5Explicit user preferences beat inferred memory for product trust.

Three layers

1. Short-term (conversation)

The last N messages in context. Summarize older turns when the window fills.

Cost: tokens every request.
Implementation: message array + rolling summary system prompt.

2. Long-term (user memory)

Facts extracted from past sessions: "prefers bullet points", "uses Python", "company is Acme Corp".

Cost: extraction LLM call + storage + retrieval embedding.
Implementation: Mem0, custom Postgres JSONB, or vector store per user.

3. External knowledge (RAG)

Not memory — documents, tickets, codebase. Retrieved by query relevance.

Implementation: RAG Explained, vector DB.

When to add long-term memory

Add when users say:

  • "Remember that I..."
  • "Like last time..."
  • "You already know my stack"

Skip when:

  • Single-session tasks (classify this email)
  • RAG on docs covers the knowledge need
  • Privacy policy forbids storing inferred traits

Memory extraction pattern

After each session:
  → LLM extracts candidate facts (structured JSON)
  → Deduplicate against existing memory
  → Store with source session ID and timestamp

On new session:
  → Embed user query
  → Retrieve top-k memory facts
  → Inject into system prompt (labeled as user memory)

Show users what is stored. Let them delete.

Production concerns

RiskMitigation
Wrong fact storedConfidence threshold + user confirm
Stale preferencesTTL + last_verified date
Cross-user leakStrict tenant isolation in DB
Token bloatCap retrieved memories at 5-10 facts

Tools (2026)

ToolRole
Mem0OSS memory layer
Postgres + pgvectorRoll your own with full control
LangGraph checkpointSession state, not user preferences
RedisFast session cache

Coming to directory: Mem0. For now see Top GitHub Repos.

Practical default

v1: 20-turn history + RAG on user docs
v2: Summarize sessions older than 1 hour
v3: Long-term memory for power users who opt in

Memory is a product feature with privacy cost — not an infrastructure requirement on launch day.

Common misconceptions

The wrong-but-common takes worth correcting.

Myth

Agents need Mem0 or a memory layer from day one.

Reality

Most v1 chatbots need RAG on docs plus 10-turn history. Memory layers add when users expect continuity across days.

Myth

Long context windows replace memory.

Reality

200k tokens still cost money and dilute attention. Summarize and retrieve selectively.

Real-world use cases

Frequently asked questions

Open-source memory layer that extracts and stores user facts across sessions, retrieved by relevance. Good when users return daily and expect personalization.