Clustering & Dedup
Grouping meaning, finding copies
Embeddings make clustering and near-duplicate detection straightforward: group by proximity. The catch is precision — thresholds pick dedup sensitivity and cluster shapes, and the costs follow the data.
What you will learn
- Cluster embeddings with k-means or agglomerative style
- Define dedup thresholds with real labels
- Tune precision vs recall for dedup
What is this?
Clustering groups embeddings into self-similar clusters (k-means, HDBSCAN) for topic bucketing or workload splitting; dedup flags pairs/groups above a similarity threshold to collapse near-duplicate content. Both are 'distance' algorithms that inherit every decision you made about metric, model and threshold.
Why do you need it?
- Topic clustering lets you organize, route or hardsample without reading everything.
- Dedup stops the same error/ticket/doc from paying triple cost.
- Near-duplicates are semantic, not just textual — embeddings catch paraphrases that hashing misses.
- Both bear the cost of wrong thresholds: over-dedup loses distinct items, under-dedup wastes tokens/space.
Mental model
Sorting socks by look, then tossing near-twins
Clustering & dedup
You group similar socks into piles by pattern (clustering). Then you remove the pair that's almost identical to another pair — same make, tiny var stain (dedup). Set the 'almost identical' bar too loose and you discard two different wellies; too tight and duplicates stay.
How it works
- Clustering: distance-based grouping (k-means with k chosen; HDBSCAN density clusters).
- Dedup: pairwise or index-based similarity search within a threshold.
- Threshold governs precision/recall: lower = stricter (only true twins), higher = more aggressive recall.
- Near-duplicate groups can be represented by one 'exemplar' (e.g. medoid).
- Large scale needs ANN (vector DB index) for neighbor search before thresholding, not brute-force.
Cluster → threshold → exemplar.
In production
- Support teams dedup inbound tickets by embedding before triage routing.
- Content pipelines dedup scraped pages so RAG indexes pay once per unique idea.
- Clusters route queries to specialized agents or FAQ buckets.
Best practices
- Tune thresholds on labeled near-duplicate pairs (yes/no examples), not guesses.
- Dedup at ingest, not query time.
- Store the exemplar + member list for transparency and rollback.
- Revalidate cluster health after data drifts.
Common mistakes
- Brute-force pairwise dedup on a large corpus (quadratic cost).
- Treating 'same cluster' as 'same meaning' — clusters are similarity, not identity.
- One global threshold when content types differ in natural similarity.
Common issues
Near-duplicates surviveThreshold too strict or model blind to paraphrase
Tune with labeled pairs; try a stronger embedding model.
Performance collapse on big corpusPairwise scans
Pre-build an ANN index and threshold on neighbors only.
Debugging tips
- Dedup collapsing distinct items → threshold too aggressive; sample labeled pairs.
- Distinct-but-similar topics merged → poor model/chunk granularity, not 'clustering is broken'.
- Slow dedup on 1M rows → quadratic loop; switch to vector-index neighbor search.
Interview checkpoint
Quick quiz
Near-duplicate detection is essentially…
Aggressive dedup thresholds can remove legitimate distinct items.