27.2 Retrieval: BM25, Flat and IVF Vectors, RRF, and MMR
After RAG ingest, the system has an index of the course docs and its own: chunks with token counts, fingerprints, and embeddings from the engine’s /v1/embeddings. Nothing reads it yet. Ask the agent “which hash names a KV block?” and it answers from SmolLM2’s weights, which have never seen this system, so it guesses. What the agent needs is the three chunks that answer the question, picked from hundreds in a few milliseconds. This section builds that step: given a query, return the best k chunks.
Concepts from other sections (9)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- PCG32: an LCG whose output is a permutation (xorshift, then a state-chosen rotation) of the old state. Read more: 12.3 Modular arithmetic, hashing, and PCG32 in Python in Mathematics
- Bits per byte (bpb): , total information divided by the UTF-8 length; comparable across tokenizers. Read more: 17.2 Perplexity, bits per byte, NLL accumulator in Mathematics
- Cosine similarity: the inner product divided by both lengths; depends only on direction. Read more: 9.6 Inner products, projections, cosine similarity, and top-k in Mathematics
- Dot product: ; it measures how much two vectors point the same way, and it is zero exactly when they are perpendicular. Read more: Chapter 10: Calculus 3 in Mathematics
- HNSW (hierarchical navigable small world): the dominant ANN index, a multi-layer proximity graph searched by greedy descent, tuned with
MandefSearchfor recall against latency. Read more: Chapter 27: Retrieval and RAG - Hybrid retrieval: running dense (embedding) and lexical (such as BM25) retrieval together and fusing their ranked lists, because dense search catches paraphrases and lexical search catches exact IDs, codes, and names. Read more: Chapter 27: Retrieval and RAG
- Skip list: a sorted multi-level linked list in which each element is promoted to the next level up with probability ; a search moves right, then drops down a level, giving expected search, insertion, and deletion without rebalancing. Read more: Chapter 14: Probabilistic data structures in Programming and Algorithms
- Cross-encoder: a model that reads a query and a document together and outputs one relevance score; more accurate than a bi-encoder but run per candidate pair, so it is used to rerank a short list. Read more: Chapter 27: Retrieval and RAG
- HyDE (hypothetical document embeddings): a query transform that has a model write a hypothetical answer and embeds that instead of the raw query, improving recall when queries and documents are phrased differently. Read more: Chapter 27: Retrieval and RAG
Retrieval has two halves that fail in opposite ways. Lexical search finds tl_kv_export and 30080 exactly but misses a paraphrase. Dense search matches “how is a cache entry dropped” to “eviction” but blurs identifiers. So you will build both, fuse them, and make the result diverse enough that three chunks are not three copies of one sentence. Along the way you will treat the index file as what it really is, a contract between two programs, and you will see why a filter has to run inside a search rather than after it.
This section builds on RAG ingest, which writes the index read here and owns the query embedder, and on the Go PCG32 port (in go/ds/rng) from An open-loop load generator in AI Engineering, which seeds k-means++. Useful background: Inner products, projections, cosine similarity, and top-k in Mathematics (re-implemented here in Go), the Search & Indexing chapter of Systems Engineering (bm25_from_scratch.py), and the K-Means Optimization case study. The full solution lives in go/agent/rag/retrieve/: bm25.go (Terms, BuildBM25, Scores, Search, WriteTo, ReadBM25, the varints), vector.go (Flat, KMeansPP, BuildIVF, IVF.Search), fuse.go (RRF, MMR), and retrieve.go (Open, Index.BuildIVF, Hybrid.Retrieve). The contract is the index directory of course/contracts/formats/rag-index.md plus the Go API shown later in this section.
Two entry points of the solution use this package. rag ingest (entry/go/cmd/ctl/rag.go) writes bm25.idx with BuildBM25 and WriteTo. The agent kit (entry/go/cmd/internal/agentkit/agentkit.go) opens /artifacts/rag/<index_id>/, picks the query embedder that matches meta.json’s embedding_model, and builds the Hybrid retriever behind search_docs.
Learning objectives
- Compute BM25 by hand and explain what saturation (
k1), length normalization (b), and the+1inside the idf each do.- Read and write an inverted index file with gap-encoded varint postings, defensively enough that a truncated file never panics.
- Build a reproducible IVF index with seeded k-means++ and measure its recall against flat search.
- Fuse lexical and dense lists with RRF and diversify the result with MMR, by hand and in code.
- Apply filters inside each search, before the cut to
k.
The notation
Section titled “The notation”Every formula in this section uses the same small set of symbols:
| Symbol | Meaning | Type |
|---|---|---|
| number of chunks in the index | integer | |
| a term (defined next) | string | |
| number of chunks containing | integer, | |
| occurrences of in chunk | integer | |
| terms in chunk , duplicates counted | integer | |
mean of over the index (avgdl) | float64 | |
BM25 knobs: saturation (1.2) and length normalization (0.75), from meta.json | float64 | |
| query vector and chunk vector, both L2-normalized | []float32 of length dim | |
| IVF centroid | []float32 | |
| squared distance from row to its nearest chosen seed | float64 | |
| rank of chunk in list , counting from 1 | integer | |
| RRF constant (60) | float64 | |
| MMR trade-off between relevance and novelty | float64 in |
Terms: the rule both programs share
Section titled “Terms: the rule both programs share”Retrieval and ingest are separate programs, and they must cut text into the same pieces. The contract’s rule is that a term is a maximal run of Unicode letters, digits, and _ ([\p{L}\p{N}_]+ in Go’s RE2), lowercased with strings.ToLower. Everything else separates terms. So tl_kv_export(handle) gives tl_kv_export and handle, k-means++ gives k and means, and Café gives café. An ASCII-only rule ([a-z0-9]+) would cut café into caf, and a query typed with the accent would never find it.
The inverted index and BM25
Section titled “The inverted index and BM25”With terms defined, the lexical index is an inverted index: a map from each term to its postings list, the chunks containing it, in chunk order, each with its . Scoring a query walks only the lists of its terms, never the whole index.
BM25 adds, for every query term occurrence and every chunk in its list,
Read the fraction from the inside. With it is : 1 at , then rising towards the ceiling , so the tenth “cache” adds far less than the first. That is saturation: a chunk cannot win by repeating a word. The factor is 1 for a chunk of average length and larger for a long one; a long chunk holds more words by chance, so each match counts for less.
The idf is large for a rare term and near 0 for a term in every chunk. Without the 1 + inside the logarithm (the textbook Robertson-Sparck Jones form), a term in more than half the chunks gets a negative weight, and matching it pushes a chunk down. Finally, a query term written twice counts twice ("cache cache" doubles every “cache” contribution), as in Lucene and bm25_from_scratch.py.
The index file
Section titled “The index file”bm25.idx stores the index so a restart does not rebuild it. Its layout is: magic TLBM, version, , number of terms, as f64 bits, every , then each term (sorted by its bytes) with and its postings. Postings are stored as gaps: the first entry is the chunk number, and each next one is the difference from the previous, so the numbers stay small. Each number is an unsigned LEB128 varint: 7 bits per byte, lowest group first, with the top bit set on every byte except the last. For example, 300 is binary 100101100. Its low seven bits 0101100 go first with the top bit set (0xac), then the remaining 10 (0x02), giving ac 02.
Dense vectors: flat search and IVF
Section titled “Dense vectors: flat search and IVF”The other half of retrieval is dense. vectors.f32 holds one embedding per chunk, L2-normalized, so cosine similarity is just the dot product . Flat search scores every row and keeps the top , with ties going to the lower row. It is exact, and it is the yardstick for anything faster.
An inverted file (IVF) index is the faster thing. It groups rows into nlist clusters around centroids and stores each row in the list of its nearest centroid. A query computes its distance to every centroid, scans only the nprobe nearest lists exactly, and returns the top among them. With 32 lists and nprobe = 8 it scores about a quarter of the rows. Recall@k measures what that costs: the fraction of the exact top that the approximate search also returned.
The centroids come from k-means: repeat “assign each row to its nearest centroid, then move each centroid to the mean of its rows” (Lloyd rounds). The result depends on where you start, so the start is fixed by k-means++ seeding:
- The first seed is a uniform row:
r.Below(n). - For every row , is the squared distance to the nearest seed so far.
- The next seed is row with probability : draw =
r.Float64()and take the first row, in row order, whose running sum of exceeds . - Repeat steps 2 and 3 until there are
nlistseeds.
Rows far from every seed are likely picks, so seeds spread out, and rows already chosen have and are never picked twice. Using the distance instead of its square, or always taking the farthest row, gives different seeds, and for outliers worse ones. The index stores no centroids. Instead, rag-index.md says every load builds them with PCG32 seeded by fnv1a64(index_id), so the same index always gets the same lists, on any machine and in any language that follows the spec.
Fusion and diversity
Section titled “Fusion and diversity”You now have two ranked lists that cannot be compared directly. BM25 scores are unbounded; dot products lie in . Adding them needs a calibration that changes with every query. Reciprocal rank fusion sidesteps calibration by using ranks only:
summed over the lists that contain . With , being first in one list is worth , and being in both lists beats being first in one. Ranks count from 1; counting from 0 changes every value.
The fused top often holds near-duplicates: the same paragraph in two docs, or two adjacent chunks. Maximal marginal relevance (MMR) picks one chunk at a time, each time the one that maximizes
with the max over an empty set equal to 0, so the first pick is the most relevant. is pure relevance; smaller buys diversity. The penalty is the max over everything already picked, because a candidate that copies the first pick is still a copy after the second.
Filters
Section titled “Filters”One last principle concerns scope. A filter (DocIDs, Sources) restricts which chunks may be returned, and it must run inside each search, before the cut to the candidate count. Filtering after the cut is the classic bug: the best 50 chunks overall may hold none from the requested document, and the caller gets an empty list although matching chunks exist.
Working it by hand
Section titled “Working it by hand”Start with BM25, and , over three chunks:
| Chunk | Text | Terms | |
|---|---|---|---|
| 0 | kv cache | kv, cache | 2 |
| 1 | kv cache eviction policy | kv, cache, eviction, policy | 4 |
| 2 | gateway rate limits | gateway, rate, limits | 3 |
So and . The query is cache eviction.
- : .
- : .
- Chunk 0, : length factor ; tf part . Score .
- Chunk 1, : length factor ; tf part . Score .
- Chunk 2 holds neither term: its score is 0, and it is not returned at all.
The short chunk earns more per “cache” (0.544 against 0.414), but the long one wins by also holding “eviction”. Search returns chunk 1, then chunk 0. Notice also that “cache” is in 2 of 3 chunks, so the textbook idf would have been negative.
Next, RRF with , where the lexical list is [x, y, z] and the dense list is [z, x]:
| Chunk | Lexical rank | Dense rank | RRF |
|---|---|---|---|
| x | 1 | 2 | |
| z | 3 | 1 | |
| y | 2 | none |
The fused order is x, z, y.
Finally, MMR with , query , and candidates , , . The relevances are , , , and the similarities are , , . Each step scores the remaining candidates:
| Step | Pick | |||
|---|---|---|---|---|
| 1 | (0.468) | |||
| 2 | picked | (0.16) | ||
| 3 | picked | picked | (-0.08) |
Pure relevance would order ; MMR moves the near-copy to last.
Designing the API
Section titled “Designing the API”The package API, grouped by file:
package retrieve // import "tinyllm/agent/rag/retrieve"
func Terms(text string) []stringtype Posting struct{ Doc, TF int }type BM25 struct { K1, B float64 DocLen []int AvgDL float64 Postings map[string][]Posting // sorted by Doc}func BuildBM25(texts []string, k1, b float64) *BM25func (x *BM25) N() intfunc (x *BM25) IDF(term string) float64func (x *BM25) Scores(q string) []float64func (x *BM25) Search(q string, k int, keep func(doc int) bool) []Scored // matching docs onlyfunc (x *BM25) WriteTo(w io.Writer) (int64, error)func ReadBM25(r io.Reader, k1, b float64) (*BM25, error) // errors wrap ErrFormatfunc PutUvarint(buf []byte, v uint64) []bytefunc Uvarint(buf []byte) (v uint64, n int) // n == 0: truncated or overflowtype Scored struct { Doc int; Score float64 }func TopK(c []Scored, k int) []Scored // score desc, ties to the lower Doc
// vector.gofunc Dot(a, b []float32) float64func Dist2(a, b []float32) float64func Normalize(v []float32) []float32type Flat struct{ Vecs [][]float32 }func (f *Flat) Search(q []float32, k int, keep func(doc int) bool) []Scoredfunc KMeansPP(vecs [][]float32, k int, r *rng.PCG32) ([]int, error) // ErrKfunc Nearest(v []float32, centroids [][]float32) inttype IVF struct { Vecs, Centroids [][]float32; Lists [][]int }func BuildIVF(vecs [][]float32, nlist, iters int, r *rng.PCG32) (*IVF, error)func (x *IVF) Search(q []float32, k, nprobe int, keep func(doc int) bool) []Scoredfunc Recall(approx, exact []Scored) float64
// fuse.goconst DefaultRRFK = 60func RRF(lists [][]Hit, k float64, n int) []Hitfunc MMR(q []float32, cands []Hit, vec func(index int) []float32, lambda float64, k int) []Hit
// retrieve.gotype Chunk struct { ChunkID, DocID, Source, URI, Text string; NTokens int; Fingerprint string } // docs.jsonltype Meta struct { IndexID string; NChunks, Dim int; BM25 struct{ K1, B float64 }; ... } // meta.jsontype Hit struct { Index int; ChunkID, DocID, URI, Text string; NTokens int; Score float64; Rank int }type Filter struct { DocIDs, Sources []string }func (f Filter) Keep(c Chunk) booltype Embedder interface { Embed(ctx context.Context, texts []string) ([][]float32, error) }type Retriever interface { Retrieve(ctx context.Context, q string, k int, f Filter) ([]Hit, error) }type Index struct { Meta Meta; Chunks []Chunk; Vecs [][]float32; BM25 *BM25; IVF *IVF }func Open(dir string) (*Index, error) // errors wrap ErrIndex (or ErrFormat from bm25.idx)func (x *Index) Chunk(id string) (Chunk, bool)func (x *Index) BuildIVF(nlist, iters int) error // PCG32 seeded with FNV1a64(index_id)func FNV1a64(s string) uint64type Hybrid struct { Index *Index; Embedder Embedder; Candidates int; RRFK float64; NProbe int; Lambda float64 }func (h *Hybrid) Retrieve(ctx context.Context, q string, k int, f Filter) ([]Hit, error)Hybrid.Retrieve takes max(Candidates, k) candidates (default 50) from BM25 and, when Embedder is set, from dense search (IVF when Index.IVF is built, else flat). It fuses them with RRF, then returns the top k, or the MMR picks when Lambda > 0. It embeds the query once and normalizes it. Embedder is a consumer-side interface that the ingest embedder satisfies: this package never imports the ingest packages, and the index files are the only contract between them. Every search takes a keep predicate so that filters can act before the cut. The solution uses only the standard library and tinyllm/ds/rng.
Building it step by step
Section titled “Building it step by step”The design follows from eight observations:
- Ingest and retrieval are different programs, so the term rule and the file layout are a contract, not an implementation detail. A Unicode-aware rule (
[\p{L}\p{N}_]+, lowercased) keepscaféwhole andtl_kv_exportintact. - Scoring must touch only relevant chunks. An inverted index maps each term to the chunks holding it, so a query walks postings rather than chunks.
- Relevance should saturate in tf and discount length, and no matched term should hurt. That is BM25 with the
1 +idf. - Postings are small increasing integers, so storing gaps and encoding them as varints keeps the file compact. Reading must check every length against the bytes left, because a crash can truncate the file.
- Dense search must be exact by default and approximate by choice. Flat scan is the yardstick. IVF scans only
nprobelists, and its lists must be identical on every load, which forces a deterministic seed (fnv1a64(index_id)) and a deterministic seeding rule (k-means++ with weights and a row-order scan). - Lexical and dense scores live on different scales. Ranks do not, so fusion uses ranks (RRF), and agreement between lists is rewarded by summing.
- The top should not repeat itself. MMR penalizes similarity to everything already picked, so a copy of the first pick stays penalized.
- A filter changes the candidate set, so it must act before the cut. Every search takes a
keeppredicate.
Building the BM25 index
Section titled “Building the BM25 index”BuildBM25 makes one pass over each text. DocLen counts every term occurrence, duplicates included, and postings are appended in document order, so every list is sorted by Doc without a sort:
func BuildBM25(texts []string, k1, b float64) *BM25 { idx := &BM25{K1: k1, B: b, DocLen: make([]int, len(texts)), Postings: map[string][]Posting{}} total := 0 for d, text := range texts { terms := Terms(text) idx.DocLen[d] = len(terms) // dl: occurrences, not distinct terms total += len(terms) tf := map[string]int{} var order []string // first-occurrence order: deterministic, unlike map order for _, t := range terms { if tf[t] == 0 { order = append(order, t) } tf[t]++ } for _, t := range order { idx.Postings[t] = append(idx.Postings[t], Posting{Doc: d, TF: tf[t]}) // d ascends } } if len(texts) > 0 { idx.AvgDL = float64(total) / float64(len(texts)) } return idx}Scoring
Section titled “Scoring”Scoring is the BM25 formula, applied once per query term occurrence (so cache cache counts twice) and only over that term’s postings:
func (x *BM25) IDF(term string) float64 { n := float64(x.N()) df := float64(len(x.Postings[term])) return math.Log(1 + (n-df+0.5)/(df+0.5)) // never negative}
func (x *BM25) Scores(q string) []float64 { s := make([]float64, x.N()) if x.AvgDL == 0 { return s // an empty index: avoid dividing by zero } for _, t := range Terms(q) { // duplicates kept on purpose pl := x.Postings[t] if len(pl) == 0 { continue } idf := x.IDF(t) for _, p := range pl { tf := float64(p.TF) dl := float64(x.DocLen[p.Doc]) s[p.Doc] += idf * tf * (x.K1 + 1) / (tf + x.K1*(1-x.B+x.B*dl/x.AvgDL)) } } return s}Search collects only documents that matched at least one term (chunk 2 in the hand example is absent, not a zero), applies keep, then calls TopK, which sorts by score descending with ties to the lower Doc.
Varints and the file
Section titled “Varints and the file”PutUvarint emits 7 bits at a time, low group first, so 300 gives ac 02. Uvarint refuses an eleventh byte, or a tenth byte above 1, which is what overflowing 64 bits looks like:
func PutUvarint(buf []byte, v uint64) []byte { for v >= 0x80 { buf = append(buf, byte(v)|0x80) // continuation bit set: more follows v >>= 7 } return append(buf, byte(v)) // last byte: top bit clear}
// in WriteTo, per term (terms sorted by their bytes): prev := 0 for _, p := range pl { post = PutUvarint(post, uint64(p.Doc-prev)) // gap; the first gap is the doc number post = PutUvarint(post, uint64(p.TF)) prev = p.Doc } b = binary.LittleEndian.AppendUint16(b, uint16(len(t))) b = append(b, t...) b = binary.LittleEndian.AppendUint32(b, uint32(len(pl))) // df b = binary.LittleEndian.AppendUint32(b, uint32(len(post))) // postings bytes b = append(b, post...)ReadBM25 is the same walk with a guard before every read. The header must be at least 24 bytes with magic TLBM and version 1; the doc lengths must fit in what remains; each term’s length plus 8 must fit; terms must be strictly increasing; each posting’s gap must be non-zero after the first and land below ; a postings block must decode to exactly df pairs; and nothing may trail the last term. Each failure is fmt.Errorf("%w: ...", ErrFormat), never an index past the end of the slice. The full code is in bm25.go.
k-means++
Section titled “k-means++”KMeansPP keeps as the squared distance to the nearest seed and updates it after each pick, so each round is :
picked := []int{int(r.Below(uint64(n)))} // first seed: uniform // ... d[i] = Dist2(vecs[i], vecs[picked[0]]) for len(picked) < k { var sum float64 for _, x := range d { sum += x } next := -1 if sum == 0 { // every remaining row duplicates a seed: lowest row not yet picked } else { u := r.Float64() * sum var run float64 for i, x := range d { // row order: part of the contract run += x if run > u { // strictly greater: a row with D = 0 is never chosen next = i break } } // ... u rounded up to sum: take the last row with weight } picked = append(picked, next) // ... d[i] = min(d[i], Dist2(vecs[i], vecs[next])) }BuildIVF runs Lloyd rounds: assign each row to Nearest, move each centroid to the float64 mean of its rows, leave an empty centroid where it is, and stop when no assignment changes. It then builds the lists from the final centroids. Search ranks centroids by negative squared distance and scans only the top nprobe lists, applying keep inside the scan:
func (x *IVF) Search(q []float32, k, nprobe int, keep func(doc int) bool) []Scored { cs := make([]Scored, len(x.Centroids)) for c, cv := range x.Centroids { cs[c] = Scored{Doc: c, Score: -Dist2(q, cv)} // nearest centroid = highest score } cs = TopK(cs, nprobe) var cand []Scored for _, c := range cs { for _, i := range x.Lists[c.Doc] { if keep == nil || keep(i) { // the filter runs before the cut to k cand = append(cand, Scored{Doc: i, Score: Dot(q, x.Vecs[i])}) } } } return TopK(cand, k)}Index.BuildIVF seeds the generator with rng.Seeded(FNV1a64(x.Meta.IndexID)), so the same index builds the same lists on any machine. The full code is in vector.go.
RRF counts ranks from 1 and sums over lists. A hit’s other fields come from the first list holding the chunk, and ties go to the lower index:
func RRF(lists [][]Hit, k float64, n int) []Hit { score := map[int]float64{} first := map[int]Hit{} for _, l := range lists { for r, h := range l { score[h.Index] += 1 / (k + float64(r+1)) // r is 0-based; the rank is r+1 if _, ok := first[h.Index]; !ok { first[h.Index] = h } } } // ... collect, sort by score desc then Index asc, cut to n, set Rank = position + 1}For the hand example, x gets , z gets , and y gets .
maxSim holds, for every unpicked candidate, its largest similarity to anything picked so far. It is updated after each pick, so a step costs dot products:
rel := make([]float64, len(cands)) for i, c := range cands { rel[i] = Dot(q, vec(c.Index)) } maxSim := make([]float64, len(cands)) // max over an empty set: 0 used := make([]bool, len(cands)) out := make([]Hit, 0, k) for len(out) < k { best, bv := -1, 0.0 for i := range cands { if used[i] { continue } v := lambda*rel[i] - (1-lambda)*maxSim[i] if best < 0 || v > bv { // strict: ties go to the earlier candidate best, bv = i, v } } used[best] = true // ... append with Score = bv, Rank = len(out)+1 pv := vec(h.Index) for i := range cands { if used[i] { continue } s := Dot(vec(cands[i].Index), pv) if len(out) == 1 || s > maxSim[i] { // the first pick sets it, even if negative maxSim[i] = s } } }The len(out) == 1 case matters: after the first pick, maxSim must become the actual similarity, which may be negative, rather than stay at the empty-set 0. The full code is in fuse.go.
Opening and retrieving
Section titled “Opening and retrieving”Open checks that the four files agree: n_chunks lines, n_chunks * dim * 4 vector bytes, n_chunks BM25 documents, and unique chunk ids that start with <doc_id>#. Retrieve builds one keep from the filter and passes it into every search:
keep := func(i int) bool { return f.Keep(x.Chunks[i]) } var lists [][]Hit // ... lex from x.BM25.Search(q, n, keep) lists = append(lists, lex) var qv []float32 if h.Embedder != nil { vs, err := h.Embedder.Embed(ctx, []string{q}) // ... exactly one vector if len(vs[0]) != x.Meta.Dim { return nil, fmt.Errorf("%w: query embedding has dim %d, index has %d", ErrIndex, len(vs[0]), x.Meta.Dim) } qv = Normalize(vs[0]) // unit query: dot products are cosines, MMR terms share a scale var dense []Scored if x.IVF != nil { dense = x.IVF.Search(qv, n, nprobe, keep) } else { dense = (&Flat{Vecs: x.Vecs}).Search(qv, n, keep) } // ... lists = append(lists, dense hits) } fused := RRF(lists, rrfk, n) if h.Lambda > 0 && qv != nil { return MMR(qv, fused, func(i int) []float32 { return x.Vecs[i] }, h.Lambda, k), nil } // ... fused[:k]The full code is in retrieve.go.
Why this design
Section titled “Why this design”Start with cost. BM25 is plus a sort of the matches; flat search is ; IVF is for the centroids plus about of the flat cost. Building IVF costs once per load. RRF is linear in the candidates, and MMR costs dot products.
Why IVF rather than HNSW? A graph index has better recall per scanned row, but it is far more code, harder to make bit-for-bit reproducible across languages, and pays off only at sizes this index does not reach. IVF with a seeded k-means is small, deterministic, and measurable against flat. Why RRF rather than score normalization? Min-max or z-score normalization of BM25 and cosine scores depends on each query’s score distribution and needs tuning; RRF has one constant, 60, that works across queries. And why build IVF at load rather than store centroids? There is no extra file format, and any implementation of the spec reproduces the lists; the cost is a few seconds of k-means per start-up.
Some tradeoffs are accepted on purpose: there is no stemming (evict does not match eviction), BM25 is held fully in memory, and a filter scans every chunk’s metadata.
What can go wrong
Section titled “What can go wrong”A correct solution must pass these checks; each line says what must hold and why it matters to the code that depends on retrieval:
| Case | Kind of check | What must hold | Why it matters downstream |
|---|---|---|---|
| the hand example’s BM25 | unit | the hand-worked BM25 numbers to 1e-12; chunk 2 is not returned | the reference formula |
| terms | boundary | underscores, hyphens, digits, accents, CJK, empty input | queries and the index cut text the same way |
| BM25 against an independent implementation | conformance | every chunk’s score for 11 queries equals an independent Python implementation to 1e-9 | the contract’s acceptance bar (rag-index.md) |
bm25.idx golden bytes | golden | WriteTo of the fixture corpus equals the independent writer’s bm25.idx byte for byte | ingest writes a file any reader can open |
bm25.idx round trip | property | ReadBM25(WriteTo(x)) equals x | a restarted retriever ranks as before |
| varints | boundary | 0, 127, 128, 300, 16383, 16384, , ; truncated input gives n = 0 | postings decode exactly |
corrupt bm25.idx | fault | every truncation, bad magic, trailing bytes, unsorted terms: ErrFormat, never a panic | a crash mid-write never takes the agent down |
| opening an index | unit | the fixture opens; short or long vectors, a missing line, a duplicate id are ErrIndex | texts and vectors stay paired |
| flat search | conformance | flat top 10 equals the independent implementation, ties to the lower chunk | the exact baseline IVF is measured against |
| k-means++ seeds | conformance | picks for two seeds equal the independent implementation’s (frozen PCG32 in Python) | centroids reproduce across languages |
| IVF seeded by the index id | unit | FNV1a64 vectors; with zero Lloyd rounds the centroids are the seed rows of fnv1a64("docs"); two loads agree | every load builds the same lists |
| IVF recall | property | 2000 clustered vectors: recall@10 at nprobe = 8 is at least 0.95 (0.986 for this solution), and 1 with every list | the catalog’s IVF bar |
| the hand example’s RRF | unit | the hand-worked fusion; equal fused scores go to the lower index | the fused order is deterministic |
| the hand example’s MMR | unit | the hand-worked MMR values; is pure relevance | the agent’s context is not three copies |
| hybrid retrieval | conformance | hybrid top 5, with a sources filter, and with MMR, equal the independent implementation’s ids and scores | context assembly gets the chunks the contract ranks |
| filter before the cut | boundary | 3 candidates per list and a one-document filter still give 3 hits, all from that document | a scoped search never comes back empty by accident |
| embedder mismatch | boundary | a query vector of the wrong dimension, or none: an error, not a panic | an index embedded by another model is refused |
| lexical only, and empty | smoke | no embedder: BM25 through RRF; no match: no hits and no error; k = 0 | “no results” is an answer the tool can return |
The pitfalls fall into three groups. In BM25, an idf without the 1 + gives common terms negative weights, so matching “the” pushes a chunk down; IDF is ln(1 + (N - df + 0.5)/(df + 0.5)). Counting document length as distinct terms means repeated words stop counting toward length, so long repetitive chunks win; DocLen[d] = len(Terms(text)), duplicates included. Deduplicating query terms makes cache cache score like cache, unlike every other BM25; Scores loops over Terms(q) with duplicates. And a different term rule in the index and the query (no lowercasing, or ASCII only) means Café never finds café and the index bytes differ from any other writer’s; one Terms ([\p{L}\p{N}_]+, strings.ToLower) serves both.
Warning: The index file is shared by two programs. A varint with the continuation bit inverted, or postings written without gaps, produces a file only one reader understands;
PutUvarintsets the top bit on every byte but the last, andWriteTowrites gaps. Trusting lengths read from the file lets a truncatedbm25.idxpanic the agent at start-up;ReadBM25checks every length against the remaining bytes. Not checking the vector file againstn_chunks x dimpairs chunk 12’s text with chunk 13’s vector;Openrequires exactlyn * dim * 4bytes.
Reproducibility is the second group. Ties broken by the higher index, or not at all, make two runs of the same query order equal chunks differently; TopK and RRF break ties to the lower index. k-means++ by farthest point, or weighted by distance instead of its square, gives centroids that differ from every other implementation and makes outliers into seeds; KMeansPP draws proportional to Dist2. Ignoring nprobe collapses either recall (one list) or speed (all lists); IVF.Search scans TopK(centroids, nprobe). Seeding IVF with a constant or the clock changes the lists between loads, so results change after a restart; Index.BuildIVF seeds with FNV1a64(index_id).
The third group is fusion and scope. RRF with ranks from 0, or the max instead of the sum, no longer rewards agreement between lists; the solution sums 1 / (k + r + 1) over lists. An MMR penalty against the last pick only, or an unnormalized query, lets near-copies come back after one diverse pick and puts relevance and redundancy on different scales; maxSim keeps the max over all picks, and Retrieve normalizes the query. Filtering after the cut returns fewer hits than exist, often none; keep is passed into every search. Not checking the query’s dimension lets an index embedded by another model give garbage or a panic; Retrieve returns ErrIndex on a dimension mismatch.
Where this leads
Section titled “Where this leads”Looking back, RAG ingest writes docs.jsonl, vectors.f32, and bm25.idx (calling BuildBM25 and WriteTo from the ingest entry point) and embeds queries, and the Go PCG32 port supplies rng.Seeded, Below, and Float64 for k-means++. Looking forward, the search_docs tool in Context Assembly, Citations, and the search_docs Tool calls Hybrid.Retrieve and assembles the hits into cited context; the agent’s cited answers over the course docs rest on it.
Each piece has a production counterpart:
| Solution piece | Production equivalent | What it adds | Where to look |
|---|---|---|---|
BM25, bm25.idx | Lucene, tantivy | skip lists over postings, block-max WAND to skip chunks that cannot reach the top k, field boosts, analyzers with stemming | tantivy (free), the tantivy example in Search & Indexing |
IVF | FAISS IndexIVFPQ, pgvector ivfflat, HNSW | product quantization to shrink vectors, graph indexes with better recall per scan | FAISS wiki (free) |
RRF + MMR | saige rag/internal/pipeline/pipeline.go, cross-encoder rerankers | multi-retriever fusion, a reranker that reads query and chunk together, HyDE query expansion | saige (free) |
Further reading
Section titled “Further reading”- Robertson and Zaragoza, The Probabilistic Relevance Framework: BM25 and Beyond (free).
- Cormack, Clarke, Buettcher, Reciprocal Rank Fusion (free).
- Arthur and Vassilvitskii, k-means++ (free).
- Carbonell and Goldstein, The Use of MMR (SIGIR 1998).
Key terms
Section titled “Key terms”- Term: a maximal run of Unicode letters, digits, and
_, lowercased; the unit both ingest and retrieval index. - Inverted index: a map from each term to its postings list of chunks and term frequencies.
- BM25: a lexical ranking function with saturating term frequency (
k1), length normalization (b), and an idf that is never negative. - Varint (unsigned LEB128): an integer encoding of 7 bits per byte, low group first, with a continuation bit on every byte but the last.
- IVF (inverted file index): a vector index that clusters rows around centroids and scans only the
nprobenearest clusters. - k-means++: a seeding rule that picks each new centroid with probability proportional to its squared distance from the nearest seed.
- Recall@k: the fraction of the exact top that an approximate search also returns.
- RRF (reciprocal rank fusion): combining ranked lists by summing over the lists containing each item.
- MMR (maximal marginal relevance): greedy selection that trades relevance against similarity to items already picked.
Summary
Section titled “Summary”- BM25 scores a chunk by the query terms it holds, with term frequency that saturates (
k1) and a penalty for long chunks (b); the+1inside the idf keeps every weight positive. - An index file is a contract between two programs: the term rule, the byte layout, and the varint postings must match exactly, or ingest and retrieval disagree silently.
- IVF trades a little recall for speed by scanning only the
nprobelists nearest the query; seeded k-means++ makes those lists the same on every load. - RRF fuses lexical and dense lists by rank alone, so scores on different scales never need calibrating; MMR then trades relevance for diversity.
- Filters run inside each search, before the cut to
k; a filter after the cut returns fewer results than exist.
Review questions
Section titled “Review questions”- Why is the idf rather than ?
Answer
For a term in more than half the chunks the inner ratio is below 1 and the plain logarithm is negative, so matching the term would push a chunk down. With `1 +` the argument is always above 1 and every matched term adds a small positive amount. - Why does
ReadBM25check that each gap after the first is non-zero?Answer
Postings are strictly increasing chunk numbers, so a gap of 0 would repeat a chunk. It can only come from corruption or a broken writer, and accepting it would double-count a chunk's tf. - Why must the k-means++ draw scan rows in row order and compare
run > ustrictly?Answer
The spec fixes the pick for a given PCG32 stream; any other order or comparison picks a different row for the same `u`, so the centroids would differ between implementations. The strict comparison also means a row with $D = 0$ (already a seed) can never be chosen. - Why does RRF reward a chunk that is second in both lists over one that is first in only one?
Answer
$2/62 = 0.0323 > 1/61 = 0.0164$. Each list contributes $1/(60 + r)$, which varies slowly with rank, so appearing in a second list matters more than moving up one place. That is the intended bias: agreement between independent signals is strong evidence. - What goes wrong if the filter is applied to the fused top
k?Answer
The 50 best candidates overall may contain no chunk of the requested document, so the caller gets fewer hits than exist, often none. Passing `keep` into each search makes the filtered chunks compete only among themselves. - Encode 300 as an unsigned LEB128 varint.
Answer
300 is binary `100101100`. The low seven bits `0101100` (0x2c) go first with the continuation bit set, giving `0xac`; the remaining bits `10` give `0x02` with the top bit clear. The encoding is `ac 02`.