TL;DR
A bi-encoder is a pair of encoders, often sharing weights, that map a query and an item to vectors separately. The score for a pair is a simple similarity between the two vectors, such as a dot product or cosine. Because the item encoder never sees the query, every item can be embedded once, ahead of time, and stored in an index.
That separation is the whole point. A query is embedded once at search time, and finding the best items becomes a nearest-neighbour lookup over precomputed vectors. The cost is that the two sides cannot look at each other, so the scores are less precise than a model that reads the pair together.
Where you'll see it: in the retrieval step of retrieval-augmented generation systems, in semantic search over documents, in image-text models such as CLIP, and in the candidate-generation stage of large recommender systems.
Intuition
Two translators, one shared language
Imagine a library where every book is summarised on a card, and every reader describes what they want in a few sentences. A librarian who reads the whole book and the whole request together, every time, would be very accurate and very slow. A faster librarian converts each book into a fixed set of coordinates once, writes them on the shelf, and converts each request into coordinates when it arrives. Finding a match is then just finding the nearest coordinates on the shelf.
That is a bi-encoder. The two encoders are the two translators, and the vectors are the shared language. They must be trained so that a query and a relevant item end up with nearby coordinates, while unrelated pairs end up far apart. This is an instance of Representation Learning: the model learns features in which the task becomes simple, here a geometric one.
flowchart LR
subgraph Offline
D[Document] --> DE[Item encoder]
DE --> DV[Document vector, stored in index]
end
subgraph Online
Q[Query] --> QE[Query encoder]
QE --> QV[Query vector]
QV --> NN[Nearest-neighbour search]
DV --> NN
NN --> R[Top-k documents]
end
Why the two sides are kept apart
The alternative is a cross-encoder, which concatenates the query and the item into one input and runs a single network over both. That lets every token of the query attend to every token of the item, which usually gives a better score. But it also means the network must run once per pair. To search a million items, it runs a million times for each query. A bi-encoder runs the query network once and does a fast vector lookup for the rest.
So the usual design is two stages. A bi-encoder retrieves a shortlist of a few hundred candidates from the whole collection, and a cross-encoder re-ranks only that shortlist. The bi-encoder's job is recall at low cost; the cross-encoder's job is precision on a small set.
The math
The scoring function
Let be the query encoder and the item encoder. Each maps its input to a vector of dimension :
Here is the query, is an item, and and are their embeddings. The score is a similarity between the two vectors. The two common choices are the dot product
and the cosine similarity
which is the dot product after both vectors are scaled to length one. Cosine ignores length and compares only direction. Dot product lets length carry information, for example how general an item is, but it can favour long vectors.
The important structural fact is that depends on only through and on only through . Nothing inside the encoders couples them. That is what allows the item vectors to be computed in advance.
Training with in-batch negatives
Training data is a set of pairs where is a relevant item for . For a batch of pairs, the other items in the batch serve as negatives for each query. The loss for query is the InfoNCE form
where is a temperature. The denominator is a Softmax over the batch, so this is the Cross-Entropy Loss of picking the correct item out of candidates. The loss is small when the true item scores highest by a clear margin. Using a larger batch gives more negatives per query and usually better retrieval, at the cost of memory.
Searching the index
Given a query vector and stored item vectors , the exact top- answer is
which means the indices with the largest scores. Doing this exactly costs dot products of length per query. Approximate indexes, such as graph-based or clustered inverted-file structures, trade a little recall for much less work. This problem is the Maximum Inner Product Search problem when the dot product is used.
Worked examples
Example 1: dot product versus cosine
Take a query and two items, and .
- Dot products: and . The dot product ranks first.
- Lengths: , , .
- Cosines: for , . For , . Cosine ranks first.
- Interpretation: points exactly in the query's direction, but is short. is long and only partly aligned. Which answer is right depends on whether length is meaningful in your embedding space. Check this on held-out data before choosing.
Example 2: precomputed items and a toy index
Suppose the item vectors are unit length, in a space of dimension 2, and the index holds three items: , and . A query arrives and is encoded as .
- The three items were encoded before the query arrived. Only the query is encoded now.
- Scores: . . .
- Ranking: item 1, then item 2, then item 3. Since the vectors are unit length, these are also the cosine scores.
- Interpretation: the lookup did three dot products. For a real index the same step runs over millions of stored vectors, which is why approximate search matters.
Example 3: the cost of a cross-encoder
Consider a collection of documents, each represented by a -dimensional float32 vector. The index takes bytes, about gigabytes, and the encoder runs over the documents once.
- Bi-encoder: one forward pass for the query, then dot products of length 768 for exact search. Each dot product is about floating-point operations, so the search is roughly operations. An approximate index touches only a small fraction of that.
- Cross-encoder: one forward pass per query-document pair, so full transformer passes for one query over the whole collection.
- Two-stage: a bi-encoder returns the top 200 candidates, and the cross-encoder runs 200 passes instead of .
Interpretation: the document side is paid for once, offline. The per-query cost of the bi-encoder is independent of how expensive the item encoder is, which is why bi-encoders dominate large-scale retrieval. The cross-encoder is affordable only on a short list.
Tradeoffs and failure modes
Bi-encoders are fast and scale to very large collections, but they lose information by compressing each side to one vector.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Fine distinctions between close candidates | One vector cannot represent every detail, so near-duplicates are ranked poorly | Re-rank the shortlist with a cross-encoder |
| Query and item vocabularies differ, and training pairs are scarce | The encoders learn weak alignment and recall drops | Add hard negatives, or fine-tune on in-domain pairs |
| Items change often | Each change needs a re-embedding and an index update | Re-embed only the changed items and update the index incrementally, or accept a staleness window |
| Scores must be calibrated across queries | Raw dot products vary in scale from query to query | Use cosine, or normalise scores before thresholding |
| Exact recall is required | Approximate search misses some true neighbours | Use exact search for small collections, or tune the index for higher recall |
Notable failure in practice: in-batch negatives are easy when the batch is random, because most other items are obviously unrelated. Hard negatives, drawn from the same topic, make the task harder and usually improve the model.
History
Dual-encoder ideas appeared in the 2010s in recommender systems and in semantic matching. The "two-tower" name comes from those systems, where a user tower and an item tower are trained together. In natural language, Reimers and Gurevych's Sentence-BERT (2019) showed that training a siamese network on sentence pairs gave useful embeddings that could be compared with a cosine score, which made fast semantic search practical. Karpukhin and colleagues (2020) applied the same design to open-domain question answering, with in-batch negatives and a dense index, and that paper is the reference point for the dense retriever in later retrieval-augmented systems. In 2021, CLIP used the same structure to match images and captions, training an image encoder and a text encoder with a contrastive loss.
The design caught on because it turns a search over a large collection into an index lookup, which can be built once and served cheaply.
Check yourself
QuestionWhat is the key property that lets a bi-encoder precompute its index?Show answer
The item encoder never sees the query. The score depends on the query only through its own vector and on the item only through its own vector, so every item can be embedded once and stored, and only the query is embedded at search time.
QuestionA query vector is and an item vector is . What are the dot product and the cosine similarity?Show answer
The dot product is . The lengths are and , so the cosine is .
QuestionWhy does a bi-encoder usually score worse than a cross-encoder on a single pair, and why is it still used for retrieval?Show answer
A cross-encoder reads the query and item together, so every token can attend to the other, which gives finer scores. A bi-encoder must compress each side to one vector, which loses detail. It is still used because a cross-encoder must run once per pair, while a bi-encoder runs the query once and then does a vector lookup, so it can search millions of items.
QuestionIn a training batch of 64 query-item pairs, how many negatives does each query get under in-batch negatives, and what does the loss measure?Show answer
Each query gets 63 negatives, the other items in the batch. The loss is the cross-entropy of picking the true item out of the 64 candidates under a softmax over their scores, so it is small when the true item scores highest by a clear margin.
QuestionA team finds that a bi-encoder retrieves the right document in its top 200 but ranks it low, while search quality is otherwise acceptable. What change is most likely to help, and why?Show answer
Add a cross-encoder re-ranker over the top 200 candidates. The bi-encoder already gives good recall, so the problem is precision among close candidates, which a pair-reading model handles better. Re-ranking 200 pairs is affordable, while re-ranking the whole collection is not.