AI Agent Memory Systems 2026
Last week the user said the budget cap was 20k. This week the agent recommends a 50k setup. The model isn't dumb; there is no memory layer. This guide covers the three tiers of agent memory, how to store and retrieve each, and when to forget.
📜 Table of Contents
📖 Keep reading
Agent Memory Systems: Short-Term, Long-Term and External Memory
If your agent forgets that the budget cap was 20k last week, prompt engineering will not fix it. You need storage, a write policy and a retrieval path. This guide covers the three tiers, with Python code and Postgres + pgvector as the default store.
The three tiers
| Tier | Where it lives | Lifetime | Typical stack |
|---|---|---|---|
| Short-term | messages in the context window | one session | message array + compaction |
| Long-term | vector store + relational table | months | pgvector, Qdrant, Mem0, Letta |
| External | document index | follows the knowledge base | RAG, full-text search |
Do not mix them. User state must not live in a document index that gets rebuilt.
Short-term: budget the window first
A 128k window does not mean you should fill it. Budget roughly: system prompt 10%, tool definitions 15%, retrieved context 20%, history 35%, output reserve 20%. Keep the last 6-10 turns verbatim and roll older turns into a summary that preserves hard constraints, confirmed decisions and open tasks.
def build_context(messages, keep_recent=8, max_summary_chars=800):
if len(messages) <= keep_recent:
return messages
summary = summarize(messages[:-keep_recent])[:max_summary_chars]
return [{"role": "system", "content": f"Earlier: {summary}"}] + messages[-keep_recent:]
Long-term: write policy matters more than storage
Three write paths: explicit ("remember this"), event-driven (task completed, written by code), and extracted facts (whitelisted fields only, with a source message id). Store flat rows with tenant_id, user_id, kind, content, embedding, confidence and expires_at. Filter by tenant and user before vector ranking, then rerank, then inject 3-5 items.
Handle conflicts by overwriting the key and marking the old value as history, so the model never sees two budgets at once.
External memory is read-only
RAG is knowledge, not user state. Chunk on semantic boundaries, attach source metadata, and return citations. Keep it in a separate table and index so a rebuild never wipes user memory.
Three failure modes
- Contamination: model guesses stored as facts. Only store explicit statements.
- Summary of summaries: recursive compression erodes content. Cap summary count and length.
- No expiry: temporary requests become permanent preferences. Set TTLs (preferences ~180 days, task state ~7 days).
Choosing a stack
Start with pgvector and one table. Move to Mem0 or Letta for batteries-included memory, LangGraph checkpointers for workflow state, and Qdrant or Weaviate when hybrid retrieval at scale becomes the bottleneck.
FAQ
Do I still need compaction with a 128k window? Yes. Long contexts cost more per turn and mid-context details get missed.
Does more memory make the agent smarter? No. Precision drops as the store grows. A few dozen high-quality memories beat thousands of log lines.
Should memory be encrypted? Yes when it holds personal data. Tenant isolation, field-level encryption, and no raw text in logs.
How do I delete a user's memory? Physical delete by user_id plus index and cache cleanup. This is a compliance requirement.
Do I need fine-tuning for memory? No. Memory is a retrieval problem; fine-tuning changes style and format.
Framework features and defaults change; check the official docs for current behaviour.
Related
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.