Embedding quantisation for RAG: int8, 4-bit and binary vectors against ranking quality
A vector index for 250 million chunks needs almost a terabyte of RAM in float32. Quantisation cuts that by 4 to 32 times, but every bit you drop moves the similarity scores. Here you watch it happen on a query, the chunk that answers it and a near-miss that is only slightly less similar.
Interactive simulation. It needs JavaScript; the text below explains what it shows.
What you are looking at
Every chunk in a RAG system is stored as an embedding: a list of numbers, usually 384 to 3,072 of them, each a 32-bit float. At 1,024 dimensions and 250 million chunks that is 953.67 GB of memory before the index structure itself. Quantisation stores each number with fewer bits.
The table holds three unit-length vectors of 24 dimensions: a query q, the chunk that answers it (d1) and a near-miss chunk on the same topic (d2). The values have the spread of normalised sentence embeddings; they are an illustrative example, not taken from one named model. Paste your own vectors, one dimension per row, and everything recomputes.
The slider picks how many bits each value keeps. The chart shows the quantisation grid: the dashed diagonal is float32, the staircase is the scalar grid for q and the flat steps are the binary code. Dots are the actual dimensions of q after rounding, and the bars show the rounding error of each dimension.
The algorithm
Scalar quantisation with b bits uses a symmetric grid of 2L + 1 levels:
- L = 2^(b-1) - 1, so 8 bits give L = 127 (int8 has 256 codes, -128 to 127), 4 bits give 7 and 2 bits give 1.
- The step size is s = max|v| / L, set by the largest value in the vector.
- Each value is stored as round(v / s) and read back as s times that integer.
Binary quantisation keeps only the sign: +1 if the value is above zero, -1 otherwise. Two binary codes are compared with the Hamming distance H, the number of positions where they differ, and the similarity 1 - 2H/D equals the dot product of the ±1 codes divided by D. Hamming distance is one XOR and a popcount, which is why binary search is so fast.
Rescoring is the fix used in practice: search the cheap index for more candidates than you need, then recompute their scores with the full-precision query against the stored vectors, and keep the best k.
Worked example with the default data. In float32, cos(q, d1) = 0.6362 and cos(q, d2) = 0.5931, so the right chunk wins by 0.0431. At 8 bits the step for q is 0.0051, the relative error of q is 0.72% and the margin becomes 0.0446: nothing changes. At 4 bits the step is 0.0927, the error is 14.51% and the quantised cosines are 0.6798 and 0.6087, margin 0.0711: the scores move but the order holds. At 2 bits every value becomes -1, 0 or +1 times 0.649, the error is 75.88%, and the quantised cosines are 0.2500 and 0.4472: the margin is -0.1972 and the near-miss now ranks first. Binary codes give 0.5000 against 0.6667, margin -0.1667: also wrong. Rescoring the 2-bit candidates with the float32 query gives 0.4722 against 0.4632, margin +0.0091, and the right chunk is back on top.
Memory for 250 million 1,024-dimension vectors: 953.67 GB in float32, 238.42 GB at 8 bits, 119.21 GB at 4 bits and 29.80 GB in binary, 32 times smaller.
Things to try
- Move the bits from 8 down to 2 and watch the quantised margin: it stays positive at 8, 4 and 3 bits (0.0446, 0.0711, 0.0886) and turns negative only at 2 bits (-0.1972).
- Compare the binary margin (-0.1667) with the rescored margin at 2 bits (+0.0091): the cheap pass can be wrong as long as the right chunk survives into the shortlist.
- Edit one dimension of q to 0.9 to create an outlier: the step size grows, every other value loses precision, and the relative error jumps. This is why Qdrant offers a quantile setting that ignores extreme values.
- Set the real model to 3,072 dimensions and the index to 1,000 million vectors: float32 needs 11,444 GB, binary 358 GB.
- Paste two chunks that are almost identical in float32 and find the bit width at which their order first flips.
How it works in real systems
Sentence Transformers quantises int8 embeddings with ranges taken from a calibration set (the minimum and maximum of each dimension), not from each vector as here, and reports that int8 keeps about 97% of retrieval quality without rescoring and about 99% with it, while binary keeps 92.53% without rescoring and 96.45% with rescoring for mxbai-embed-large-v1. Its rescoring step retrieves rescore_multiplier times top_k candidates (4 times 100 in those experiments) and re-ranks them with the float32 query. The same post gives the memory bill used above: 953.67 GB, 238.41 GB and 29.80 GB for 250 million 1,024-dimension vectors.
Qdrant offers scalar quantisation to uint8 (4 times smaller, error usually under 1%), binary quantisation (32 times smaller, up to 40 times faster) plus 2-bit and 1.5-bit encodings, and an oversampling setting: with oversampling 2.4 and limit 100 it pre-selects 240 candidates and rescores them with the original vectors. Binary works best on high-dimensional models with centred values; with only 24 dimensions, as here, a few sign flips change the score a lot, which is why the binary margin is so wrong.
In a full pipeline quantisation sits next to the graph index, see the HNSW simulation, and in front of a reranker, see reranking. For weight quantisation of the language model itself see the quantization simulation, and for Matryoshka truncation combined with binary codes read Matryoshka embeddings, binary quantisation and rescoring.