In Product Quantization (PQ) and Vector Compression, a codebook is the fundamental lookup table generated by applying k-means clustering to a subspace of training vectors. Each entry in the codebook is a centroid that represents a cluster of similar data points. During encoding, an original vector is decomposed into subvectors, and each subvector is replaced not by its full coordinates, but by the short integer ID of the nearest centroid in the corresponding codebook, dramatically reducing memory footprint.
Glossary
Codebook

What is Codebook?
A codebook is a finite set of representative centroid vectors learned via clustering that enables lossy compression of high-dimensional data by mapping original vectors to the discrete index of their nearest centroid.
The tradeoff is governed by quantization error: the distortion between the original vector and its codebook-based reconstruction. A larger codebook with more centroids reduces this error but increases storage and the bitrate of the codes. During search, the Asymmetric Distance Computation (ADC) uses the codebook to efficiently approximate distances by looking up precomputed query-to-centroid distances, avoiding full vector reconstruction.
Key Characteristics of Codebooks
A codebook is the fundamental compression lookup table in vector quantization. It maps high-dimensional continuous vectors to discrete integer codes, enabling dramatic memory reduction and fast approximate distance computation.
Centroid-Based Representation
A codebook is a set of K representative centroid vectors learned from the data distribution. Each original vector is replaced by the ID of its nearest centroid, reducing storage from D floating-point numbers to a single integer. The codebook itself stores the full-precision centroids for reconstruction. Typical codebook sizes range from 256 (8-bit codes) to 4096 (12-bit codes), balancing compression ratio against quantization error.
K-Means Clustering Foundation
Codebooks are typically constructed using the Lloyd's k-means algorithm on a representative training set. The process iteratively:
- Assigns each training vector to the nearest centroid
- Updates centroids to the mean of their assigned vectors
- Repeats until convergence or a maximum iteration count
The resulting centroids minimize the within-cluster sum of squared errors, ensuring the codebook optimally represents the training distribution under Euclidean distance.
Product Quantization Decomposition
In Product Quantization (PQ), a high-dimensional vector is split into M equal-length subvectors, each quantized using its own distinct codebook. For a 128-dimensional vector split into 8 subvectors of 16 dimensions, each with a 256-entry codebook:
- Storage drops from 128 floats (512 bytes) to 8 bytes
- The total number of representable vectors is 256^8, despite storing only 8 × 256 centroids
- This exponential expressiveness with linear storage is the key insight behind PQ's effectiveness
Distance Computation via Lookup Tables
Codebooks enable Asymmetric Distance Computation (ADC) for fast similarity search. When a query arrives:
- Compute distances between the query subvectors and all centroids in each codebook
- Build M lookup tables of size K each
- For any database vector, approximate its distance to the query by summing precomputed table values using its stored codes
This replaces expensive D-dimensional distance calculations with M integer table lookups and additions, yielding 10-50x speedups.
Quantization Error and Codebook Size Tradeoffs
The quantization error — the mean squared distance between original vectors and their codebook reconstructions — decreases as codebook size K increases. However:
- Larger K increases training time and lookup table computation cost
- For PQ with M sub-codebooks of size K, total training complexity is O(M × K × D_sub × iterations)
- Practical sweet spots: K=256 for 8-bit codes, K=65536 for 16-bit codes in high-precision scenarios
- Residual quantization can reduce error by encoding the difference between the original vector and its first-stage approximation
Codebook Training and Overfitting Prevention
Effective codebooks require a training set representative of the query distribution. Best practices include:
- Using 100K to 1M training vectors per codebook for stable convergence
- Applying k-means++ initialization to avoid poor local minima
- Running multiple restarts and selecting the clustering with lowest distortion
- For dynamic datasets, implementing online codebook updates or periodic retraining to prevent distribution drift
Poorly trained codebooks exhibit high quantization error on production data, directly degrading search recall.
Frequently Asked Questions
Clear, technically precise answers to common questions about codebooks, their role in vector quantization, and their impact on approximate nearest neighbor search performance.
A codebook is a finite set of representative centroid vectors learned via clustering during the quantization process, used to encode original high-dimensional vectors as short codes referencing the nearest centroids. In Product Quantization (PQ), the original vector space is decomposed into lower-dimensional subspaces, and a distinct sub-codebook is trained for each subspace using k-means clustering. Each sub-codebook typically contains 256 centroids, allowing the centroid index to be stored in a single byte. The full vector is then represented as a tuple of these byte-sized codes, achieving dramatic compression—a 128-dimensional float32 vector requiring 512 bytes can be compressed to just 8-16 bytes, depending on the number of subvectors. The codebook itself is stored separately and used during distance computation to reconstruct approximate vector values via table lookups.
Codebook vs. Related Quantization Concepts
Distinguishing the codebook from the broader quantization frameworks and index structures it enables.
| Feature | Codebook | Product Quantization (PQ) | Scalar Quantization (SQ) |
|---|---|---|---|
Definition | A set of centroid vectors learned via clustering for a single subspace | A composite compression method using multiple distinct codebooks on orthogonal subvectors | A compression method mapping each vector dimension independently to a small integer bin |
Granularity | Sub-vector or full-vector subspace | Multiple independent sub-vector subspaces | Per-dimension scalar values |
Primary Role | Lookup table for encoding vectors to short codes | Memory-efficient approximate distance computation | Bit-width reduction for storage compression |
Memory Compression Ratio | Depends on codebook size (e.g., 256 centroids = 8-bit code) | 4-8x typical; 32x-64x aggressive | 2-4x typical for 8-bit; 4x for 4-bit |
Distance Computation | Symmetric or asymmetric distance between query and centroid codes | Asymmetric Distance Computation (ADC) using precomputed distance lookup tables | Exact or approximate L1/L2 with integer arithmetic |
Used In | Standalone Vector Quantization, PQ sub-spaces, IVF coarse quantizer | IVFPQ, FAISS, compressed vector storage | DiskANN, FAISS, edge deployment |
Accuracy Impact | Quantization error proportional to codebook size and subspace variance | Low to moderate recall loss; anisotropic weighting improves MIPS | Moderate recall loss; uniform binning ignores data distribution |
Training Complexity | O(nkd) for k-means clustering | O(nkd') per subspace with k-means; parallelizable | O(n) for min/max range estimation; trivial |
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Core concepts that interact with codebooks during the vector compression and approximate nearest neighbor search pipeline.
Product Quantization (PQ)
The parent compression technique that decomposes a high-dimensional vector into smaller subvectors, quantizing each independently using a distinct codebook per subspace. The original vector is represented as the concatenation of centroid IDs from each codebook, dramatically reducing memory footprint from 4 bytes per float to a few bits per dimension.
Quantization Error
The distortion introduced when mapping a continuous vector to its nearest discrete centroid in a codebook. Measured as the Euclidean distance between the original vector and its quantized representation. This error represents the fundamental accuracy–compression tradeoff: larger codebooks reduce error but increase storage and encoding cost.
IVFPQ
A composite indexing strategy combining an Inverted File Index for coarse partitioning with Product Quantization for compressed residual vector storage. The IVF coarse quantizer assigns vectors to partitions; the residual vectors are then compressed using PQ codebooks, enabling high recall with low memory consumption.
Asymmetric Distance Computation (ADC)
An efficient distance approximation where only the database vectors are compressed via quantization, while the query vector remains in full precision. Distances are computed between the uncompressed query and the codebook centroids, yielding higher accuracy than symmetric computation where both vectors are quantized.
Residual Vector
The difference vector calculated by subtracting the assigned coarse centroid from the original vector. This residual encodes fine-grained local information within a Voronoi cell and is subsequently compressed using a PQ codebook, concentrating quantization precision on the local neighborhood rather than the full space.
Vector Compression
The broader category of techniques—including scalar, binary, and product quantization—that reduce the storage size of high-dimensional vectors. Codebooks are the core mechanism enabling lossy compression, trading computational precision for lower memory footprint and faster I/O during similarity search.

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us