Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
AI/MLJune 21, 20264 min read

Optimizing RAG Retrieval: A Practical Guide to Semantic Reranking

Master semantic reranking to improve your RAG retrieval accuracy. Learn how to implement cross-encoders to filter noisy search results and boost precision.

RAGAI EngineeringPythonVector SearchSemantic RerankingMachine LearningAILLMPrompt Engineering
Selective focus close-up of the word justice in a dictionary, emphasizing concept clarity.

Last month, I spent three days debugging a "hallucination" issue in our internal documentation bot. It wasn't the LLM failing; it was the retrieval layer returning semantically similar but contextually irrelevant chunks. I had built a standard vector search using OpenAI’s text-embedding-3-small, but the nuance of our technical docs was getting lost in the high-dimensional noise.

If you’re building RAG pipelines, you eventually hit a wall where vector search isn't enough. You need semantic reranking to bridge the gap between "kinda related" and "actually answers the user's question."

Why Vector Search Falls Short

Vector search relies on cosine similarity in a shared embedding space. It’s fast and scales well, but it’s essentially a blunt instrument. It captures global meaning, not specific intent.

When a user asks, "How do I reset my password in v2.4?", a standard vector search might return a generic page about account security or a document about v1.0 password resets because the vectors are "near" each other. This is where hybrid search in RAG pipelines helps, but even with keyword matching, you often get a messy list of top-K results.

Implementing Semantic Reranking

Selective focus on Arabic script in an open book, showcasing elegant calligraphy.

Semantic reranking introduces a second stage to your retrieval process. Instead of trusting the top 5 results from your vector database, you retrieve a larger candidate set (say, 20-50 chunks) and pass them through a cross-encoder model to score their actual relevance to the query.

A cross-encoder is a model that takes both the query and the document as a single input pair. It’s computationally expensive compared to bi-encoders, but its precision is significantly higher because it can model the interaction between query tokens and document tokens.

The Workflow

  1. Initial Retrieval: Perform a standard vector or hybrid search to get the top 50 candidates.
  2. Scoring: Use a reranker model (like Cohere Rerank or BGE-Reranker) to assign a relevance score to each candidate.
  3. Filtering: Sort by score and keep only the top 3-5 segments.
  4. Generation: Pass the refined, high-precision context to the LLM.

Practical Implementation in Python

I’ve been using sentence-transformers for local reranking. It’s straightforward to drop into an existing pipeline.

PYTHON
from sentence_transformers import CrossEncoder

# Load a pre-trained cross-encoder
model = CrossEncoder(CE9178">'cross-encoder/ms-marco-MiniLM-L-6-v2')

query = "How to reset password in v2.4"
candidates = ["General account security page", "v2.4 password reset procedure", ...]

# Score pairs
scores = model.predict([(query, doc) for doc in candidates])

# Pair docs with scores and sort
ranked_results = sorted(zip(candidates, scores), key=lambda x: x[1], reverse=True)

The performance impact is real. My latency increased by about 180ms per query, but the recall at top-3 improved by roughly 25%. For our use case, that trade-off was a no-brainer.

Managing the Latency Trade-off

Adding a reranker isn't free. If you're already controlling LLM cost and latency, you need to be careful. Sending 50 chunks to a cross-encoder can add significant delay to your API response.

Here are a few ways to keep it manageable:

  • Batching: Don't send one pair at a time. Send the entire candidate list to the model in one batch request.
  • Early Exit: If the top candidate from the reranker is below a certain threshold (e.g., 0.5), consider telling the user you couldn't find a good answer rather than feeding the LLM "trash" context.
  • Cache Early: Use semantic caching for RAG pipelines to avoid reranking the same queries repeatedly.

When to Skip Reranking

Wooden letters spelling "WHEN" on a textured burlap surface, ideal for concepts of time and planning.

Don't over-engineer if you don't have to. If your initial vector search is returning highly relevant results consistently, adding a reranker adds complexity and cost for marginal gains. I usually recommend starting without it, measuring your retrieval accuracy with a golden dataset, and only introducing reranking once your "missing context" error rate starts trending upward.

I’m still experimenting with whether it’s better to use a smaller, faster local model or a managed API like Cohere. For now, the local MiniLM model works well enough for our document scale. However, if your documents are highly domain-specific—like legal or medical text—you might find that standard cross-encoders struggle, and you'll need to fine-tune your own.

Retrieval optimization is an iterative game. You’ll never get it perfect on the first try, but implementing reranking is a solid step toward making your RAG app feel like a true expert rather than a search engine that just guesses.

Back to Blog

Similar Posts

View of large industrial pipelines running through a lush forest landscape.
AI/MLJune 20, 20264 min read

Semantic caching for RAG pipelines: Cut latency and costs

Semantic caching for RAG pipelines is the most effective way to slash LLM costs. Learn to implement a vector-based cache to serve similar user requests.

Read more
Detailed image of a modern industrial control panel featuring multiple connectors and buttons.
AI/ML
June 20, 2026
5 min read

LLM Guardrails for Production: Input Validation and Output Filtering

LLM guardrails are essential for production AI. Learn how to implement reliable input validation and output filtering to keep your LLM apps safe and secure.

Read more
Aerial view of a modern highway interchange at dusk with flowing traffic.
AI/MLJune 20, 20265 min read

LLM Routing: A Strategy for Multi-Model Architectures

Master LLM routing to optimize costs and latency in production. Learn how to build a deterministic multi-model architecture for your AI application.

Read more