How to persist agent memory between sessions
Quick answer
To persist
agent memory between sessions, save the memory state externally (e.g., in a file, database, or vector store) and reload it when the agent restarts. This allows the agent to maintain context across sessions by restoring previous conversation history or embeddings.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the openai Python SDK and set your API key as an environment variable.
- Run
pip install openaito install the SDK. - Set your API key in your shell:
export OPENAI_API_KEY='your_api_key'(Linux/macOS) orsetx OPENAI_API_KEY "your_api_key"(Windows).
pip install openai Step by step
This example demonstrates saving and loading agent memory as a JSON file to persist conversation history between sessions.
import os
import json
from openai import OpenAI
# Initialize OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# File to persist memory
MEMORY_FILE = "agent_memory.json"
# Load memory from file if exists
if os.path.exists(MEMORY_FILE):
with open(MEMORY_FILE, "r") as f:
memory = json.load(f)
else:
memory = [] # start with empty memory
# Append new user message
user_message = "Hello, how are you?"
memory.append({"role": "user", "content": user_message})
# Call the chat completion with memory as context
response = client.chat.completions.create(
model="gpt-4o",
messages=memory
)
# Append assistant response to memory
assistant_message = response.choices[0].message.content
memory.append({"role": "assistant", "content": assistant_message})
print("Assistant:", assistant_message)
# Persist updated memory to file
with open(MEMORY_FILE, "w") as f:
json.dump(memory, f, indent=2) output
Assistant: I'm doing well, thank you! How can I assist you today?
Common variations
You can persist memory using different storage backends:
- Databases: Store conversation history in SQL or NoSQL databases for scalability.
- Vector stores: Save embeddings for semantic search and retrieval (e.g., FAISS, ChromaDB).
- Async usage: Use async SDK methods to load/save memory without blocking.
- Different models: Use
claude-3-5-sonnet-20241022orgemini-1.5-prowith the same memory persistence approach.
Troubleshooting
- If memory file is corrupted or unreadable, delete it to reset memory.
- If context length exceeds model limits, truncate older messages before saving.
- Ensure environment variable
OPENAI_API_KEYis set correctly to avoid authentication errors.
Key Takeaways
- Persist agent memory externally to maintain context across sessions.
- Use JSON files, databases, or vector stores depending on scale and use case.
- Always reload and update memory before and after each interaction.
- Truncate memory if it exceeds model token limits to avoid errors.
- Set environment variables securely for API authentication.