🤖 AI/ML

Reranking

Reranking

A technique that reorders search results to improve relevance. Used in RAG pipelines.

📖 Detailed Explanation

Reranking is a technique that improves relevance by re-ordering initial search results with a more sophisticated model. In the first stage, candidates are selected through fast retrieval (sparse retrieval or dense retrieval), and in the second stage, a precise model such as a cross-encoder directly compares the query with each document to readjust the ranking.

The background of reranking is the accuracy-speed trade-off in search systems. Sparse retrieval such as BM25 is fast but misses meaning, and a bi-encoder is fast but lacks interaction between the query and the document. A cross-encoder is accurate but slow, so a two-stage structure solves this problem. BERT-based rerankers emerged in 2020 and became mainstream.

The core principle of reranking is cross-attention. When the query and the document are input into the model together, the model captures the fine-grained semantic relationship between the two. Unlike a bi-encoder, which embeds each of them independently, a cross-encoder directly scores "how relevant this document is to this query." It is usually applied to the top-20 to 100 candidates.

In 2024-2025, reranking became an essential component of RAG systems. Commercial services such as the Cohere Rerank API, Jina Reranker, and BGE Reranker are growing, and they improve LLM response quality by 10-30%. The effect is especially large for complex questions and long documents. LangChain and LlamaIndex both support reranking by default.

📊 Performance & Cost

RerankerModel SizeNDCG@10SpeedCost
Cohere Rerank v3Proprietary~0.72~100ms/1000 docs$1/1K searches
BGE-reranker-large560M~0.70~50ms/100 docsOpen Source
Jina Reranker v2137M~0.68~30ms/100 docsFree tier available
cross-encoder/ms-marco110M~0.65~40ms/100 docsOpen Source

* As of January 2025, based on the MS MARCO benchmark

💻 Code Example

# RAG 파이프라인에서 리랭킹 적용
from sentence_transformers import CrossEncoder
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings

# 1단계: 벡터 검색으로 후보 추출
embeddings = OpenAIEmbeddings()
vectorstore = Chroma(embedding_function=embeddings)

query = "LLM의 할루시네이션을 줄이는 방법"
initial_docs = vectorstore.similarity_search(query, k=20)

# 2단계: Cross-Encoder로 리랭킹
reranker = CrossEncoder('BAAI/bge-reranker-large')

# 쿼리-문서 쌍 생성
pairs = [[query, doc.page_content] for doc in initial_docs]

# 관련성 점수 계산
scores = reranker.predict(pairs)

# 점수로 재정렬
reranked = sorted(
    zip(initial_docs, scores),
    key=lambda x: x[1],
    reverse=True
)

# 상위 5개 선택
top_docs = [doc for doc, score in reranked[:5]]

print("🔝 리랭킹 후 상위 문서:")
for i, (doc, score) in enumerate(reranked[:5]):
    print(f"{i+1}. [점수: {score:.4f}] {doc.page_content[:80]}...")

# Cohere API 사용 예시
import cohere
co = cohere.Client("your-api-key")

results = co.rerank(
    query=query,
    documents=[doc.page_content for doc in initial_docs],
    top_n=5,
    model="rerank-v3.0"
)

for r in results.results:
    print(f"순위 {r.index}: 점수 {r.relevance_score:.4f}")

🗣️ Say It Like This in Practice

💬 In a Meeting
"The low RAG response quality is a retrieval problem. If we apply Cohere Rerank after vector search, the Hit Rate of relevant documents goes up from 65% to 85%. The monthly cost is expected to be around 1000 dollars."
💬 In an Interview
"Reranking is the core of 2-stage retrieval. We quickly extract the top-100 candidates with a bi-encoder, and precisely analyze the query-document interaction with a cross-encoder. Speed becomes slightly slower, but accuracy improves significantly, which improves the quality of the LLM context."
💬 In a Technical Discussion
"BGE-reranker-large is the best among open-source models, but in production Cohere v3 is more stable. The performance gap is especially large for multilingual queries. Considering cost-effectiveness, a commercial API is better."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Common Mistakes & Cautions

❌
First-stage retrieval results are too few (k=5)

Reranking reorders among the documents that exist. If there are no relevant documents in the first stage, reranking is meaningless too. Start with at least k=20~50.

❌
Too many reranking targets (k=1000)

A cross-encoder has O(n) cost. Reranking 1000 documents can take several seconds. 100-200 is the appropriate range.

✅
The right way

Extract enough in the first stage with k=50~100, and pass the top-5~10 after reranking to the LLM. Also consider fine-tuning a domain-specific reranker.

🔗 Related Terms

📚 Learn More