/

/

/

What Is a Vector Database & How Does It Work? Use Cases and Agent Memory

Share

What is Vector Database?

What is Vector Database?

What is Vector Database?

aashi dutt
Published on Oct 9, 2026

·

25 min read

vector database
vector database

On this page

A vector database is a specialized data system that stores and searches high-dimensional vectors, usually generated by embedding models, to enable similarity search instead of exact matching. It is a core building block for retrieval-augmented generation, semantic search, and many production AI agents.

AI engineers building agents face a different problem: not just retrieving similar items, but maintaining long-term, user-specific memory across sessions. Vector databases solve fast similarity search. A memory layer is needed on top to decide what to store, how to update it over time, and how to expose it safely to agents. That is where Mem0 fits.

What is a vector database?

A vector database stores data as numerical vectors and performs similarity search over those vectors. Each vector is typically the output of a model that encodes text, images, audio, or other content into a fixed-length array of floats. Instead of asking "what equals this exact value," the system answers "what is close to this in meaning."

In practice, a vector database accepts vectors and associated metadata, builds indexes that make nearest neighbor search efficient, and supports queries that return the most similar vectors to a given input. This structure is suited to semantic tasks where exact keywords are not enough, such as "refund" being close to "return" in customer support data.

What are vector embeddings?

Vector embeddings are dense numerical representations that capture the semantic properties of data. For text, an embedding model maps a sentence like "I need to return this product" into a high-dimensional vector, for example, a 1536-dimensional array. Sentences with similar meaning end up close together in that space.

The embedding space follows a metric structure. Distances and angles between vectors encode relationships such as similarity, topic, or sentiment. Different tasks use different embedding models, because the geometry of the space needs to match the goal. A model trained for code similarity arranges "sort list" near "order array" and far from "render template."

When a user writes "I want a refund," a good embedding model will place that vector near other refund-related phrases and support tickets. The vector database then uses that proximity to retrieve relevant past interactions, even if they do not share exact words.

How does a vector database work?

How does a vector database work

At a high level, a vector database operates in four main steps: embed-and-store, index, query, and post-process or re-rank.

Embed and store

Raw data, such as documents, chat messages, or item descriptions, is passed through an embedding model to produce vectors. These vectors are stored along with identifiers and metadata such as user_id, timestamp, tags, or source.

Index

The database builds one or more specialized indexes over the stored vectors. These indexes organize the space to make nearest neighbor queries fast, even when there are millions or billions of vectors. Approximate nearest neighbor algorithms are used to trade a small amount of accuracy for large gains in speed.

Query

At query time, input text or other content is embedded to produce a query vector. The database runs similarity search against its indexes, often with metadata filters. It returns the closest vectors and their associated payloads.

Post-process or re-rank

Retrieved candidates can be re-ranked or filtered further using metadata, recency, popularity, or custom scoring. In RAG and agent workflows, these results feed into an LLM prompt as context, sometimes after additional reranking.

This pipeline is simple conceptually, yet each step has deep engineering detail. Index design, similarity choice, metadata handling, and operational architecture all influence accuracy and latency in production agents.

Vector indexing and ANN algorithms

Indexing is central to vector database performance. The naive approach, exact K nearest neighbors (KNN), compares the query vector to every stored vector. This is accurate but scales poorly. Approximate nearest neighbor (ANN) methods build data structures that avoid scanning the entire dataset while returning neighbors that are close enough for most applications.

Common ANN techniques include:

HNSW (Hierarchical Navigable Small World)

HNSW builds a multi-layer graph where nodes represent vectors and edges connect nearby vectors. Queries start at upper layers, which are sparser, and descend to denser layers, following edges to closer neighbors. This structure yields high recall and low latency on large datasets, at the cost of more memory and complex index construction. The original HNSW paper by Malkov and Yashunin (first posted in 2016, revised in 2018 as v4) showed state-of-the-art performance for similarity search.

IVF (Inverted File Index)

IVF partitions the vector space into coarse clusters, usually via k-means. Each cluster has an inverted list of vectors assigned to it. Queries are routed to the most relevant clusters based on the query vector, and search is performed only within those lists. IVF reduces the search space significantly and can be combined with product quantization for further compression.

PQ (Product Quantization)

Product quantization splits vectors into sub-vectors and quantizes each sub-vector into discrete codes. Instead of storing full-precision floats, the index stores compact codes and uses precomputed distance tables. This enables large-scale search with reduced memory footprint and reasonable accuracy, often used in conjunction with IVF or HNSW.

LSH (Locality Sensitive Hashing)

LSH uses hash functions that map similar vectors to the same buckets with high probability. Multiple hash tables improve recall. For a query vector, only vectors in the same buckets are compared. LSH is simple, works well for some distributions, and provides probabilistic guarantees, but can require many hash tables for high-quality results.

KNN is conceptually straightforward and exact, but ANN methods are what make vector databases viable at scale. Engineers choose algorithms based on dataset size, dimensionality, latency requirements, and update patterns.

Vector similarity measures

Vector similarity measures

Similarity measures define what "close" means in vector space. The three common options are cosine similarity, dot product, and Euclidean distance.

Cosine similarity

Cosine similarity measures the cosine of the angle between two vectors. It focuses on orientation, not magnitude, and is widely used for text embeddings. Cosine similarity scores range from -1 to 1, where 1 means identical direction. For many language models, cosine similarity is the default recommendation.

Dot product

Dot product measures the sum of pairwise products of vector components. When vectors are normalized, dot product and cosine similarity are closely related. It is often used when vector magnitude carries meaning, such as in some recommendation models.

Euclidean distance

Euclidean distance measures the straight-line distance between two points. It is intuitive and suits tasks where spatial distance matters, such as some image or sensor embeddings. In a search context, similarity is often expressed as the inverse of Euclidean distance.

Selection of similarity measure should follow the embedding model's documentation. For normalized vectors, cosine similarity, dot product, and Euclidean distance induce the same ranking. OpenAI's embedding models, for example, produce normalized vectors, so cosine and Euclidean give identical results (see the OpenAI embeddings guide). In non-normalized settings, such as using Euclidean distance on embeddings tuned and scaled for cosine similarity, retrieval quality can drop.

Metadata filtering and hybrid queries

Metadata filtering and hybrid queries

Metadata is critical for AI agents. It gives structure to otherwise dense embeddings and allows queries that respect user identity, time, and other constraints. Vector databases support metadata filtering and hybrid queries that combine semantic similarity with structured conditions.

Two common patterns are:

Pre-filtering

The system applies metadata constraints before similarity search. For example, restricting search to user_id = "alice" ensures that only Alice's memories are considered, and then nearest neighbors are found within that subset. This reduces the candidate set and improves privacy and relevance. The tradeoff is that strict pre-filtering can miss globally relevant results and add overhead when filters are complex.

Post-filtering

The system runs similarity search on a broader set, then filters results afterwards by metadata conditions. This is helpful when the filter and the semantic condition are both important, or when indexes are not partitioned by the filter attribute. The tradeoff is additional overhead, because some irrelevant results need to be retrieved and then discarded during filtering.

Hybrid queries can combine keyword search and vector similarity. For example, an agent may search for customer support tickets that mention "refund" with high semantic similarity and require status = "open". In production, user-based filtering, such as user_id, is central to agent memory and aligns directly with how Mem0 organizes long-term data.

For a deeper comparison of semantic, keyword, recency, and hybrid approaches, see memory retrieval strategies for AI agents.

Vector database architecture

Vector database architecture extends traditional database concepts to support high-dimensional search at scale. Typical components include storage, indexing, query engine, ingestion, and operational tooling such as sharding, replication, monitoring, access control, and backups.

Storage layer

Stores raw vectors, metadata, and any associated payloads such as full documents. It may use disk-based storage, memory-mapped files, or log-structured engines, and is often designed to support high write throughput and compaction.

Indexing layer

Maintains ANN structures such as HNSW graphs, IVF lists, or other specialized indexes. It needs to handle incremental updates when vectors are inserted or deleted, and may support background index builds and rebuilds.

Query engine

Accepts queries over vectors and metadata, routes them to the right indexes and shards, performs similarity search, and assembles results. It often exposes APIs such as REST or gRPC that agent frameworks call directly.

Ingestion and batch pipelines

Embedding generation typically occurs outside the database, then vectors are ingested via streaming or batch processes. Some systems offer integrated embedding services, while others expect the client to handle embedding generation.

Operationally, production vector databases support:

Sharding and replication

Sharding splits data across nodes for scalability. Replication provides fault tolerance and read scaling. Engineers need to choose shard keys, often based on semantic partitions or user groups.

Monitoring and observability

Metrics such as query latency, recall, index build times, and resource usage are crucial. Vector systems introduce new failure modes, such as index corruption or skewed partitions, that need observability.

Access control and security

Authentication, authorization, and encryption protect sensitive embeddings, especially user-specific data. Role-based access, tenant isolation, and audit logging are key for agent memory systems.

Backups and restore

Snapshotting indexes and storage, and supporting restore to a point in time, matters for compliance and disaster recovery. Vector indexes can be large, so backup strategies must be efficient.

These architectural elements ensure that the core similarity capabilities are usable in production environments.

Vector database vs traditional database

Vector database vs traditional database

Traditional databases and vector databases solve different problems. The table below summarizes key differences.

Aspect

Traditional database

Vector database

Data model

Rows, tables, documents, key-value pairs

High-dimensional vectors with metadata

Query type

Exact match, ranges, joins, aggregations

Similarity search, nearest neighbors

Primary question

"What equals this?"

"What is close to this?"

Indexing

B-tree, hash, inverted indexes

ANN structures such as HNSW, IVF, PQ, LSH

Use cases

Transactions, reporting, OLTP, OLAP

Semantic search, RAG, recommendations, anomaly detection

Consistency focus

ACID, transactional guarantees

Often eventual consistency for vector indexes

Query language

SQL, NoSQL APIs

Custom APIs for similarity, often with filters

Scalability pattern

Horizontal scaling via sharding and replicas

Horizontal scaling with index-aware sharding

Stored content

Raw data, structured records

Vectors plus metadata, sometimes raw content

Optimization targets

Joins, aggregations, transactional throughput

Vector distance computations and recall

In practice, production systems often combine both, using a traditional database for transactional data and a vector database for semantic retrieval.

Vector database vs vector index (FAISS)

Vector indexes such as FAISS provide efficient similarity search over vectors, but they are libraries or components rather than full databases. They handle storage of vectors in memory or files, build ANN structures, and execute nearest neighbor queries.

A vector database adds data management and operational features around those indexes. It supports metadata filtering, real-time updates, multi-tenant access, sharding, replication, backups, and security controls. Engineers can embed FAISS into custom systems, but a vector database reduces the amount of infrastructure code needed for production.

Vector support in general-purpose databases

General-purpose databases have started to add vector support, bridging structured data and semantic search. PostgreSQL, for example, offers the pgvector extension that lets users store embeddings in a vector type and run similarity queries directly in SQL.

Search engines and document stores also integrate vector capabilities. They allow combined queries that mix full-text search, filters, and vector similarity within a single system. This makes it easier to add basic semantic search without deploying a separate vector database.

These extensions are useful for smaller projects or workloads where relational features and vector search are tightly coupled. For very large datasets or highly latency-sensitive agent workloads, dedicated vector databases are often still chosen.

Vector database use cases

Vector databases support several core AI use cases. For engineers building agents, these use cases often converge on retrieval, personalization, and anomaly detection.

Retrieval-augmented generation (RAG)

RAG systems retrieve relevant documents or snippets based on embeddings and feed them into an LLM prompt. The vector database holds the knowledge base, and similarity search selects context for each generation. For example, a support bot can answer user questions by retrieving help center articles and FAQs from a vector database, then summarizing them in natural language. This reduces hallucinations and connects agents to external sources.

RAG retrieves knowledge for an answer, while persistent memory retains useful information across interactions; see RAG vs. AI memory.

Semantic and multimodal search

Users can search with natural language across text, images, audio, or mixed content. Embeddings align different modalities into comparable spaces. A query like "red shoes for running" can retrieve products even if descriptions vary widely.

Recommendations

Items and users can be embedded into a common space, letting similarity drive recommendations. Closest items to a user's interaction history reflect related products, content, or courses based on behavior, not just co-occurrence.

Anomaly and fraud detection

Normal behavior patterns form dense clusters in embedding space. Outliers and unusual vectors can signal anomalies or potential fraud. Similarity search helps identify close past cases or cluster membership.

Conversational AI

Vector databases store prior chat turns, FAQs, and support logs as embeddings. Retrieval provides context for responses, lets agents surface similar past solutions, and supports personalization by user segment.

AI agent memory

For agents, memories are not just documents. They include user preferences, habits, commitments, and facts discovered over time. A vector database can store these embeddings, but deciding what constitutes a memory, how it evolves, and how it is scoped per user is a separate layer. Mem0 addresses that, and the next sections explore where vector databases stop, and memory begins.

Benefits of vector databases

Vector databases provide several practical benefits for AI workloads.

Speed and scalability

ANN indexes allow high-throughput similarity search on millions or billions of vectors with low latency. Horizontal scaling and sharding keep performance consistent as data grows.

Flexibility of representation

Embeddings can encode text, images, audio, code, and combinations of those. A single vector database can support multimodal search and discovery.

Improved relevance

Semantic search finds items that are conceptually related, not just keyword-matched. This boosts relevance for natural language queries and fuzzier tasks.

Data management features

Compared to raw indexes, vector databases provide APIs, metadata support, access control, monitoring, and operational tooling required in production.

When a vector database is not the right tool

Vector databases are not ideal for every task. Traditional transactional workloads, such as banking ledgers or inventory systems, still rely on relational or document databases with strict consistency and complex joins.

If a dataset is small enough to fit comfortably in memory and the application is simple, a lightweight vector library or a database extension like pgvector may suffice. For deterministic, rule-based queries that do not involve semantic similarity, vector search is unnecessary and can complicate the design. Engineers should align the choice of tools with the actual retrieval needs.

Technical limits also matter:

  • Embeddings are assumed to be stable and meaningful for the task. If the embedding model changes frequently, or if the semantics of the space shift, indexes may need to be rebuilt and existing vectors re-embedded, which can be expensive at scale.

  • ANN algorithms trade exactness for speed. For tasks that demand perfect nearest neighbors, such as safety-critical systems or some scientific applications, approximate methods may not be acceptable. Engineers must tune recall and validate retrieval quality.

  • Metadata filtering and user-scoped queries rely on accurate metadata. If user_id labels are wrong or missing, agent memory retrieval can mix identities. The database does not enforce semantic correctness of metadata.

Types of vector databases and how to choose

Vector systems come in several forms, each with trade-offs.

Standalone vector databases

Purpose-built systems that focus on similarity search, indexes, and distributed architecture. They are suited to large-scale RAG and search workloads.

Open-source libraries and servers

Embedding and ANN libraries, along with self-hosted vector services, provide flexibility and control. They require more operational work but allow deep customization.

Database extensions such as pgvector

Add vector types and similarity functions to relational databases. They are ideal when structured queries and vector search must live together, or for moderate-scale deployments.

Search engines with vector support

Systems that combine inverted indexes, full-text search, and vector capabilities. They handle complex queries that mix keywords, filters, and semantic similarity.

Serverless and managed offerings

Hosted vector services that abstract infrastructure and provide usage-based pricing. They reduce operational overhead for teams focused on application logic.

Selection depends on data volume, latency requirements, existing infrastructure, and team expertise. For example:

  • Small, integrated workload with strong relational needs: PostgreSQL with pgvector.

  • Large-scale RAG or recommendation system: a standalone vector database.

  • Mixed keyword and semantic search with complex filters: a search engine with vector support.

Mem0 is designed to sit on top of several vector stores. In self-hosted and open-source setups, it supports integrations with popular systems such as Qdrant (the default provider as a library when no config is provided), Elasticsearch, Chroma, Pinecone, Weaviate, Milvus, OpenSearch, PostgreSQL with pgvector, and others documented in the Mem0 vector database overview. The TypeScript SDK currently supports a subset of vector stores (Qdrant, Redis, Valkey, Vectorize, and an in-memory provider).

In the self-hosted Docker server, the default backend is PostgreSQL with pgvector, which provides a relational foundation alongside vector search.

Where vector databases stop helping AI agents

Vector databases excel at storing and retrieving embeddings. For AI agents, memory involves more than similarity search. Several gaps appear when agents rely only on raw vector databases.

No memory selection

Agents produce many tokens and interactions. Not every message or document should become a persistent memory. Vector databases accept whatever vectors are pushed, but they do not decide which content is worth remembering or how long it should be kept.

No memory types or retention rules

Short-term context, long-term preferences, task-specific notes, and ephemeral facts differ in how they are retained. Vector databases treat vectors uniformly. There is no built-in notion of "forget this after a day," "keep this until revoked," or "cache this only for the current task."

No identity or privacy logic

Engineers must ensure that an agent retrieves only the memories for the current user or tenant, and must handle consent, deletion, and policy enforcement. Vector databases offer basic metadata filters, but they do not manage identities or policy logic by themselves.

No composite ranking

Agents might prioritize recent interactions, explicit preferences, or high-confidence observations over older or vague memories. Implementing composite ranking logic directly on top of a vector database is possible but requires additional code and state management.

In short, vector databases are powerful for retrieval, but they operate at the level of vectors and indexes. Agent memory is a product-level concern that spans what to store, how to update, how to segment by user, and how to expose memories to the agent safely and effectively. This layer is where Mem0 focuses. For a focused, side-by-side breakdown, read vector databases vs. memory layers for AI agents.

How Mem0 adds agent memory on top of a vector database

How Mem0 adds agent memory on top of a vector database

Mem0 acts as an intelligent memory layer for LLMs and agents, sitting on top of vector databases and embedding services. It addresses the questions that raw vector systems leave open: what should be remembered, how it should be structured, and how agents should access it.

Key responsibilities of Mem0 include:

Automatic memory extraction

Mem0 analyzes conversations and interactions to identify meaningful facts, preferences, and commitments. Instead of storing every token, it decides which pieces are worth persisting as memories.

User-scoped memory management

Memories are associated with user identifiers, so retrieval naturally respects identity boundaries. Queries can filter by user_id and other metadata, and Mem0 organizes memory around individuals or entities.

Memory updates and deduplication

When new messages arrive, Mem0 checks related existing memories so it avoids storing the same fact twice, then deduplicates and embeds what it keeps. Mem0's extraction uses a single-pass, ADD-only algorithm: every extracted fact becomes an independent record rather than overwriting existing entries. If a user says they moved from Austin to Seattle, the new fact lives alongside the old one, preserving the full history of state changes. When your application needs to correct or remove a memory, use the explicit update and delete operations. For details on the ADD-only algorithm and its benchmark results, see Token-Efficient Memory Algorithm Introdcution.

Where memories live

Mem0 stores the parts of a memory in different stores. A SQL database holds facts and metadata, a vector database holds embeddings for semantic search, and an entity store holds the entities extracted from memory text. On the hosted platform, these stores are managed for you. In self-hosted and open-source deployments, you choose and operate them through configuration, including which vector database to use.

Retrieval beyond similarity

On the hosted platform, search ranks stored memories using several signals in parallel: semantic similarity over embeddings, keyword matching, entity matches, and time. This multi-signal retrieval fuses the results so that different queries lean on different signals automatically. In the new algorithm, this approach achieves 92.5 on LoCoMo, 94.4 on LongMemEval, and 64.1/48.6 on BEAM (1M/10M) while averaging under 7,000 tokens per retrieval call. Full-context approaches on the same benchmarks routinely consume 25,000+ tokens. The full evaluation framework is open-source at the Mem0 memory benchmarks. Open-source retrieval depends on your configured vector store and an optional reranker. Scoping every search with user_id, agent_id, or run_id keeps memories from different users, agents, and sessions from mixing.

For AI engineers, this means that they can focus on agent behavior and prompts while delegating memory concerns such as extraction, structuring, and retrieval to Mem0. The vector database remains the infrastructure layer, and Mem0 becomes the interface that agents use to think about memory.

For more details on how Mem0 structures memories, see the architecture overview.

Example: Mem0 hosted platform with a managed vector store in Python

The following Python example shows how an agent can use the hosted Mem0 platform to read and write memories while relying on OpenAI for LLM responses. In this mode, Mem0 manages the underlying vector store for you as part of the SaaS platform, so you do not configure the vector database directly.

It follows the Mem0 quickstart.

import os
from mem0 import MemoryClient
from openai import OpenAI

os.environ["MEM0_API_KEY"] = "YOUR_MEM0_API_KEY"
os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"

memory = MemoryClient()
llm = OpenAI()

def chat_with_memory(user_id: str, message: str) -> str:
    # 1. Retrieve relevant memories (semantic search over embeddings)
    results = memory.search(message, filters={"user_id": user_id})
    memories = [r["memory"] for r in results.get("results", [])]
    context = "\n".join(f"- {m}" for m in memories) or "No prior memories."

    # 2. Generate a reply with the memories as context
    completion = llm.chat.completions.create(
        model="gpt-4.1-mini",
        messages=[
            {
                "role": "system",
                "content": "You are a helpful assistant. "
                           f"Known user memories:\n{context}",
            },
            {"role": "user", "content": message},
        ],
    )
    reply = completion.choices[0].message.content

    # 3. Store the exchange. Mem0 decides what is worth remembering.
    memory.add(
        [
            {"role": "user", "content": message},
            {"role": "assistant", "content": reply},
        ],
        user_id=user_id,
    )
    return reply

if __name__ == "__main__":
    user = "user-123"
    print(chat_with_memory(user, "Hi, my favorite language is Python."))
    print(chat_with_memory(user, "What language did I say I like?"))
import os
from mem0 import MemoryClient
from openai import OpenAI

os.environ["MEM0_API_KEY"] = "YOUR_MEM0_API_KEY"
os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"

memory = MemoryClient()
llm = OpenAI()

def chat_with_memory(user_id: str, message: str) -> str:
    # 1. Retrieve relevant memories (semantic search over embeddings)
    results = memory.search(message, filters={"user_id": user_id})
    memories = [r["memory"] for r in results.get("results", [])]
    context = "\n".join(f"- {m}" for m in memories) or "No prior memories."

    # 2. Generate a reply with the memories as context
    completion = llm.chat.completions.create(
        model="gpt-4.1-mini",
        messages=[
            {
                "role": "system",
                "content": "You are a helpful assistant. "
                           f"Known user memories:\n{context}",
            },
            {"role": "user", "content": message},
        ],
    )
    reply = completion.choices[0].message.content

    # 3. Store the exchange. Mem0 decides what is worth remembering.
    memory.add(
        [
            {"role": "user", "content": message},
            {"role": "assistant", "content": reply},
        ],
        user_id=user_id,
    )
    return reply

if __name__ == "__main__":
    user = "user-123"
    print(chat_with_memory(user, "Hi, my favorite language is Python."))
    print(chat_with_memory(user, "What language did I say I like?"))
import os
from mem0 import MemoryClient
from openai import OpenAI

os.environ["MEM0_API_KEY"] = "YOUR_MEM0_API_KEY"
os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"

memory = MemoryClient()
llm = OpenAI()

def chat_with_memory(user_id: str, message: str) -> str:
    # 1. Retrieve relevant memories (semantic search over embeddings)
    results = memory.search(message, filters={"user_id": user_id})
    memories = [r["memory"] for r in results.get("results", [])]
    context = "\n".join(f"- {m}" for m in memories) or "No prior memories."

    # 2. Generate a reply with the memories as context
    completion = llm.chat.completions.create(
        model="gpt-4.1-mini",
        messages=[
            {
                "role": "system",
                "content": "You are a helpful assistant. "
                           f"Known user memories:\n{context}",
            },
            {"role": "user", "content": message},
        ],
    )
    reply = completion.choices[0].message.content

    # 3. Store the exchange. Mem0 decides what is worth remembering.
    memory.add(
        [
            {"role": "user", "content": message},
            {"role": "assistant", "content": reply},
        ],
        user_id=user_id,
    )
    return reply

if __name__ == "__main__":
    user = "user-123"
    print(chat_with_memory(user, "Hi, my favorite language is Python."))
    print(chat_with_memory(user, "What language did I say I like?"))

To run this example, you need valid Mem0 and OpenAI API keys configured as environment variables or via your secrets manager.

Example: Mem0 self-hosted with an explicit Qdrant backend in Python

To show how backend choice works in open-source and self-hosted mode, the following Python snippet configures Mem0 with Qdrant as the vector database. Other providers, including Chroma and PostgreSQL with pgvector, use the same config shape with their own settings listed in the vector database docs. In self-hosted mode, you can choose, swap, or scale the underlying vector database as needed.

import os
from mem0 import Memory

os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"

config = {
    "vector_store": {
        "provider": "qdrant",
        "config": {
            "collection_name": "test",
            "host": "localhost",
            "port": 6333,
        },
    }
}

m = Memory.from_config(config)
m.add("Likes to play cricket on weekends", user_id="alice", metadata={"category": "hobbies"})
import os
from mem0 import Memory

os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"

config = {
    "vector_store": {
        "provider": "qdrant",
        "config": {
            "collection_name": "test",
            "host": "localhost",
            "port": 6333,
        },
    }
}

m = Memory.from_config(config)
m.add("Likes to play cricket on weekends", user_id="alice", metadata={"category": "hobbies"})
import os
from mem0 import Memory

os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"

config = {
    "vector_store": {
        "provider": "qdrant",
        "config": {
            "collection_name": "test",
            "host": "localhost",
            "port": 6333,
        },
    }
}

m = Memory.from_config(config)
m.add("Likes to play cricket on weekends", user_id="alice", metadata={"category": "hobbies"})

In this self-hosted configuration:

  • The vector_store block chooses Qdrant as the backend and points Mem0 at the running Qdrant instance.

  • As a library, Mem0 defaults to a local Qdrant vector store when no config is given in open-source setups.

  • In the Docker server, the default backend is PostgreSQL with pgvector.

  • To run this example, you need a running Qdrant instance reachable at localhost:6333 and a valid OpenAI API key.

Engineers who want to self-host Mem0 can follow the open-source overview and the vector database configuration details.

How does Mem0 work with an existing vector database?

Mem0 can work in two modes:

  • On the hosted Mem0 platform, the service manages the underlying vector store for you. You interact via the MemoryClient or HTTP API, and do not configure the backend directly.

  • In self-hosted and open-source deployments, Mem0 connects to supported vector backends and uses them for storage and search, while handling embedding generation and memory logic itself. Engineers integrate Mem0 via its SDK or API, configure the desired backend (for example, Qdrant, Chroma, or pgvector), and then call add and search against Mem0 rather than managing vectors directly. In this mode, you can swap or scale the underlying vector database without changing how the agent interacts with memory.

Summary

Vector databases provide the core infrastructure for similarity search over embeddings, making RAG, semantic search, and recommendations practical at scale. For AI agents, similarity search alone is not enough — agents need user-scoped, evolving memories. Mem0 adds that layer on top, handling extraction, deduplication, and scoped retrieval while using vector stores underneath.

Frequently Asked Questions

What is a vector database in AI?

A vector database in AI is a system that stores embeddings and performs similarity search to find data points that are close in meaning to a query. It uses high-dimensional vectors instead of exact values, and supports use cases like semantic search, RAG, and recommendations where "closeness" matters more than exact matches.

How does a vector database work with vector embeddings?

Applications generate embeddings from text, images, or other data, then store those vectors and metadata in the database. The system builds indexes over the vectors, and at query time embeds the input, runs nearest neighbor search, and returns the most similar items. Post-processing can re-rank results by recency or filters, and feed them into an LLM.

What is the difference between a vector database and a vector index like FAISS?

A vector index like FAISS provides data structures and algorithms for nearest neighbor search, usually as a library. It focuses on storing vectors and running similarity queries in a process. A vector database adds APIs, metadata filtering, updates, scaling, access control, monitoring, and backups, making similarity search a managed service rather than an embedded component.

Do I need a vector database for RAG?

For RAG systems at scale, a vector database is usually needed to support fast, consistent retrieval from large knowledge bases. Smaller projects can start with an in-memory library or a database extension and still achieve good performance. As document volume and query load grow, moving to a dedicated vector database becomes important for latency and reliability.

Can PostgreSQL be used as a vector database?

PostgreSQL can act as a vector store when extended with pgvector, which adds a vector type and similarity functions. This allows storing embeddings and running similarity queries directly in SQL, alongside relational data. It suits applications that combine structured queries and semantic retrieval, or that prefer to keep everything in one database.

Which similarity measure should be used?

Most text embedding models recommend cosine similarity. Dot product is common when magnitude carries meaning. Euclidean distance fits tasks where spatial distance matters. Model documentation usually specifies the preferred metric.

When should AI engineers use a vector database instead of a traditional database?

A vector database is useful when queries must find items similar in meaning, not just exact matches or ranges. Examples include semantic search, RAG over large corpora, and personalization based on embeddings. Traditional databases remain the right choice for transactional workloads, complex joins, and reporting where similarity search is not required.

Why is Mem0 needed if a vector database already stores embeddings?

A vector database stores embeddings and runs similarity search, but it does not decide what to remember, how to organize memories by user, or how to keep them free of duplicates. Mem0 adds a layer that extracts meaningful facts from interactions, deduplicates them, scopes them to identities such as user_id, and retrieves them for agents through a simple API. In self-hosted mode, it runs on top of the vector store you choose. On the hosted platform, the storage is managed for you.

Further Reading

Sources

Start building with memory

Wire persistent memory into your own agent in about 15 minutes. Free tier, no credit card.

Share on:

aashi dutt

Aashi Dutt

She is a senior technical content writer at Mem0. She covers agent memory architecture and the engineering decisions behind building agents that actually remember. She experiments with new features and turns research into posts developers can put straight to use.

Start building with memory

Free tier, no card