The basic retrieval pipeline
- Cut documents into small chunks (within a few hundred characters)
- Convert each chunk into a vector with an embedding model
- Store them in a vector database
- When a user asks, find the most similar chunks
- Put those chunks into the prompt and let the model answer
The pipeline is simple, but the first step plants the pitfall.
The problem of chunks losing context
Example: a financial report is cut into small chunks, and one chunk reads “the company’s revenue grew 3% versus the previous quarter”.
- Which company?
- Which quarter?
- Growth relative to what?
Away from the original, this chunk is hard to retrieve and the model cannot judge whether it is relevant.
Improvement one: add one contextual sentence per chunk
After chunking and before indexing, have the model write a short locator sentence for each chunk (about 50–100 characters) and prepend it to the text:
Original: The company's revenue grew 3% versus the previous quarter.
Added context: This excerpt comes from ACME's 2023 Q2 financial report; the previous quarter's revenue was 314 million USD.
After that, build the vector and keyword indexes. Now each chunk carries “who I am and where I come from”.
The reported result: this approach reduced retrieval failure rate by about 49%, and by about 67% when combined with reranking (research evaluation results; actual effect depends on the data).
Improvement two: hybrid retrieval
- Vector retrieval: good at semantic similarity, but may miss exact terms
- Keyword retrieval (BM25): good at exact matches (such as the error code “TS-999” or product models)
Approach: run both, fuse the rankings into a union and deduplicate.
Improvement three: reranking
After retrieving many candidates, use a more refined model to reorder them and put the most relevant first. It costs more than retrieval, but is usually worth it.
When not to use retrieval
When the knowledge base is small (within a few hundred pages), putting the full text straight into the prompt is simpler and more accurate, and can also use caching to lower cost.
The criteria:
- Is the knowledge base small enough to fit in the context
- Does it update often (frequent updates suit retrieval better)
- Do answers need sources
Practical recommendations
- Measure the knowledge-base size first to decide “put it in directly” or “retrieve”
- Keep title, source and date metadata when chunking
- Add one locator context sentence to each chunk before indexing
- Use hybrid vector + keyword retrieval
- Add a reranking layer where possible
- Evaluate retrieval with a batch of real questions and check the hit rate
Checklist
- Knowledge-base size and update frequency are assessed
- Chunks carry title / source / date
- Every chunk has locator context
- Hybrid vector and keyword retrieval
- Reranking exists (or is planned)
- Hit rate evaluated with real questions
Reference: key points adapted from Anthropic’s engineering blog post “Introducing Contextual Retrieval”, rewritten for general knowledge-base scenarios.
Questions
How big does a knowledge base need to be before retrieval is worth it?
When the knowledge base is small (say within a few hundred pages), putting it straight into the prompt is often simpler; beyond that scale, or when it updates often, retrieval pays off.
Why do chunks lose context?
A cut-out fragment may lack its subject, time and source. For example, 'quarterly revenue grew 3%', away from the original, does not say which company or which quarter, so retrieval cannot match it.
Should hybrid retrieval and reranking be used together?
We suggest adding hybrid keyword (BM25) plus vector retrieval first, then considering reranking; reranking costs more but pushes the most relevant results to the front.