AWSAIRAGPython

Retrieval Quality Tuning: A Similarity Threshold That Cut RAG Context Noise by 60% — With the Numbers to Prove It

A measured retrieval-quality experiment on a production RAG pipeline: a golden eval dataset nearly lied to me with a ceiling-effect bug, then real baseline numbers showed most of top-8 retrieval was noise. A similarity threshold fixed it — recall unchanged, 60% less irrelevant context per query.

December 2, 2026·7 min read·Sanjay Patoliya·⭐ View on GitHub

The Question

Every RAG tutorial retrieves the top-k nearest chunks and calls it done. Mine does too — find_relevant_chunks_global() in the AI Document Processor pulls the 8 nearest chunks by cosine similarity, no matter what they actually score. I'd never measured whether those 8 were any good. So I built a real eval, measured it, and found out: most of them weren't.

The result up front, because the whole point of this series is real numbers, not vibes: adding a similarity floor of 0.3 cut retrieved context from 8 chunks/query to an average of 3.2, with zero change in recall. Here's how I got there — including the point where my own eval tried to lie to me.


Building a Golden Eval (and Catching a Lie In It)

To measure retrieval quality honestly you need a golden dataset: a fixed set of queries with a known-correct answer, scored the same way every time. I wrote five pairs/singletons of deliberately confusable documents — a Q2-vs-Q3 infra cost report, an original-vs-renewal logistics contract, a remote-work policy — and ten queries with an exact expected fact for each, scored by whether that fact appears anywhere in the retrieved chunks.

First run: 10/10 recall. 100%. Great, right?

No — it was a lie. My test corpus only had 10 total chunks, and _GLOBAL_TOP_K=8 was returning 80% of the entire corpus on every single query. Recall was guaranteed to be ~100% regardless of whether the embeddings were any good at all. The eval wasn't testing retrieval quality; it was testing whether 8 is bigger than 10.

The fix: add 10 unrelated distractor documents (marketing reports, HVAC contracts, benefits guides — plausible business noise) to dilute the corpus to 30 chunks. Before trusting the next run, I manually ranked all 30 chunks by similarity for one query to confirm distractors were actually competing for slots, not just sitting at the bottom:

 1. sim=0.7498  atlas_q3_infra_report (p1)  <-- correct answer
 2. sim=0.6833  atlas_q2_infra_report (p1)  <-- the confusable near-miss
 3. sim=0.2734  borealis_renewal_notes (p1)
 4. sim=0.2408  distractor_q1_marketing_campaign (p2)
 5. sim=0.2146  atlas_q3_infra_report (p2)
 6. sim=0.2091  distractor_q3_sales_pipeline_report (p2)
 7. sim=0.2030  borealis_vendor_contract (p2)
 8. sim=0.1947  distractor_q3_sales_pipeline_report (p1)
 9. sim=0.1887  distractor_customer_support_sla_tiers (p2)
   ... (noise floor continues down to 0.03)

That's a real, non-trivial top-8: two genuinely relevant chunks, then six chunks that are a mix of tangentially-related and outright noise, with distractors interleaved throughout — not just parked at the bottom. The eval was finally measuring something real.

A rule of thumb worth stating plainly: a "too easy" eval dataset is worse than no eval at all, because it produces a confident number instead of an honest "I don't know." If I'd shipped on that first 100%, I'd have shipped on a number that meant nothing.


The Real Baseline

With a 30-chunk corpus and a confirmed-real top-8, the baseline came back:

VariantRecallZero-result queriesAvg results/query
Baseline (flat top-8)10/10 (100%)08.0

Still perfect recall — but now for a real reason worth looking at: the similarity gap above (0.75 / 0.68 for the real hits, then a cliff to ~0.1–0.27) shows the correct answer was never in danger. What was happening on every query was LIMIT 8 faithfully returning 6 chunks of noise alongside the 2 that mattered, because the query has no concept of "good enough" — it only knows "closest N."

That's context Claude has to read and reason over on every single question, for no benefit. The eval's job at this point was to find out if trimming it would cost anything.


The Fix: A Similarity Threshold

The lever: widen the candidate pool, then filter by a similarity floor instead of a flat count.

-- before
SELECT dc.chunk_index, dc.page_number, dc.content,
       1 - (dc.embedding_vec <=> CAST(:embedding AS vector)) AS similarity, ...
FROM document_chunks dc JOIN documents d ON dc.document_id = d.id
WHERE d.owner_id = CAST(:owner_id AS uuid) AND d.status = 'completed'
  AND dc.embedding_vec IS NOT NULL
ORDER BY dc.embedding_vec <=> CAST(:embedding AS vector)
LIMIT :top_k   -- always 8, no matter the score
-- after
SELECT chunk_index, page_number, content, similarity, document_name, document_id FROM (
    SELECT dc.chunk_index, dc.page_number, dc.content,
           1 - (dc.embedding_vec <=> CAST(:embedding AS vector)) AS similarity, ...
    FROM document_chunks dc JOIN documents d ON dc.document_id = d.id
    WHERE d.owner_id = CAST(:owner_id AS uuid) AND d.status = 'completed'
      AND dc.embedding_vec IS NOT NULL
    ORDER BY dc.embedding_vec <=> CAST(:embedding AS vector)
    LIMIT :pool_size   -- widened to 20
) pool
WHERE similarity >= :threshold   -- the actual filter
ORDER BY similarity DESC
LIMIT :top_k   -- kept as a safety ceiling, now a cap rather than a target

I swept three thresholds against the real baseline before touching production code:

ThresholdRecallZero-result queriesAvg results/query
≥ 0.310/10 (100%)03.2
≥ 0.459/10 (90%)1—
≥ 0.66/10 (60%)4—

0.3 is the clean win: identical recall, ~60% less retrieved content. 0.45 already drops a real hit (a Borealis contract counter-offer query came back empty). 0.6 is too aggressive — four of ten queries returned nothing at all. I didn't pursue reranking on top of this; the plan going in was to only reach for it if a threshold alone wasn't already a clear win, and 0.3 was.


Shipping It

Threshold = 0.3, pool widened to 20, capped at 8 after filtering — applied to both the cross-document search (find_relevant_chunks_global) and the per-document chat retrieval (find_relevant_chunks, with a smaller pool of 15 capped at 5). The per-document path wasn't covered by this eval — it searches within a single document, where off-topic noise is structurally less likely — so that second change is the same logic applied on reasoning, not on a measured number, and I'm saying so rather than implying both are equally proven.

After shipping, I re-ran the eval one more time — not against a parallel script copy, against the actual updated rag_service.py function — and got back exactly 10/10 recall, 3.2 avg results/query. The production code now measurably does what the experiment said it would.


Lessons Learned

A dataset that's too easy for your retrieval system is a bug in the eval, not a compliment to the system. 100% recall on a 10-chunk corpus against a top-8 limit told me nothing except that 8 ≥ 10. The fix wasn't to trust the number — it was to make the test hard enough to possibly fail.

LIMIT k and "relevant" are different claims. A flat top-k always gives you an answer to "what are the k nearest things," whether or not any of them are actually close. Looking at the raw similarity scores — not just whether retrieval technically returned something — is what surfaced the real problem: a sharp cliff between signal and noise that a fixed count can't see.

Recall and precision are different experiments, and recall alone can hide a real win. Every threshold variant that passed had identical recall to baseline — the win here was never "find more correct answers," it was "stop paying context budget for noise you were already getting right despite." A recall-only eval would have called 0.3 a no-op; looking at result count is what made the actual improvement visible.

A 10-query golden set is honest-sized, not rigorous-sized. I'm not pretending otherwise: ten hand-written queries are enough to catch an obvious ceiling effect and compare a few threshold values, not enough to claim 0.3 is provably optimal across all possible documents. The threshold that ships is the one measured to work on this dataset — worth watching in production, not worth overclaiming in a blog post.


GitHub

Key code samples (RAG service, Lambda processor, CDK stacks) are available on GitHub:

👉 github.com/sanjaypatoliya/ai-document-processor-sample


About the Author

I'm Sanjay Patoliya — AWS Certified Generative AI Developer Pro, Solutions Architect & DevOps Engineer building production-ready AI systems on AWS. If you're looking to build something similar or need a remote AWS + AI contractor, feel free to reach out.