TencentDB Agent Memory Retrieval Methods: BM25, Vector Search, and RRF Explained

TencentDB Agent Memory combines BM25 sparse keyword search, dense vector similarity search via Tencent Vector DB, and Reciprocal Rank Fusion (RRF) to deliver high-recall retrieval for AI agents.

The TencentDB Agent Memory system implements a sophisticated hybrid retrieval pipeline that leverages both lexical and semantic search techniques. By integrating classical BM25 keyword matching with neural embedding-based similarity search, the system ensures comprehensive coverage of exact matches and conceptual relevance. Understanding these TencentDB Agent Memory retrieval methods is essential for developers optimizing agent knowledge retrieval.

The Three Core Retrieval Methods

The architecture employs three complementary techniques to surface the most relevant knowledge for any given query.

The system implements BM25 (Best Match 25) through SQLite FTS5 full-text search for precise lexical matching. This classical algorithm uses term frequencies and inverse document frequencies to rank documents based on exact keyword overlap.

The implementation resides in MemoryCore/src/core/tools/memory-search.ts, where queries are constructed and executed against the local SQLite store. This approach excels at matching specific identifiers, technical terms, and exact phrases present in the stored memories.

Dense Vector Search via Tencent Vector DB

For semantic similarity, the system utilizes Tencent Vector DB (TCVDB) to perform K-Nearest-Neighbour (KNN) lookups on dense embeddings. When a query embedding is supplied, the vector backend retrieves contextually similar content even when keywords differ.

The vector operations are handled in MemoryCore/src/core/store/tcvdb.ts, which interfaces with the TCVDB backend to fetch semantically related candidates based on vector distance metrics.

Reciprocal Rank Fusion (RRF)

To combine the strengths of both retrieval paradigms, the system employs Reciprocal Rank Fusion through the rrfMerge function in MemoryCore/src/core/store/search-utils.ts. This algorithm merges ranked lists from BM25 and vector search into a single, unified ranking.

The fusion uses the standard RRF formula with a constant k = 60 (defined as RRF_K), calculating scores as 1 / (k + rank + 1) for each result. Items appearing in both lists receive aggregated scores, boosting documents that match both lexically and semantically.

How the Hybrid Pipeline Works

The retrieval process follows a three-stage pipeline that maximizes recall before applying the fusion algorithm.

Over-Retrieval Phase

Both retrieval engines initially return oversized candidate sets. The BM25 engine (SQLite FTS5) and the vector engine (TCVDB) each fetch a topK of approximately 100 candidates—significantly larger than the final result set. This over-retrieval ensures high recall, capturing relevant items that might rank moderately in one modality but highly in the other.

RRF Merging and Scoring

The rrfMerge function processes both ranked lists simultaneously. For each candidate, it calculates the reciprocal rank score and sums these values when a record appears in both the BM25 and vector results. This scoring mechanism naturally favors items that demonstrate both keyword relevance and semantic similarity.

The merged results are then sorted by the computed rrfScore in descending order.

Final Selection

The system truncates the fused list to the desired output size, typically returning the top-N results with the highest combined RRF scores. This final ranked list is exposed to downstream components through functions like memory-search.ts and conversation-search.ts.

Source Code Architecture

The implementation spans several key files within the MemoryCore/src/core directory:

  • tools/memory-search.ts – Orchestrates the hybrid search workflow, invoking both BM25 and vector searches before calling rrfMerge to combine results.

  • tools/conversation-search.ts – Implements identical hybrid logic specifically for conversation-level queries, utilizing the same RRF merging strategy.

  • store/search-utils.ts – Contains the core rrfMerge implementation and the RRF_K constant (set to 60) used in the fusion formula.

  • store/tcvdb.ts – Manages the vector backend integration, providing KNN retrieval capabilities when dense embeddings are available.

  • hooks/auto-recall.ts – Implements intelligent routing logic that determines whether to use pure BM25, pure vector search, or the full hybrid RRF pipeline based on query characteristics.

Practical Implementation Example

The following TypeScript pattern reflects the actual flow used in the agent's search pipeline:

import { rrfMerge } from './store/search-utils';
import { searchByFTS, searchByVector } from './tools/memory-search';

// 1️⃣ Perform sparse (BM25) search via SQLite FTS5
const ftsResults = await searchByFTS(query, { topK: 100 });

// 2️⃣ Perform dense (vector) search via TCVDB KNN
const vecResults = await searchByVector(queryEmbedding, { topK: 100 });

// 3️⃣ Merge the two ranked lists with RRF using record_id as the unique key
const hybridResults = rrfMerge(
  [ftsResults, vecResults],
  (item) => item.record_id,
);

// Results are now sorted by combined RRF score (rrfScore property)
const topResults = hybridResults.slice(0, 10);
console.log(topResults);

This implementation demonstrates how rrfMerge accepts multiple result arrays and a key extractor function, returning a unified array where each item contains the aggregated rrfScore used for final ranking.

Summary

TencentDB Agent Memory retrieval methods provide a robust hybrid architecture that balances precision and semantic understanding:

  • BM25 via SQLite FTS5 delivers exact keyword matching for technical terms and identifiers.
  • Vector search via TCVDB KNN captures conceptual similarity through dense embeddings.
  • RRF fusion combines both modalities using the rrfMerge utility with a standard k=60 constant, ensuring items ranking well in either search paradigm surface in final results.

This multi-stage approach ensures agents retrieve both explicitly mentioned concepts and contextually relevant information, significantly improving downstream LLM reasoning quality.

Frequently Asked Questions

What is the RRF formula used in TencentDB Agent Memory?

The system uses the standard Reciprocal Rank Fusion formula score = 1 / (k + rank + 1) with a constant k = 60 (defined as RRF_K in search-utils.ts). When a document appears in both BM25 and vector result lists, its scores from each list are summed to produce the final rrfScore used for ranking.

The auto-recall.ts hook analyzes query content to determine the retrieval strategy. While the system defaults to hybrid RRF merging for maximum recall, it can route queries to pure BM25 (SQLite FTS5) when only keyword precision is needed, or to pure vector search when semantic similarity is prioritized over exact lexical matches.

What vector database powers the semantic search component?

TencentDB Agent Memory uses Tencent Vector DB (TCVDB) as the backend for dense embedding retrieval. The tcvdb.ts file handles K-Nearest-Neighbour lookups, performing vector similarity calculations to fetch semantically related memory records when query embeddings are provided.

How are duplicate records handled during RRF merging?

The rrfMerge function in search-utils.ts accepts a key extractor function (typically (item) => item.record_id) to identify unique records across result lists. When the same record appears in both BM25 and vector results, the function aggregates their reciprocal rank scores rather than duplicating the entry, ensuring each unique memory receives a combined relevance score.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →