How to Intermediate · 3 min read

How to use vector database for agent memory

Quick answer
Use a vector database to store embeddings generated from agent interactions, enabling efficient similarity search for memory retrieval. Integrate this with your agent by embedding new inputs and querying the vector store to recall relevant past context, enhancing the agent's long-term memory capabilities.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0
  • pip install faiss-cpu
  • pip install langchain>=0.2

Setup vector database environment

Install necessary Python packages and set your environment variables for API keys. We'll use FAISS as the vector database and OpenAI embeddings for vectorization.

bash
pip install openai faiss-cpu langchain

Step by step example

This example shows how to embed text inputs, store them in a FAISS vector store, and query the store to retrieve relevant memory for an agent.

python
import os
from openai import OpenAI
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS

# Initialize OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Initialize embeddings using OpenAI
embeddings = OpenAIEmbeddings(client=client)

# Sample memory texts representing past agent interactions
memory_texts = [
    "User asked about weather forecast in New York.",
    "Agent provided Python code example for vector search.",
    "User requested help with API key setup.",
    "Agent explained how to use vector databases for memory."
]

# Create FAISS vector store from embeddings
vector_store = FAISS.from_texts(memory_texts, embeddings)

# New user query to find relevant memory
query = "How do I set up API keys for OpenAI?"

# Embed query and search vector store
results = vector_store.similarity_search(query, k=2)

print("Top relevant memories:")
for i, doc in enumerate(results, 1):
    print(f"{i}. {doc.page_content}")
output
Top relevant memories:
1. User requested help with API key setup.
2. Agent explained how to use vector databases for memory.

Common variations

  • Use async versions of the OpenAI client and embeddings for non-blocking calls.
  • Swap FAISS with other vector databases like Chroma or Pinecone for cloud-based storage.
  • Use different embedding models such as gpt-4o or claude-3-5-sonnet-20241022 for better semantic understanding.

Troubleshooting tips

  • If similarity search returns irrelevant results, check that embeddings are generated with the same model for both memory and queries.
  • Ensure your OPENAI_API_KEY environment variable is set correctly to avoid authentication errors.
  • For large memory datasets, periodically rebuild or update the vector index to maintain search performance.

Key Takeaways

  • Store agent memory as vector embeddings in a vector database for efficient similarity search.
  • Use consistent embedding models for both storing and querying memory to ensure relevance.
  • FAISS is a lightweight, local vector store ideal for prototyping agent memory.
  • Switch to cloud vector databases for scalable, persistent agent memory solutions.
  • Integrate vector search results into your agent’s prompt to provide contextual long-term memory.
Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022
Verify ↗