Sub topic 3
How a cross-encoder reads query and passage together
This chapter assumes you know what text embeddings are, how vector similarity search works, and roughly what a transformer is. It builds on the previous chapter, where a bi-encoder's single-vector similarity missed fine-grained matches between a query and a passage.
The core idea
A cross-encoder is a transformer that takes the query and the passage as one input and outputs a single relevance score. Because both texts sit in the same input, every query token can attend to every passage token. The model can notice that "rotate" in the query lines up with "creating a new key and deleting the old one" in the passage.
A bi-encoder works differently. It encodes the query and the passage separately into vectors, and the two texts never see each other. Only the final cosine similarity compares them.
What the input looks like
The tokenizer packs the pair into one sequence, with a separator token between the two texts. For BERT-style models it looks like this:
[CLS] how do i rotate an api key [SEP] api keys can be rotated from the security page ... [SEP]The model also receives segment (token type) IDs that mark which tokens belong to the query and which to the passage. Exact special tokens vary by model family, so always use the tokenizer that ships with the model.
The transformer then runs self-attention over the whole sequence. The final hidden state of the [CLS] token is passed to a small classification head, a linear layer, which produces the score. Many rerankers have one output logit. The score is not an embedding, and you cannot store it for later reuse.
Running one
The sentence-transformers library wraps the tokenizing, batching, and scoring steps:
from sentence_transformers import CrossEncoder
model = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2", max_length=512)
query = "How do I rotate an API key?"
passages = [
"API keys can be rotated from the Security page by creating a new key and deleting the old one.",
"Our API supports JSON and XML response formats.",
"To change your password, open Account Settings and choose Reset password.",
]
scores = model.predict([(query, p) for p in passages])
ranked = sorted(zip(passages, scores), key=lambda x: x[1], reverse=True)
for passage, score in ranked:
print(f"{score:.2f} {passage}")The input to predict is a list of (query, passage) pairs. The output has one score per pair. Sorting by score gives the reranked order. The key-rotation passage should come first, but the exact numbers depend on the model version.
The cost of reading jointly
The joint input is what makes a cross-encoder accurate, and it is also what makes it slow.
| Row | Bi-encoder | Cross-encoder |
|---|---|---|
| Passage processing | Once, offline | Again for every query |
| Stored index | Vectors | None possible |
| Cost per query | One query encoding plus a vector search | One forward pass per candidate |
| Interaction between texts | Only at the final similarity | At every attention layer |
A cross-encoder cannot precompute anything for a passage, because the passage's representation depends on the query. Scoring a million passages would need a million forward passes for each query. This is why cross-encoders are used only on a short candidate list. The next chapter builds that pipeline.
Exercise
Why can a cross-encoder not precompute and store a vector for each passage the way a bi-encoder does?
Show answer
A cross-encoder's computation depends on the query and the passage together. Its attention layers mix the two texts from the first layer on, so there is no query-independent passage representation to store. Each (query, passage) pair needs its own forward pass at query time.