What is long term memory in AI agents
AI agents is a persistent storage mechanism that retains information across multiple interactions or sessions, enabling the agent to remember facts, user preferences, or past events. It differs from short-term memory by maintaining knowledge beyond immediate conversations, often implemented using databases, vector stores, or external knowledge bases.How it works
Long term memory in AI agents functions like a digital notebook or filing cabinet where the agent stores important information beyond a single conversation. Unlike short-term memory, which only holds context temporarily during one session, long term memory persists data such as user preferences, facts, or previous interactions. When the agent needs to recall this information, it queries this memory store to provide more personalized and context-aware responses.
Think of it as a librarian who remembers which books you borrowed last month and uses that knowledge to recommend new books today. This memory can be implemented using databases, vector embeddings with similarity search, or knowledge graphs integrated with the agent's reasoning process.
Concrete example
This example shows how an AI agent can store and retrieve long term memory using a vector database with OpenAI embeddings and gpt-4o. The agent saves user notes and recalls them later to personalize responses.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Simulated long term memory store (in practice use a vector DB like FAISS or Pinecone)
long_term_memory = {}
# Function to embed and store memory
async def store_memory(key, text):
response = await client.embeddings.create(
model="text-embedding-3-large",
input=text
)
embedding = response.data[0].embedding
long_term_memory[key] = {"text": text, "embedding": embedding}
# Function to retrieve memory by simple keyword match (demo only)
def retrieve_memory(key):
return long_term_memory.get(key, {}).get("text", None)
# Store a user preference
await store_memory("user_preference", "User likes sci-fi books and prefers short summaries.")
# Later, retrieve and use it in a prompt
memory_text = retrieve_memory("user_preference")
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": f"Remember this about the user: {memory_text}. Recommend a sci-fi book."}
]
response = await client.chat.completions.acreate(
model="gpt-4o",
messages=messages
)
print(response.choices[0].message.content) The AI agent responds with a sci-fi book recommendation tailored to the user's preference for short summaries.
When to use it
Use long term memory in AI agents when you need persistent context across sessions, such as remembering user preferences, ongoing projects, or historical data. It is essential for personalized assistants, customer support bots, and any AI that benefits from continuity.
Do not use long term memory when interactions are one-off or privacy concerns prevent storing user data. In those cases, short-term or session-based memory suffices.
Key terms
| Term | Definition |
|---|---|
| Long term memory | Persistent storage of information across multiple AI agent sessions. |
| Short term memory | Temporary context held only during a single interaction or session. |
| Vector embeddings | Numerical representations of text used for similarity search in memory retrieval. |
| Knowledge base | Structured repository of facts and information used by AI agents. |
| Vector database | A database optimized for storing and searching vector embeddings. |
Key Takeaways
- Long term memory enables AI agents to retain knowledge beyond single sessions for personalized interactions.
- Implement long term memory using vector embeddings, databases, or knowledge graphs integrated with the agent.
- Use long term memory when persistent context improves user experience; avoid it if privacy or one-off interactions dominate.
- Combining long term memory with powerful LLMs like
gpt-4oenhances contextual understanding and response relevance.