Prompt
How do I give an agent memory across conversations?
Latest observation
To give an agent memory across conversations, you usually need to store important information externally and retrieve it at the start of future chats. The model itself is typically stateless between requests.
Common approach
1) Save memory to a datastore
When the user says something worth remembering, write it to storage such as:
- a database
- key-value store
- vector database
- user profile store
Examples of things to store:
- user preferences: “prefers concise answers”
- personal facts: “uses Python 3.11”
- long-term task context: “building a fitness app”
- conversation summaries
2) Retrieve relevant memory on each new conversation
At the beginning of a new chat:
- identify the user/session
- fetch relevant stored memory
- inject it into the prompt or tool context
Usually you don’t dump everything in; you retrieve only what matters.
3) Decide what counts as memory
Use rules or a classifier to distinguish:
- short-term context: current conversation only
- long-term memory: stable facts/preferences
- ephemeral notes: one-off details not worth saving
A good memory policy helps avoid clutter and stale facts.
Typical architecture
User message → Memory extraction → Store/update memory → Later retrieval → Prompt augmentation → Model response
You can implement memory in a few ways:
A. Structured memory
Store facts in fields:
{
"user_id": "123",
"preferences": {
"tone": "concise",
"language": "English"
},
"projects": ["fitness app"]
}
Best for stable, high-confidence data.
B. Semantic memory
Store embeddings of notes/messages in a vector DB and retrieve by similarity.
Best for:
- remembering prior discussion topics
- long notes
- loosely related context
C. Summarized conversation memory
After each chat, generate a summary and store it. Then use the summary in future chats.
Best for:
- keeping a running history without huge prompts
Practical best practices
- Ask permission before storing personal data.
- Let users view/edit/delete memory.
- Separate per-user and per-session memory.
- Expire stale memory or verify before using it.
- Prefer summaries + structured facts over raw logs.
- Don’t overstore; only keep useful, durable information.
Simple implementation pattern
- On each message, run a memory extractor:
- “Does this contain something worth remembering?”
- If yes, store it with metadata:
- user ID
- timestamp
- memory type
- confidence
- On future conversations:
- fetch top relevant memories
- include them in the system or developer prompt
Example prompt augmentation
Relevant memory about the user:
- Prefers short answers
- Working on a fitness app
- Uses Python
Respond accordingly.
Important caution
If by “memory” you mean the model remembering across chats automatically, that usually requires:
- a product-level memory feature, or
- your own backend storage and retrieval layer
The model alone won’t remember past conversations unless you provide that context again.
If you want, I can also show you:
- a simple database-based memory design,
- a vector search memory design, or
- a working code example in Python or JavaScript.