Sub topic 3
Input Length, Cost, and Model Choice in Reranking
Assume you know Python, embeddings, vector search, and basic retrieval terms. This chapter focuses on the second stage: scoring a small candidate set more carefully.
Input length, truncation, and chunking
A cross-encoder reads the query and passage together, so its maximum input length applies to both. If a passage is too long, the tokenizer may truncate its ending. Important evidence can disappear without producing an obvious error.
Chunk long documents before indexing. Keep chunks semantically coherent and store their document IDs. During reranking, inspect the tokenized length and use a consistent truncation policy.
def make_pairs(query, passages, max_chars=1200):
pairs = []
for passage in passages:
shortened = passage[:max_chars]
pairs.append((query, shortened))
return pairs
query = "How do I rotate an API key?"
passages = ["Security instructions " * 300, "Short API-key instructions."]
print(make_pairs(query, passages))Latency, batching, and cost
Cross-encoder work grows with the number of query-passage pairs. Retrieve broadly enough to protect recall, then rerank a smaller depth such as 20–100. Batch pairs to improve hardware utilization, but measure memory use and tail latency.
Failure modes and debugging
A weak candidate pool cannot be repaired by reranking. Check first-stage recall before changing the reranker. Other common causes include truncation, domain mismatch, duplicate chunks, and batches that exceed memory.
Log candidate IDs, input lengths, scores, and final ranks for representative queries. Compare the original and reranked lists.
Combining lexical and dense retrieval
Lexical search matches exact terms. Dense search matches meaning. Run both, then merge their ranked lists with reciprocal rank fusion before reranking. This protects identifiers and rare terms while retaining semantic matches.
Training data and hard negatives
Training examples contain a query, a relevant passage, and negatives. Hard negatives are plausible but incorrect passages from the retriever. They teach the model to distinguish close alternatives.
Training objectives
Pointwise training predicts a relevance label per pair. Pairwise training teaches that one passage should outrank another. Listwise training considers an entire ranked list. Pairwise or listwise objectives often fit reranking better than isolated labels.
Choosing and adapting a model
Choose a model using measured recall, ranking quality, latency, language coverage, and maximum length. Fine-tune it when your domain has specialized terms or relevance rules. Keep a held-out evaluation set to detect overfitting.
Cross-encoders versus LLM-based rerankers
Cross-encoders are usually faster, cheaper, and easier to score consistently. LLM-based rerankers can follow complex instructions and explain decisions, but they cost more and may vary between runs. Use an LLM when reasoning requirements justify that cost.
Exercise
What should you check before increasing the reranker’s candidate depth?
Show answer
Check first-stage recall and input truncation. More candidates help only when relevant passages are retrieved and their evidence reaches the model.