A number that belongs to one model
Similarity thresholds appear everywhere in retrieval systems: deduplicate help-desk cases above 0.85 cosine similarity, treat a cached answer as a hit above 0.9, drop retrieved chunks below 0.7. The number is usually chosen once, by looking at a few examples, and then copied into configuration. It stays there when the embedding model changes, because nothing in the system fails loudly: the vectors still have a length, the index still returns neighbours, and the threshold still filters something.
The problem is that a cosine value has no meaning outside the model that produced it. Different models spread unrelated texts over very different parts of the range. A threshold that separates duplicates from non-duplicates well for one model can accept everything, or almost nothing, for another.
Measured on two local models
To put numbers on this, I embedded 24 synthetic sentence pairs from Belgavi's Book Store's help desk with two models served by a local Ollama 0.40.18: nomic-embed-text (768 dimensions) and all-minilm (384 dimensions). Twelve pairs are the same request worded differently ("I was charged twice for one order" and "My card shows two payments for a single order"). Twelve are related but different requests that share words and topic ("I was charged twice for one order" and "I ordered the same book twice by mistake"). For nomic-embed-text each text was given the clustering: prefix that the model card describes for grouping texts; all-minilm takes no prefix. Cosine similarity was computed in plain Python with a left-to-right loop.
| Model | Same-request pairs | Different-request pairs | Best single threshold | Errors at that threshold |
|---|---|---|---|---|
| nomic-embed-text | 0.852 to 0.971 | 0.664 to 0.890 | 0.852 or 0.893 (tie) | 3 false matches and 0 missed, or 0 false matches and 3 missed |
| all-minilm | 0.635 to 0.905 | 0.220 to 0.753 | 0.635 | 1 false match, 0 missed |
Two things stand out. The ranges overlap for both models, so no single threshold is perfect on these pairs; for nomic-embed-text two thresholds tie at three errors, one lenient and one strict. And the two models live on different scales: every different-request pair under nomic-embed-text scored above 0.66, while all-minilm gave the most different pair 0.22.
Now move the threshold across models, which is exactly what happens when a configuration value survives a model swap:
| Threshold taken from | Applied to | Matches | False matches | Missed duplicates | Precision | Recall |
|---|---|---|---|---|---|---|
| all-minilm (0.635) | nomic-embed-text | 24 | 12 | 0 | 0.50 | 1.00 |
| nomic-embed-text (0.852, the lenient tie) | all-minilm | 2 | 0 | 10 | 1.00 | 0.17 |
Moving from all-minilm to nomic-embed-text with the old threshold turns the deduplicator into a machine that merges every pair, including all twelve different requests. Moving the other way merges two of twelve real duplicates. Neither failure produces an error. This is a small, synthetic set: the exact values say nothing about these models in general, only that thresholds must be measured per model and per task.
Calibrate on labelled pairs
Calibration is cheap compared with the cost of a silently wrong threshold. The procedure:
- Collect labelled pairs from your own traffic. For deduplication, pairs that a person marked as the same request or not. For retrieval filters, query and passage pairs marked relevant or not. Include hard negatives: pairs that share vocabulary but differ in meaning, because those decide where the threshold sits.
- Score every pair with the exact model, prefix and normalisation you deploy. A changed prefix changes the scores, so the prefix is part of the model configuration.
- Choose the threshold from the cost of each error. A false merge of two different customer requests means one customer gets no answer; a missed duplicate means two agents answer the same customer. Write down which costs more, then pick the threshold that minimises the weighted errors on the labelled set.
- Store the threshold with the model identity, for example as a record keyed by model name, digest and prefix, so that a model change without a new threshold fails at startup.
- Re-run on every model, prefix or preprocessing change, and keep the labelled set versioned.
def errors_at(scores, labels, t, false_match_cost=3.0, miss_cost=1.0):
fm = sum(1 for s, same in zip(scores, labels) if s >= t and not same)
miss = sum(1 for s, same in zip(scores, labels) if s < t and same)
return false_match_cost * fm + miss_cost * miss
def calibrate(scores, labels, **costs):
candidates = sorted(set(scores))
return min(candidates, key=lambda t: (errors_at(scores, labels, t, **costs), -t))The tie-break toward the higher threshold is a choice; state it. On these pairs it settles the nomic-embed-text tie at 0.893, accepting three missed duplicates to avoid all false merges. With three times the cost on a false match, 0.893 is no longer a tie but a clear winner (3 weighted errors against 9), which is the point of writing the costs down: the threshold follows from the decision you care about, not from the scores alone.
When a threshold is the wrong shape
A global threshold assumes the boundary between "same" and "different" is the same for every kind of text. Short texts and long texts often score differently; so do texts in different languages. If your labelled set shows that one category needs 0.80 and another 0.92, either keep per-category thresholds with enough labels behind each, or stop thresholding raw cosine values. Two alternatives work well:
- Rank, do not threshold, when the downstream step can take the top k and decide. Retrieval followed by a reranker or a model that reads the candidates does not need a cosine cut-off at all, only enough recall in the top k.
- Use a pair classifier (a cross-encoder or a small model trained on your labels) when you need a yes or no answer. It reads both texts together and usually separates hard negatives better than a distance does.
How many labelled pairs are enough
The 24 pairs above are enough to show the scale problem and far too few to set a production threshold. Each misclassified pair moves precision or recall by several points, and the boundary sits where hard negatives live, which a small set samples badly. A practical target is a few hundred labelled pairs per decision you threshold, with at least a third of the negatives chosen to be hard: same product, same order number format, same complaint category, different request. Sample positives and negatives from real traffic rather than writing them, because written paraphrases are cleaner than what customers send.
Report the threshold with an interval rather than a single number. Resample the labelled set with replacement a few hundred times, recalibrate on each sample and look at the spread of chosen thresholds. If the spread is wide, the data cannot pin the threshold down, and the right next step is more hard negatives, not more decimal places.
Failure modes
- Threshold copied across a model swap. As measured above, the error can go either way. Bind thresholds to the model digest.
- Prefix drift. One code path adds the documented task prefix and another does not, so the same text gets two different vectors and scores. Test that every path builds the same input.
- Unnormalised vectors with an inner-product index. Scores change scale and the threshold means nothing. Check vector norms for a sample after every model change.
- Easy negatives only. A labelled set of obviously unrelated pairs produces a threshold that looks perfect and fails on real near-misses.
- Threshold tuned on the evaluation set. Keep a held-out set to report the final precision and recall.
Verification
The companion example ships the 48 sentences, the labels, the scores recorded from this run and the calibration code, with tests that recompute the table offline. A live mode re-embeds the pairs if you have Ollama running, so you can repeat the measurement on your own machine; your numbers may differ with other model builds. In production, log the score distribution of accepted and rejected pairs per model digest, and alert when a deploy changes the digest without changing the threshold record.