Memory Architectures for Autonomous Agents
The context window is not memory. It is working memory — volatile, expensive, and bounded. Building agents that learn from experience and maintain state…
A taxonomy of memory systems for autonomous agents — episodic, semantic, procedural, and working memory — with concrete implementation patterns using vector databases, structured stores, and hybrid retrieval systems. Includes tradeoffs and failure modes observed in production deployments.
Contents
- A Taxonomy of Agent Memory
- Working Memory — The Context Window
- Episodic Memory — What Happened
- Semantic Memory — What Is True
- Procedural Memory — How to Do Things
- Retrieval-Augmented Memory — The Bridge
- Chunking Strategy
- Embedding Choice
- Re-ranking
- Memory Coordination in Multi-Agent Systems
- Memory Inconsistency
- Memory Flooding
- Memory Poisoning
- Implementation Pattern — Minimal Production Memory System
- When Context Windows Are Enough
Every autonomous agent confronts the same fundamental constraint: it can only reason about what is in its context window. The context window is finite, expensive, and volatile. When the session ends, the window empties. The agent forgets.
This is fine for agents that do single-session tasks — search a document, answer a question, refactor a function. It is catastrophic for agents that need to learn from experience, maintain relationships across time, or coordinate with other agents over extended operations.
Building agents that persist across sessions requires deliberate memory architecture. This is not a problem you can solve by increasing the context window size — a 200K token window helps with breadth but does nothing for the fundamental impermanence of the context-only approach.
A Taxonomy of Agent Memory
Cognitive science gives us a useful starting taxonomy, adapted for AI systems:
Working Memory — The Context Window
Working memory is what the agent can actively reason about right now. In LLM-based agents, this is the context window. Fast, directly accessible, and limited. The agent's reasoning operates here. Everything else is retrieval.
Optimization principle: working memory is precious. Only include what is needed for the current reasoning step. Aggressive summarization of prior conversation turns, tool call results, and intermediate reasoning reduces working memory load and improves output quality.
Episodic Memory — What Happened
Episodic memory stores specific past events: "on Tuesday I ran the database migration and it failed with error X." In agent systems, this maps to a log of completed actions, their outcomes, and the context in which they occurred.
Implementation: a time-ordered log stored in a vector database, where each entry is a chunk of completed-action context. Retrieval uses semantic similarity — "how do I handle this type of failure?" returns past failures similar to the current one, along with what resolved them.
The key property of episodic memory is that it is append-only. You do not update past episodes — you add new ones. This preserves the audit trail and prevents confabulation, where an agent rewrites its history to match its current beliefs.
Semantic Memory — What Is True
Semantic memory stores general knowledge: facts, schemas, domain knowledge, relationship definitions. In agent systems, this is the layer that answers "how does this system work?" rather than "what happened?"
Implementation: structured key-value stores, knowledge graphs, or vector-embedded fact bases. Semantic memory is updatable — when the agent learns that a system changed, the old fact should be replaced. This creates a consistency challenge that episodic memory avoids entirely.
A practical approach is to store semantic facts with confidence scores and timestamps. A fact with low confidence or an old timestamp triggers re-verification before use. This prevents the agent from acting on stale knowledge without knowing it is stale.
Procedural Memory — How to Do Things
Procedural memory stores skills and procedures: how to run a deployment, how to debug a specific class of error, how to structure a particular type of report. In agent systems, this maps to system prompts, few-shot examples, and validated tool usage patterns.
Implementation: structured prompt libraries indexed by task type. When an agent begins a task, it retrieves the procedure for that task type and includes it in the system prompt. The procedure is not static — successful task completions can update the procedure library with improved steps.
Retrieval-Augmented Memory — The Bridge
RAG (Retrieval-Augmented Generation) connects long-term memory to working memory. The agent formulates a query based on the current task, retrieves relevant memories from the long-term store, and includes them in the context window for the current reasoning step.
The key decisions in RAG architecture are:
Chunking Strategy
How you chunk stored information determines what can be retrieved. Naive fixed-size chunking (every 512 tokens) produces incoherent retrievals when a chunk boundary falls in the middle of a logical unit. Better strategies:
- Semantic chunking — split at logical boundaries (paragraph breaks, section headers) rather than token counts
- Hierarchical chunking — store both parent documents and child chunks; retrieve chunks, include parent for context
- Proposition extraction — extract atomic facts from documents and store facts as the retrievable unit
Embedding Choice
The embedding model determines retrieval quality. For technical content (code, system descriptions, error messages), code-specific embedding models (CodeBERT, text-embedding-3-large with code fine-tuning) significantly outperform general-purpose embeddings. For conversational content, general-purpose models perform well.
Hybrid search — combining vector similarity with keyword matching (BM25) — consistently outperforms pure vector search in production. The intuition: vector search finds semantically similar content but can miss exact technical terms; keyword search finds exact terms but misses paraphrase. The hybrid approach captures both.
Re-ranking
Retrieve more than you include. Retrieve the top 20 candidates, re-rank them with a cross-encoder model, and include only the top 5 in context. Re-ranking is the highest-ROI improvement to retrieval quality because it trades compute (cheap) for relevance (high impact on generation quality).
Memory Coordination in Multi-Agent Systems
When multiple agents operate on the same task — a common pattern in production orchestration systems — memory coordination becomes critical. Three failure modes to design against:
Memory Inconsistency
Two agents read the same fact at time T1. Agent A updates the fact at T2. Agent B reads the stale version at T3 and acts on it. The agents now have conflicting world models.
Solution: treat shared memory as a database with transaction semantics. Reads are versioned. Updates include a compare-and-swap check. An agent that reads a version and then tries to update based on it will fail if another agent modified the fact in between, triggering a re-read and re-reason cycle.
Memory Flooding
A high-frequency agent (monitoring loop running every minute) writes to shared memory constantly. Other agents' retrieval becomes saturated with monitoring-generated entries and misses the important episodic events they need.
Solution: separate memory namespaces by agent type. Monitoring agents write to their own namespace. Episodic agents write to theirs. Cross-namespace retrieval requires explicit query scoping. Each agent's memories are retrievable by others, but the default retrieval is namespace-scoped.
Memory Poisoning
An agent stores incorrect conclusions in shared memory, and other agents act on them. The incorrect conclusion propagates across the system faster than it can be corrected.
Solution: memory entries include a confidence score and a source chain. Low-confidence entries trigger verification before use. Entries from a single agent (rather than corroborated by multiple) are marked as unverified. This is analogous to quorum in distributed systems — agreement from multiple independent sources increases the reliability of stored knowledge.
Implementation Pattern — Minimal Production Memory System
interface MemoryEntry {
id: string
content: string // human-readable description
embedding: number[] // vector representation
type: 'episodic' | 'semantic' | 'procedural'
agentId: string // which agent created this
confidence: number // 0–1
timestamp: string // ISO
ttl?: number // ms until expiry (null = permanent)
tags: string[] // for keyword retrieval
}
class AgentMemory {
async store(entry: Omit<MemoryEntry, 'id' | 'embedding'>): Promise<string>
async retrieve(query: string, options?: RetrievalOptions): Promise<MemoryEntry[]>
async update(id: string, changes: Partial<MemoryEntry>, ifVersion?: number): Promise<void>
async forget(id: string): Promise<void>
}
The ifVersion parameter on update implements optimistic concurrency control — the update fails if the entry was modified since the version the agent last read. This prevents the Memory Inconsistency failure mode without requiring distributed locks.
When Context Windows Are Enough
Not every agent needs persistent memory. Before building memory infrastructure, answer two questions:
- Does this agent need to remember anything across session boundaries?
- Does this agent's effectiveness improve with experience over time?
If both answers are no, a well-constructed system prompt and access to relevant documentation is sufficient. Memory infrastructure adds operational complexity — it requires a vector database, an embedding pipeline, retrieval tuning, and memory hygiene processes (TTL management, garbage collection, consistency maintenance). That cost is only justified when the agent genuinely needs it.
The agents that benefit most from persistent memory are those with long-running relationships: support agents that learn a customer's system over time, infrastructure agents that develop operational intuition about a specific deployment's behavior, research agents that accumulate domain knowledge across a project lifecycle. For these agents, memory is not an optimization — it is the mechanism by which they become useful at all.