esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
Let’s talk

I’ll build the agent for it.

What Is Product Quantization?

What product quantization is, how it shrinks each vector into a short code, and the accuracy it costs, explained for your AI interview.

In short

Product quantization splits each vector into short pieces and replaces every piece with the ID of its nearest typical example, so a long vector becomes a short code. It cuts memory a lot for search over millions of chunks, at the cost of some accuracy.

12 min readAdvancedOccasional · estimateFacts checked 27 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

Product quantization shrinks a vector so search can hold millions of them in memory. I split each vector into short pieces, then swap each piece for the ID of its nearest typical example, called a centroid. The list of IDs is the code. Search builds a small table of distances, then scores the codes fast. It costs some accuracy, so I re-check the best few candidates against the full vectors.

What is product quantization, in plain words?

Product quantization, or PQ, is a way to shrink the vectors that an AI search stores. A vector is a long list of numbers that stands for the meaning of a chunk of text. A big search index can hold millions of them, and that takes a lot of memory.

PQ cuts each vector into short pieces. For each piece, it keeps a small set of typical examples, called centroids. Then it replaces each piece with the ID of the nearest centroid, so the whole vector becomes a short list of IDs.

Think of a courier sorting parcels. At each hub they do not read the full street address. They read a short route code that stands for a whole area. The code is less exact, but it is enough to sort. A legal-tech team holding 40 million contract chunks can do the same: keep short codes in memory, and check only a few full vectors for each question.

Key words

Vector
A list of numbers that stands for the meaning of a piece of text, an image or an item.
Centroid
The centre of a cluster of similar pieces. It stands for all of them.
Codebook
The set of centroids learned for one piece of the vector.
Short code
The list of centroid IDs that replaces a vector. It is what gets stored.

How are the codes built?

Building happens before any search. Microsoft's documentation for its Azure DocumentDB product describes the steps: cut each vector into smaller parts (the docs call them subspaces), group sample parts into clusters with a standard method called k-means, which finds the centre of each group, then store each vector as the cluster IDs of its parts.

  • Step one

    Cut the vector

    Split every vector into equal pieces. A made-up 768-number vector becomes 96 pieces of 8 numbers.

  • Step two

    Learn the codebooks

    For each piece, cluster sample pieces to find the centroids. Each piece gets its own codebook.

  • Step three

    Write the code

    Swap each piece for its nearest centroid ID. The 96 IDs are the short code for that vector.

Turning one vector into a short code

Full vector

768 float32 numbers, about 3 KB each

96 pieces of 8 numbers

each piece gets its own codebook

Learned codebooks

Nearest centroid

typically 256 per piece

96 IDs, one byte each

96 bytes, not about 3 KB

  1. 1Cut the 768-number vector into 96 pieces of 8 numbers each.
  2. 2For each piece, find the closest centroid in its learned codebook.
  3. 3Keep only the 96 centroid IDs. They are the short code.
A vector goes in whole and comes out as a short code. Each piece is matched to its nearest centroid, and the codebooks were learned in advance from sample data. Only the IDs are stored.

Why 256? The usual design gives each piece 256 centroids. An ID from 0 to 255 fits in one byte, because eight bits give 256 values. So 96 pieces give a 96-byte code.

The idea is old and well known. Jégou, Douze and Schmid described the method in a 2011 paper in IEEE Transactions on Pattern Analysis and Machine Intelligence.

Here it is in plain Python, with made-up numbers. Read the encode function: it is the step that matters.

Python
# A small sketch with made-up numbers: a 4-number vector,
# split into 2 pieces, with 3 centroids for each piece.
CODEBOOKS = [
    [(0.0, 0.0), (1.0, 1.0), (2.0, 0.5)],
    [(0.0, 1.0), (1.5, 0.0), (1.0, 2.0)],
]

def dist2(a, b):
    return sum((x - y) ** 2 for x, y in zip(a, b))

def encode(vec):
    codes = []
    for i, book in enumerate(CODEBOOKS):
        piece = vec[2 * i:2 * i + 2]
        codes.append(min(range(len(book)), key=lambda j: dist2(piece, book[j])))
    return codes

def decode(codes):
    rebuilt = []
    for i, c in enumerate(codes):
        rebuilt.extend(CODEBOOKS[i][c])
    return rebuilt

vec = [1.2, 0.9, 1.4, 0.1]
codes = encode(vec)
rebuilt = decode(codes)
print("original:", vec)
print("codes:", codes)
print("rebuilt:", rebuilt)
print("error:", round(dist2(vec, rebuilt) ** 0.5, 3))

Output

original: [1.2, 0.9, 1.4, 0.1]
codes: [1, 1]
rebuilt: [1.0, 1.0, 1.5, 0.0]
error: 0.265

What does it cost, and what does it save?

  • Saves

    Memory

    Each vector becomes a short code of 96 bytes in the example below, instead of 3,072 bytes of numbers.

  • Costs

    Accuracy

    Distances are estimates. A true near neighbour can drop out of the top list.

  • Costs

    Build time

    Learning the codebooks takes time. In Azure, a larger training sample (pqSampleSize) gives better quality but a longer build.

  • “PQ compression can lead to precision loss in distance calculations.”

    Microsoft, Azure DocumentDB docs

  • “These extra candidates are used to refine the search using the original, full-precision vectors, improving the final top k accuracy.”

    Microsoft, Azure DocumentDB docs

Here is a made-up case with real arithmetic. A legal-tech company indexes 40 million text chunks, each as a 768-number vector. Each number is a 32-bit value, which is 4 bytes, so one vector is 768 × 4 = 3,072 bytes. For 40 million vectors that is about 123 GB of raw data. With 96 one-byte codes per vector, the codes take 96 bytes each, about 3.8 GB in total. That is 32 times less memory for the codes held in RAM, so they fit easily in the RAM of one server.

The price is accuracy, so the team keeps the full vectors on disk and checks the best 20 candidates against them. That check is what helps recover the ranking quality the codes lose, at a small extra cost in time.

Tip

Asked about the trade-off, say how you would measure it: recall (the share of the true best answers the search still finds) on real questions with and without PQ, the memory saved, and the build time. Then name the fix: re-rank a few candidates with full vectors, and raise the candidate count if recall is too low.

How would you cut the memory of a 40-million-chunk index?

Start with the errors users can live with. In contract search, a missed clause is a real risk, so the bar for recall is high and must be tested before any compression goes live.

  1. 1

    Measure first

    Build a set of real lawyer questions. For each one, run an exact search and keep its top 10 as the baseline.

  2. 2

    Compress and take extra candidates

    Compress with PQ, then take more candidates than you need. Azure's oversampling setting does this: it fetches extra candidates and refines them with the full-precision vectors.

  3. 3

    Size the training sample

    Give the codebooks enough sample vectors. A larger sample gives better quality, but the build takes longer, so budget that time.

  4. 4

    Compare and decide

    Record recall at 10 for the compressed search: how many of the exact top 10 it still returns. If recall drops too far, raise the candidate count or use less aggressive compression. If it holds, ship the smaller index and keep watching recall.

What does the interviewer want to hear?

A strong answer covers

  • A plain definition: split each vector into pieces, then store the IDs of their nearest centroids.
  • The memory saving, with a rough size: about 32 times less in the example above.
  • The cost: distances are estimates, so accuracy drops.
  • The fix: re-rank a few candidates against the full vectors.
  • Where it fits: very large indexes that must fit in memory, often with IVF in front (IVF groups vectors into clusters first).
  • How you would tune it: recall tests, sample size and build time.

Common traps

  • Trap"Product quantization makes search exact, just smaller."

    BetterIt makes the distances approximate. Re-rank a few candidates with the full vectors to win accuracy back.

  • Trap"Use it on every index."

    BetterA small index fits in memory anyway. Use PQ when memory or cost is the real limit.

  • Trap"Each vector gets its own codebook."

    BetterThe codebooks are shared. One codebook per piece is learned from sample data and reused for every vector.

Can you spot the right pipeline?

Spot the pipeline

A team has 40 million chunks, memory is tight, and the answers must stay close to an exact search. Which pipeline fits?

Quick check

A chunk index holds 40 million vectors and must fit in RAM. What does product quantization change?

Can you answer these from memory?

Flash cards · tap a card to flip it

1 / 6

What will they ask next?

  1. Q119What is approximate nearest-neighbour search?10 min
  2. Q120How does HNSW work?11 min
  3. Q121How does IVF indexing work?11 min
  4. Q123What is hybrid search (BM25 + vectors)?11 min
  5. Q131Retrieval metrics: recall@k, MRR, nDCG?12 min

Last updated on 27 November 2026

Building search over a large set of documents?

I design and build AI products and agents, including the search behind them. Book a short call and tell me how many documents you have and what the answers must get right.