0Pricing
LangChain / RAG / Vector DBs · Lesson

Quantization and Compression of Vectors

Shrink vector storage and speed up search with scalar and product quantization while controlling accuracy loss.

Quantization and Compression of Vectors is a free LangChain / RAG / Vector DBs lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the LangChain / RAG / Vector DBs learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Memory Problem

A million 1536-dimension vectors stored as 32-bit floats need about 6 GB of RAM. Quantization compresses vectors so they fit in far less memory and search faster.

Float32 Baseline

By default each dimension is a 4-byte float. Storage equals vectors x dims x 4 bytes. Reducing the bytes per dimension is the path to compression.

vectors = 1_000_000
dims = 1536
bytes_total = vectors * dims * 4
print(bytes_total / 1e9, "GB")  # ~6.14 GB

Scalar Quantization

Scalar quantization maps each float to an 8-bit integer using the min and max range of that dimension. This is a 4x reduction with modest accuracy loss.

def quantize(x, lo, hi):
    span = hi - lo
    return round((x - lo) / span * 255)

print(quantize(0.3, -1.0, 1.0))  # 165

Dequantization

To compare vectors you can dequantize back to an approximate float, or compute distances directly in integer space for speed.

def dequantize(q, lo, hi):
    return lo + (q / 255) * (hi - lo)

print(round(dequantize(165, -1.0, 1.0), 3))  # ~0.294

Product Quantization (PQ)

PQ splits each vector into sub-vectors and replaces each with the id of its nearest centroid from a small learned codebook. Compression can reach 16x or more.

How PQ Encodes

For 8 sub-vectors with 256 centroids each, every vector becomes 8 bytes regardless of original dimension. Distances are estimated from precomputed centroid tables.

Binary Quantization

The most aggressive option keeps only the sign of each dimension: positive becomes 1, negative becomes 0. A 1536-dim vector fits in 192 bytes and uses fast Hamming distance.

def binarize(vec):
    return [1 if v > 0 else 0 for v in vec]

print(binarize([0.4, -0.1, 0.9, -2.0]))  # [1, 0, 1, 0]

The Accuracy Tradeoff

More compression means more approximation error. Measure recall against an uncompressed baseline to ensure the quality loss is acceptable for your use case.

Rescoring with Full Vectors

A common pattern: search fast with quantized vectors to get a candidate set, then rescore the top candidates using the original float vectors for precision.

candidates = quantized_search(query, k=100)
rescored = sorted(
    candidates,
    key=lambda c: exact_distance(query, full_vec[c]),
)[:10]

Configuring in a Store

Production stores expose quantization as a collection setting. You pick the type and any rescore depth at index creation.

# pseudo-config
collection.create(
    vectors={"size": 1536, "distance": "Cosine"},
    quantization={"scalar": {"type": "int8"}},
)

Choosing a Strategy

Start with scalar quantization for an easy 4x win. Move to PQ or binary only when memory is critical and you can afford rescoring to recover accuracy.

Quick Check

Test your understanding of vector compression.

Recap

You explored vector compression:

  • Scalar quantization: float to int8, 4x smaller
  • PQ: codebook ids, big compression
  • Binary: sign bits, Hamming distance
  • Rescore with full vectors to regain accuracy

Frequently asked questions

Is the “Quantization and Compression of Vectors” lesson free?

Yes — the full text of “Quantization and Compression of Vectors” is free to read here on the web, and the LangChain / RAG / Vector DBs course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the LangChain / RAG / Vector DBs course, upgrade to CoddyKit PRO.

What will I learn in “Quantization and Compression of Vectors”?

Shrink vector storage and speed up search with scalar and product quantization while controlling accuracy loss. You practise LangChain / RAG / Vector DBs with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start LangChain / RAG / Vector DBs?

No prior experience is required. LangChain / RAG / Vector DBs on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Quantization and Compression of Vectors” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this LangChain / RAG / Vector DBs lesson?

Yes. Every LangChain / RAG / Vector DBs lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Vector DB Storage Architectures
  2. Proximity Search Algorithms (HNSW, IVFFlat)
  3. Vector DB Persistence and Scalability
  4. Quantization and Compression of Vectors
← Back to LangChain / RAG / Vector DBs