keepcalculator

06 Numbers and Powers

AI Agent Memory: Database Choice Guide

Card No. 06·16957 · Updated · Standard formula, shown below

Estimated storage (GB) 0.000954 GB
Estimated query latency (ms)
6.0 ms
Result
Number of memories
1000
You entered
Average memory size (KB)
1 KB
You entered
You enter
Number of memories, Average memory size (KB), Queries per second (QPS)
You get
Estimated storage (GB), Estimated query latency (ms)
Rounding
2 dp · 1 dp

Choosing a database for AI agent memory retrieval and state management depends on your access patterns: vector databases excel at semantic similarity search for long-term memory, while key-value stores like Redis are better for fast, exact-match state retrieval. This article compares the main options—vector databases, relational databases, key-value stores, and graph databases—and explains how to match them to your agent’s needs.

Formula

The calculator estimates storage requirements and a rough query latency for an AI agent memory system. Storage (GB) = (number of memories × average memory size in KB) / 1,048,576 (KB per GB). Estimated latency (ms) = 5 + (queries per second / 10), a simplified linear model assuming a typical vector index.

  • t – total number of memories (e.g., conversation snippets, embeddings).
  • d – average size of each memory in kilobytes (including text and embeddings).
  • q – expected queries per second (QPS) for memory retrieval.
  • storage – total storage needed in gigabytes.
  • latency – estimated average query latency in milliseconds (simplified).

Worked Example

Using the defaults: 1,000 memories, 1 KB each, 10 QPS.

  1. Storage = (1000 × 1) / 1,048,576 = 0.00095 GB ≈ 0.00 GB (rounds to 0.00).
  2. Latency = 5 + (10 / 10) = 6 ms.

The calculator shows 0.00 GB and 6 ms, matching the formula.

Background: What AI Agent Memory Actually Is

AI agents (like chatbots with tools, autonomous coding assistants, or personal assistants) need two distinct types of memory:

  • State – the current session’s variables, conversation context, and task progress. This is short-lived, frequently updated, and requires low-latency reads/writes.
  • Long-term memory – facts, user preferences, and past interactions that persist across sessions. This is often stored as embeddings (vector representations) for semantic search.

The database choice hinges on which of these you prioritize. Many production systems use a hybrid: a key-value store for state and a vector database for long-term memory.

How Memory Retrieval Works in Practice

When an agent needs to recall something, it typically:

  1. Converts the current query into an embedding using a model like text-embedding-3-small.
  2. Searches the vector database for similar embeddings (e.g., cosine similarity).
  3. Retrieves the top-k results and injects them into the LLM context.

For state, the agent simply reads/writes a key-value pair (e.g., session_123: current_step=3). The latency requirements differ: state reads must be under 10ms, while memory retrieval can tolerate 50-200ms.

Database Options Compared

Here’s a practical comparison of the main categories:

Type Best For Examples Strengths Weaknesses
Vector DB Semantic memory retrieval Pinecone, Weaviate, Qdrant Fast similarity search, scalable Not ideal for exact filters or transactions
Key-Value Store State, session data Redis, Memcached Ultra-low latency, simple No complex queries
Relational DB Structured facts, relationships PostgreSQL, MySQL ACID, SQL, joins Slower for vector search unless extension used
Graph DB Entity relationships Neo4j, Amazon Neptune Traversals, relationship queries Overkill for simple memory

PostgreSQL with pgvector is a popular middle ground: it supports both relational and vector queries, reducing infrastructure complexity.

How to Choose: Key Factors

  • Access pattern: If you need semantic search (find similar memories), vector DB is required. If you need exact state lookups, key-value is fine.
  • Scale: For <100k memories, even SQLite with a vector extension works. For millions, consider dedicated vector DBs.
  • Consistency: If state must be transactional (e.g., multi-step task), relational DBs are safer.
  • Latency budget: State reads need <10ms; memory retrieval can be slower.
  • Operational complexity: Running a separate vector DB adds overhead. A single PostgreSQL instance might be simpler.

Common Problems and Misconceptions

Misconception: “Vector DBs are always faster.� For exact key lookups, Redis is faster than any vector DB. Vector search is only needed for similarity, not exact matches.

Problem: Embedding drift. If you update your embedding model, old vectors become incompatible. Plan for re-embedding or versioning.

Problem: State loss on crashes. Redis without persistence loses data. Use AOF or switch to a database with durability if state is critical.

Problem: Over-fetching context. Retrieving too many memories can exceed the LLM’s context window. Use top-k and relevance thresholds.

Costs and Performance Trade-offs

Costs vary widely: open-source self-hosted (e.g., Qdrant, PostgreSQL) have infra costs, while managed services (Pinecone, Redis Cloud) charge per GB and QPS. For small prototypes, SQLite with sqlite-vec is free and sufficient. Performance-wise, vector search latency grows with the number of vectors; approximate nearest neighbor (ANN) indexes (HNSW, IVF) keep it sub-100ms up to millions of vectors, but exact search (brute force) becomes slow.

Safety and Accuracy Considerations

When storing agent memory, consider privacy: user data in memories may contain sensitive information. Encrypt at rest and in transit. Also, ensure you have a data retention policy—delete memories when no longer needed. For state, avoid storing secrets or tokens in plain text. Always validate retrieved memories before injecting into prompts to prevent prompt injection attacks.

Frequently Asked Questions

Can I use a single database for both state and memory? Yes, PostgreSQL with pgvector can handle both relational state and vector search, but for very high QPS state access, a dedicated key-value store is better.

What is the best database for a small AI agent project? For a prototype, SQLite with sqlite-vec or PostgreSQL with pgvector is ideal—simple, free, and supports both state and memory. Only scale to dedicated vector DBs when you hit performance limits.

How do I choose between Pinecone and Weaviate? Pinecone is fully managed and simple, while Weaviate offers more control and can be self-hosted. Choose based on your need for customization and data residency.

Is Redis good for long-term memory? Redis is not designed for vector search (though Redis Stack has vector support). It’s better for short-term state. For long-term semantic memory, use a vector DB.

How do I handle memory eviction? Use a combination of recency and importance scores. For example, store a timestamp and a priority field, and periodically prune old or low-relevance memories.

What is the typical latency for vector search? With HNSW index, expect 10-50ms for 1M vectors on a single node. For 100M vectors, it can be 100ms+. Exact search is slower.

Related

Formula used
in t | Number of memories | - | 1000 | 1 | 1000000
in d | Average memory size (KB) | KB | 1 | 0.1 | 10000
in q | Queries per second (QPS) | - | 10 | 1 | 100000
out storage | Estimated storage (GB) | GB | t * d / 1048576 | 2
out latency | Estimated query latency (ms) | ms | 5 + q / 10 | 1

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →