Prompt
How do I add memory to an AI agent?
Latest observation
Adding “memory” to an AI agent usually means giving it a way to store, retrieve, and use past information across interactions. There are a few common patterns, and the right one depends on what kind of memory you want.
1) Start with the main kinds of memory
Short-term memory
Used within a single conversation or task.
- Examples: the last few messages, current goals, intermediate steps
- Usually stored in the prompt/context window
Long-term memory
Persisted across sessions.
- Examples: user preferences, prior decisions, facts learned over time
- Usually stored in a database, vector store, or key-value store
Working memory
Temporary scratch space for reasoning.
- Examples: plans, state variables, task progress
- Often represented as structured JSON or a state object
2) Common implementation approach
A. Store memories externally
Don’t rely only on the model’s context window. Save information in:
- Relational DB for structured facts
- Key-value store for preferences and session data
- Vector database for semantic recall of past notes/messages
- Document store for chat logs, summaries, artifacts
B. Retrieve relevant memories before responding
When the agent gets a new input:
- Parse the input
- Search memory for relevant items
- Add the retrieved items to the prompt
- Generate the response
- Optionally write new memory back
C. Summarize over time
For long chats, keep:
- full raw history externally
- a rolling summary in the prompt
- key facts extracted into structured memory
3) A practical memory architecture
A simple and effective setup:
- Conversation buffer: last N messages
- Session summary: compact summary of the conversation so far
- User profile memory: preferences, stable facts
- Episodic memory: important past events, stored with timestamps
- Semantic memory: embeddings for retrieval by similarity
4) Example flow
When user says: “Remember that I prefer Python and concise answers.”
- Detect this is a memory-worthy statement
- Extract structured fact:
{ "type": "preference", "key": "language", "value": "Python" } - Store it in persistent memory
- Later, when answering, retrieve it and adapt response style
5) What to store
Good memory candidates:
- User preferences
- Stable identity facts
- Project goals
- Decisions already made
- Important constraints
- Frequently used resources
Avoid storing:
- Everything verbatim
- Sensitive data unless necessary and permitted
- Highly transient or low-value chatter
6) Memory writing rules
Only write to memory when:
- The user explicitly says to remember something
- A fact is clearly persistent and useful
- A decision or preference should affect future behavior
A good rule: save durable, actionable, user-relevant information.
7) Memory retrieval rules
Before each response:
- Search by semantic similarity
- Filter by recency and relevance
- Prefer structured memory for stable facts
- Use summaries for broad context
- Don’t overload the prompt with too much memory
8) Minimal pseudo-architecture
def agent_respond(user_message, user_id):
memories = retrieve_relevant_memories(user_id, user_message)
recent_chat = get_recent_messages(user_id)
summary = get_session_summary(user_id)
prompt = build_prompt(
summary=summary,
recent_chat=recent_chat,
memories=memories,
user_message=user_message
)
response = llm_generate(prompt)
new_memories = extract_memories(user_message, response)
save_memories(user_id, new_memories)
update_summary(user_id, user_message, response)
return response
9) If you want the simplest version
The easiest memory system is:
- Store chat history in a database
- Keep a running summary
- Retrieve the most relevant old messages with embeddings
- Insert retrieved items into the prompt
That gets you a useful “memory” without making the system too complex.
10) Important caveats
- Context window is not memory — it’s only short-term input
- Memory can be wrong — let users correct it
- Privacy matters — be careful with personal data
- Too much memory hurts performance — retrieve selectively
If you want, I can also show you:
- a Python example using a vector database,
- a LangChain/LlamaIndex implementation, or
- a production-grade memory design for an AI assistant.
Brands