Memory Types in AI Agents
A single context window is not enough for agents that need to remember facts across sessions, search large knowledge bases, or learn user preferences over time. OpenClaw supports four distinct memory types, each suited to different use cases.
Why Memory Matters
Without persistent memory, every agent run starts fresh. The agent forgets what happened last session, can't build on prior results, and can't accumulate knowledge. Memory lets agents become genuinely useful over time.
In-Context Memory (Short-Term)
The simplest form: everything stored in the current LLM context window. It's automatic — every tool result, every thought, every user message is in-context by default.
- Pros: Zero configuration, no external storage
- Cons: Cleared after each session, limited by context window
Episodic Memory
Episodic memory stores summaries of past sessions so the agent can reference what happened "last time." OpenClaw compresses and stores episode summaries to a local file or key-value store.
# Enable episodic memory
memory:
episodic:
enabled: true
backend: "file" # file | redis | postgres
path: ".openclaw/episodes"
max_episodes: 50
compression: "summary" # Store LLM summary, not full transcriptSemantic Memory (Vector Store)
Semantic memory embeds facts as vectors and retrieves them by similarity. Ideal for agents that need to search a large knowledge base (documents, notes, code) at query time.
memory:
semantic:
enabled: true
backend: "chroma" # chroma | pinecone | weaviate | pgvector
collection: "agent-kb"
embedding_model: "text-embedding-3-small"
top_k: 5 # Retrieve 5 most relevant chunksfrom openclaw.memory import SemanticMemory
mem = SemanticMemory(backend="chroma", collection="agent-kb")
# Ingest documents
mem.add("Python 3.13 introduces a JIT compiler for 1.5–5x speedups.")
mem.add("The walrus operator := was introduced in Python 3.8.")
# Query at runtime (happens automatically during agent runs)
results = mem.search("What's new in Python 3.13?")
print(results[0].text)Procedural Memory (Learned Preferences)
Procedural memory stores how the agent should behave: user preferences, style rules, recurring patterns. It's injected into the system prompt at the start of each session.
memory:
procedural:
enabled: true
path: ".openclaw/preferences.json"
inject_at: "system_prompt"Example stored preference:
{
"preferences": [
"Always use British English spelling.",
"Output code in Python unless the user says otherwise.",
"Keep responses under 300 words unless asked for detail."
]
}Persistence Across Sessions
With episodic and procedural memory enabled, a new agent session starts with injected context from prior runs:
- System prompt ← procedural preferences
- First user message ← episodic summary from last session
- Semantic query ← relevant knowledge retrieved on demand
- In-context memory builds as the session progresses
Memory Type Trade-offs
| Type | Capacity | Latency | Persistence | Best For |
|---|---|---|---|---|
| In-Context | ~128k tokens | None | Session only | Short tasks |
| Episodic | Unlimited summaries | Low | Cross-session | Ongoing tasks |
| Semantic | Millions of facts | Medium | Persistent | Knowledge search |
| Procedural | Small (rules list) | None | Always-on | Behaviour tuning |
- Four memory types cover all agent memory needs: short-term, episodic, semantic, and procedural.
- In-context memory is automatic; external types require opt-in configuration.
- Use semantic memory for large knowledge bases; use episodic for session continuity.
- Procedural memory is the easiest way to persist user preferences across sessions.
Implementing Memory in Practice
Here’s a complete example showing all four memory types working together in a customer service agent:
from openclaw import Agent
from openclaw.memory import (
InMemory, # Working memory (automatic)
EpisodicMemory, # Session history
SemanticMemory, # Knowledge base (vector DB)
ProceduralMemory, # User preferences
)
agent = Agent(
model="gpt-4o",
memory=[
EpisodicMemory(max_turns=20), # Keep last 20 turns
SemanticMemory(
backend="pinecone",
index="product-knowledge-base",
top_k=5,
),
ProceduralMemory(
store="redis",
key_prefix="user:{user_id}:prefs",
),
],
)
Memory Privacy and Data Retention
Memory stores that persist user data are subject to privacy regulations. Follow these practices:
- Honour erasure requests. Implement a
clear_user_memory(user_id)function that deletes episodic and procedural memory for a user when they request deletion. - TTL on episodic memory. Set a time-to-live on stored conversations (e.g., 90 days) rather than retaining them indefinitely.
- Don’t store raw PII in semantic memory. Redact names, emails, and phone numbers before embeddings are generated and stored in the vector database.