Embedding models: when a bigger one is worth it, and how to tell before you index anything

AIEvie3 repliesThis topic is about huggingface/transformers

3
EvieMember#1

The first decision in a RAG system is which embedding model to use, and it is the one decision that is most expensive to undo, because the vectors are baked into the index. Here is how I would choose, having got it wrong once.

What you are actually choosing

Three things, and people talk about the first one only.

The dimension. The length of the vector the model returns. It is fixed per model. 384, 768, 1024, 1536, 3072, and a few thousand for the very large ones. It sets the storage cost of your index, and it is the thing you cannot change without re-embedding everything.

The maximum input length. How many tokens of text the model will encode in one go. 256 is common for the small general-purpose models, 512 for some, and the good ones go to 512, 1024 or 8192. If your chunk is 400 tokens and the model takes 256, you are silently truncating, and you will not get an error.

The training domain. A model trained on code and documentation embeds identifiers and API names very differently from a model trained on natural language paragraphs. If your corpus is API documentation, a general-purpose model will still work, and a code-trained one will be noticeably better.

The rule I now use

Index with the smallest model that is good enough, because the cost of a bigger model is paid on every query forever and the benefit is not.

The way to know it is good enough is to build a small set of questions with the right document, and measure recall at 5 before you index the whole corpus. Thirty questions and an afternoon. If the small model gets 80% of them, a bigger model might get 90%, and that 10% is not worth doubling your index size and your query latency.

The number people forget

The embedding cost of indexing is small. The generation cost of answering is large. So the lever that matters is not "which embedding model", it is "how many chunks do you send to the model per question" and "how long is your system prompt".

/tools/token-counter on the chunks and the system prompt, then /tools/llm-cost-calculator for the monthly figure. /learn/embeddings has the vocabulary; /tools/vector-similarity-calculator is for checking two vectors by hand.

3 replies

IvanMember#2

The truncation point is the one that catches people. A model with a 256-token limit and a chunker that produces 400-token chunks does not fail, it just embeds the first 256 tokens of every chunk, and the part of the chunk that carried the answer is often the end.

Check the model's maximum length against your chunk size before you index, and if the chunker is the problem, fix the chunker rather than the model.

Devn NieMember#3

In reply to Ivan (#2)

Seconded on the measurement, and the framing that helped me: the embedding model is a one-off cost per document plus a per-query cost of a few milliseconds. The thing that scales with your traffic is the generation.

That is why the decision order for me is: prompt length, then top-k, then the model. The embedding model is last, and it is last because it is the one you are least likely to be able to change later.

EvieAuthorMember#4

In reply to Devn Nie (#3)

Agreed, and I would put the "which chunk size" decision even earlier, because it decides whether the embedding model's truncation limit matters at all.

The order I would use: chunk size that matches what the model can encode, then the cheapest model that passes thirty questions, then the prompt and top-k, then the model. The embedding model gets chosen last, and by then you have the numbers to know whether changing it is worth a re-index.