Visual explanation of Rerankers in RAG

Vector search gets you the rough top-k. Rerankers fix the order. An interactive demo running a real cross-encoder in your browser.

In the embedding visualization post you watched sentences cluster by meaning. That clustering is how a vector database picks its top-k candidates for RAG. Fast and good enough — but it is not the most accurate retrieval you can do.

Rerankers are the second stage. They look at every (query, document) pair together and give each one a fresh relevance score. They are slower than embedding search, so you only run them on the top 20-50 candidates. The combination — fast vector retrieval, then careful reranking — is the standard production RAG pattern in 2026.

This post lets you watch a reranker in action. Type a query and the same all-MiniLM-L6-v2 embedder from the last post picks the top 5 candidates from a small corpus. Then a cross-encoder (ms-marco-MiniLM-L-6-v2) reranks those 5 and you can see exactly which rows moved.

Loading embedder and cross-encoder… (~60MB total, only on first visit)

Run a query to see vector-search vs reranker output side-by-side.

left: top 5 by cosine similarity (fast) · right: same 5 reranked by cross-encoder (accurate)

Why a second pass even helps

The intuition that trips most people up: if embeddings already capture meaning, why do we need a second model?

The answer is that bi-encoders (embedding models) and cross-encoders see the world very differently:

Bi-encoder (embedding)used for vector searchquerydocumentencoderencoder[0.12, -0.4, …][0.10, -0.3, …]cosine similarityCross-encoder (reranker)used to refine the top-kquery + [SEP] + documentconcatenated into ONE inputcross-attention transformertokens of query attend to tokens of docsingle relevance scoree.g. 8.42 or -2.1

A bi-encoder embeds the query and each document separately, then compares vectors with cosine similarity. That separation is the whole point — you embed your documents once, store the vectors, and at query time you only embed the query and do a fast nearest-neighbor lookup. It scales to billions of documents.

A cross-encoder does no such thing. It concatenates the query and a candidate document into one input and runs them through a transformer together. Every token of the query attends to every token of the document. The model can pick up on fine-grained interactions that get lost when you collapse a sentence into a single vector. The output is a single relevance score, not an embedding.

The cost: you cannot precompute anything. Every query has to do a full forward pass per candidate document. That is why rerankers only run on the top-k from a faster first stage — typically 20-100 candidates, not 100,000.

The two-stage pattern in practice

This is what every production RAG pipeline above the toy level looks like:

Queryfrom the userStage 1: Vector searchbi-encoder + ANNmillions of docs → top 50Stage 2: Rerankercross-encoder50 docs → top 5LLM context5 best chunkscheap~10ms per query~100ms per queryexpensivefunnel: cast a wide net cheaply, then filter precisely

The numbers are illustrative — real systems vary. The point is the funnel: each stage is more expensive and more accurate than the last, so you only pass it the survivors from the previous stage. By the time the LLM sees anything, you have spent compute proportional to result quality, not corpus size.

How to read the demo above

When you ran a query, the left column shows what vanilla vector search returned, sorted by cosine similarity. The right column shows the same 5 documents reranked, sorted by the cross-encoder score (which can be any real number, not just 0-1 like cosine).

What to notice:

  • Score scales are different. Cosine ranges roughly -1 to 1. The cross-encoder outputs raw logits, often -10 to +10. Absolute scores between the two columns are not comparable — only the order within each column matters.
  • Reranker is more decisive. Vector-search scores within the top 5 are often clustered tight (0.45, 0.43, 0.41…). The reranker spreads them out far more, making the “best” result obvious.
  • Sometimes the order is identical. When the corpus is small and queries are clear, vector search already gets it right and reranking just confirms. The reranker earns its keep on ambiguous queries or near-duplicates.

Try queries that mix multiple concepts — “how to debug performance issues in production” — to see the reranker really matter. The bi-encoder might surface anything containing “debug” or “production”; the cross-encoder picks the one that actually addresses both.

When NOT to use a reranker

Like every component in RAG, rerankers have a cost-benefit. Skip them when:

  • Latency is critical. A reranker adds 50-300ms per query depending on hardware. For real-time chat over a small corpus, it may not be worth it.
  • Your corpus is tiny. Under ~1000 documents, the top-5 from cosine is usually as good as it gets. Reranking is most valuable when you have many near-misses to disambiguate.
  • You cannot evaluate quality. Without a test set of (query, ideal-answer) pairs, you cannot know whether the reranker actually helped. It is possible to make things worse with a poorly chosen model.

Production options

If you want to use a reranker in a real system, the popular choices in 2026 are:

  • Cohere Rerank v3 — API-based, very strong, multi-lingual, ~$1 per 1k searches
  • bge-reranker-large — open source, top-tier quality, requires GPU for production latency
  • cross-encoder/ms-marco-* family — small, fast, decent quality — what this demo uses
  • Voyage rerank-2 — newer entrant, competitive on benchmarks

The decision is mostly about latency budget and whether you can host a GPU. For most teams shipping a RAG product, Cohere’s API is the lowest-friction option. For privacy-sensitive deployments, self-hosting bge-reranker-large is the standard play.

The code

The two-stage pipeline is conceptually compact:

// Stage 1: cheap vector search
const queryEmbed = await embedder(query, { pooling: 'mean', normalize: true });
const candidates = documents
.map((doc, i) => ({ doc, score: cosineSim(queryEmbed, docEmbeddings[i]) }))
.sort((a, b) => b.score - a.score)
.slice(0, 5);
// Stage 2: expensive but accurate rerank
const rerankPromises = candidates.map(c =>
reranker({ text: query, text_pair: c.doc })
);
const reranked = await Promise.all(rerankPromises);
const finalRanked = candidates
.map((c, i) => ({ doc: c.doc, score: reranked[i][0].score }))
.sort((a, b) => b.score - a.score);

That’s the entire pattern. Two model calls per query, glued together with array sorts.

What I would build next

Next post in this thread: I’ll wire all three stages — embedding, vector store, reranker — into a single working RAG endpoint running on my homelab, and we’ll watch how a real query flows through every stage with actual latency numbers.

// discussion

Comments

Replies use GitHub Discussions. You'll need a GitHub account to post.