Measure how compressed vectors change candidate recall, then recover ordering with exact reranking where the budget permits.
Vector compression: spend memory without losing the answer
Separate storage from ranking quality
A dense index can store full precision vectors or compressed codes. Product quantization and related codecs reduce per-vector storage by representing a vector with learned codebook entries, but the distance computed from a code is approximate. A smaller index does not automatically mean a better service: a support document that falls out of the candidate set cannot be rescued by the language model. Keep the original vector ID, embedding-model version and source revision alongside each compressed code. Embedding migration must rebuild or isolate incompatible spaces.
Budget the entire resident index
A 768-dimensional float32 vector uses 3,072 raw bytes before IDs, graph edges, partition metadata or replicas. A 48-byte code plus an eight-byte ID looks much smaller, but codebooks, coarse centroids, filtering structures and a raw-vector store for reranking still consume memory. Estimate each component against the actual number of vectors and replicas. If exact reranking reads remote raw vectors, account for that I/O in tail latency. Filtered ANN can lose still more candidates after permission or category constraints narrow the search space.
Use two stages deliberately
Retrieve a larger candidate set using compressed distances, apply eligibility filters, then compute exact similarity on the surviving vectors before final ranking. The second stage corrects ordering errors only when the correct item survived stage one. Increasing the approximate candidate count raises memory reads and exact-score work; it is not a free recall fix. Keep the rerank window tied to latency and access budgets. Candidate-window design makes this trade measurable rather than setting one global top-K by habit.
Train codes on representative data
A codebook trained on general prose may compress rare part numbers, source code or other specialized content poorly. Sample from the production distribution without including restricted documents in an unsafe shared training artifact. Evaluate queries by domain, language, document length and freshly added content. Rebuild when the embedding distribution changes enough to shift recall, and compare the new index with a full-precision baseline over the same corpus snapshot. A score difference across codec versions is not directly interpretable as a relevance change.
Gate on supported evidence
Measure exact-neighbor recall, answer-bearing passage recall, supported-answer rate, p95 latency and resident bytes together. A codec that preserves average neighbor recall but drops the only passage for safety-critical questions should fail its release slice. Record whether a miss arose in coarse partition selection, code distance, filter application or reranking. Keep a rollback index or a staged rollout until those failures are understood. Release gates should include both quality and resource limits.
Worked contract
from dataclasses import dataclass
@dataclass(frozen=True)
class VectorCandidate:
passage_id: str
approximate_distance: float
exact_distance: float
def exact_rerank(candidates: list[VectorCandidate], width: int) -> list[str]:
if width < 1:
raise ValueError("candidate width must be positive")
shortlist = sorted(candidates, key=lambda item: item.approximate_distance)[:width]
return [item.passage_id for item in sorted(
shortlist, key=lambda item: (item.exact_distance, item.passage_id))]
def storage_bytes(vectors: int, dimension: int, code_bytes: int) -> tuple[int, int]:
return vectors * dimension * 4, vectors * (code_bytes + 8)
candidates = [
VectorCandidate("seal-procedure-r4", .18, .08),
VectorCandidate("seal-catalog-r2", .09, .13),
]
assert exact_rerank(candidates, 2)[0] == "seal-procedure-r4"
assert exact_rerank(candidates, 1)[0] == "seal-catalog-r2"
assert storage_bytes(2_700_000, 768, 48) == (8_294_400_000, 151_200_000)Performance and operating cost
For C retrieved candidates, sorting the approximate stage costs O(C log C), then sorting W shortlisted candidates costs O(W log W), with O(C) working space in this demonstration. Real ANN engines avoid a full corpus sort and add codec decoding and coarse-search costs. The byte calculation excludes codebooks, centroids, graph links, filtering metadata, replicas and any full-precision rerank store; measure those separately before committing to an index size.
Common Mistakes
- Reporting code bytes as total index memory.
- Assuming exact reranking can restore a relevant item absent from the shortlist.
- Training compression on a corpus unlike the one served.
- Comparing approximate distances from different codecs as if they shared a calibrated scale.
Validation drill
For 61 judged queries, compare full precision, compressed top-1 and compressed top-12 followed by exact reranking. Record the first lost answer-bearing passage, resident memory and p95 latency per domain.
