In my newest put up, I how hybrid search will be utilised to considerably enhance the effectiveness of a RAG pipeline. RAG, in its primary model, utilizing simply semantic search on embeddings, will be very efficient, permitting us to utilise the ability of AI in our personal paperwork. Nonetheless, semantic search, as highly effective as it’s, when utilised in giant information bases, can generally miss actual matches of the person’s question, even when they exist within the paperwork. This weak point of conventional RAG will be handled by including a key phrase search part within the pipeline, like BM25. On this manner, hybrid search, combining semantic and key phrase search, results in way more complete outcomes and considerably improves the efficiency of a RAG system.

Be that as it might, even when utilizing RAG with hybrid search, we are able to nonetheless generally miss necessary data that’s scattered in several components of the doc. This may occur as a result of when a doc is damaged down into textual content chunks, generally the context — that’s, the encompassing textual content of the chunk that kinds a part of its that means — is misplaced. This may particularly occur for textual content that’s advanced, with that means that’s interconnected and scattered throughout a number of pages, and inevitably can’t be wholly included inside a single chunk. Suppose, for instance, referencing a desk or a picture throughout a number of totally different textual content sections with out explicitly defining to which desk we’re refering to (e.g., “as proven within the Desk, earnings elevated by 6%” — which desk?). Consequently, when the textual content chunks are then retrieved, they’re stripped down of their context, generally ensuing within the retrieval of irrelevant chunks and technology of irrelevant responses.
This lack of context was a significant situation for RAG techniques for a while, and several other not-so-successful options have been explored for enhancing it. An apparent try for enhancing this, is growing chunk measurement, however this usually additionally alters the semantic that means of every chunk and finally ends up making retrieval much less exact. One other method is growing chunk overlap. Whereas this helps to extend the preservation of context, it additionally will increase storage and computation prices. Most significantly, it doesn’t absolutely resolve the issue — we are able to nonetheless have necessary interconnections to the chunk out of chunk boundaries. Extra superior approaches trying to unravel this problem embody Hypothetical Doc Embeddings (HyDE) or Doc Abstract Index. Nonetheless, these nonetheless fail to supply substantial enhancements.
In the end, an method that successfully resolves this and considerably enhances the outcomes of a RAG system is contextual retrieval, initially launched by Anthropic in 2024. Contextual retrieval goals to resolve the lack of context by preserving the context of the chunks and, due to this fact, enhancing the accuracy of the retrieval step of the RAG pipeline.
. . .
What about context?
Earlier than saying something about contextual retrieval, let’s take a step again and speak a bit bit about what context is. Positive, we’ve all heard in regards to the context of LLMs or context home windows, however what are these about, actually?
To be very exact, context refers to all of the tokens which might be out there to the LLM and based mostly on which it predicts the following phrase — keep in mind, LLMs work by producing textual content by predicting it one phrase at a time. Thus, that would be the person immediate, the system immediate, directions, abilities, or another tips influencing how the mannequin produces a response. Importantly, the a part of the ultimate response the mannequin has produced to this point can be a part of the context, since every new token is generated based mostly on the whole lot that got here earlier than it.
Apparently, totally different contexts result in very totally different mannequin outputs. For instance:
- ‘I went to a restaurant and ordered a‘ might output ‘pizza.‘
- ‘I went to the pharmacy and acquired a‘ might output ‘medication.‘
A basic limitation of LLMs is their context window. The context window of an LLM is the utmost variety of tokens that may be handed without delay as enter to the mannequin and be taken under consideration to supply a single response. There are LLMs with bigger or smaller context home windows. Fashionable frontier fashions can deal with tons of of 1000’s of tokens in a single request, whereas earlier fashions usually had context home windows as small as 8k tokens.
In an ideal world, we might need to simply go all the data that the LLM must know within the context, and we’d most definitely get excellent solutions. And that is true to some extent — a frontier mannequin like Opus 4.6 with a 200k token context window corresponds to about 500-600 pages of textual content. If all the data we have to present matches this measurement restrict, we are able to certainly simply embody the whole lot as is, as an enter to the LLM and get an important reply.
The problem is that for many of real-world AI use circumstances, we have to make the most of some form of information base with a measurement that’s a lot past this threshold — suppose, for example, authorized libraries or manuals of technical tools. Since fashions have these context window limitations, we sadly can’t simply go the whole lot to the LLM and let it magically reply — we have now to somwhow decide what is a very powerful data that needs to be included in our restricted context window. And that’s basically what the RAG methodology is all about — selecting the suitable data from a big information base in order to successfully reply a person’s question. In the end, this emerges as an optimization/ engineering drawback — context engineering — figuring out the suitable data to incorporate in a restricted context window, in order to supply the absolute best responses.
That is probably the most essential a part of a RAG system — ensuring the suitable data is retrieved and handed over as enter to the LLM. This may be finished with semantic search and key phrase search, as already defined. However, even when bringing all semantically related chunks and all actual matches, there’s nonetheless a great likelihood that some necessary data could also be left behind.
However what sort of data would this be? Since we have now lined the that means with semantic search and the precise matches with key phrase search, what different kind of knowledge is there to contemplate?
Completely different paperwork with inherently totally different meanings might embody components which might be comparable and even an identical. Think about a recipe e-book and a chemical processing handbook each instructing the reader to ‘Warmth the combination slowly’. The semantic that means of such a textual content chunk and the precise phrases are very comparable — an identical. On this instance, what kinds the that means of the textual content and permit us to separate between cooking and chemnical engineering is what we’re reffering to as context.

Thus, that is the form of additional data we purpose to protect. And that is precisely what contextual retrieval does: preserves the context — the encompassing that means — of every textual content chunk.
. . .
What about contextual retrieval?
So, contextual retrieval is a technique utilized in RAG aiming to protect the context of every chunk. On this manner, when a bit is retrieved and handed over to the LLM as enter, we’re in a position to protect as a lot of its preliminary that means as potential — the semantics, the key phrases, the context — all of it.
To realize this, contextual retrieval means that we first generate a helper textual content for every chunk — specifically, the contextual textual content — that enables us to situate the textual content chunk within the authentic doc it comes from. In observe, we ask an LLM to generate this contextual textual content for every chunk. To do that, we offer the doc, together with the precise chunk, in a single request to an LLM and immediate it to “present the context to situate the particular chunk within the doc“. A immediate for producing the contextual textual content for our Italian Cookbook chunk would look one thing like this:
the complete doc Italian Cookbook doc the chunk comes from
Right here is the chunk we need to place throughout the context of the complete doc.
the precise chunk
Present a quick context that situates this chunk throughout the total
doc to enhance search retrieval. Reply solely with the concise
context and nothing else.
The LLM returns the contextual textual content which we mix with our preliminary textual content chunk. On this manner, for every chunk of our preliminary textual content, we generate a contextual textual content that describes how this particular chunk is positioned in its guardian doc. For our instance, this could be one thing like:
Context: Recipe step for simmering do-it-yourself tomato pasta sauce.
Chunk: Warmth the combination slowly and stir often to stop it from sticking.
Which is certainly much more informative and particular! Now there isn’t any doubt about what this mysterious combination is, as a result of all the data wanted for identiying whether or not we’re speaking about tomato sauce or laboratory starch options is conveniently included throughout the identical chunk.
From this level on, we take care of the preliminary chunk textual content and the contextual textual content as an unbreakable pair. Then, the remainder of the steps of RAG with hybrid search are carried out basically in the identical manner. That’s, we create embeddings which might be saved in a vector search and the BM25 index for every textual content chunk, prepended with its contextual textual content.

This method, so simple as it’s, ends in astonishing enhancements within the retrieval efficiency of RAG pipelines. In response to Anthropic, Contextual Retrieval improves the retrieval accuracy by a formidable 35%.
. . .
Lowering value with immediate caching
I hear you asking, “However isn’t this going to break the bank?“. Surprisingly, no.
Intuitively, we perceive that this setup goes to considerably enhance the price of ingestion for a RAG pipeline — basically double it, if no more. In spite of everything we now added a bunch of additional calls to the LLM, didn’t we? That is true to some extent — certainly now, for every chunk, we make a further name to the LLM as a way to situate it inside its supply doc and get the contextual textual content.
Nevertheless, it is a value that we’re solely paying as soon as, on the stage of doc ingestion. In contrast to different strategies that try to protect context at runtime — similar to Hypothetical Doc Embeddings (HyDE) — contextual retrieval performs the heavy work in the course of the doc ingestion stage. In runtime approaches, further LLM calls are required for each person question, which might shortly scale latency and operational prices. In distinction, contextual retrieval shifts the computation to the ingestion section, that means that the improved retrieval high quality comes with no further overhead throughout runtime. On prime of those, further strategies can be utilized for additional lowering the contextual retrieval value. Extra exactly, caching can be utilized for producing the abstract of the doc solely as soon as after which situating every chunk in opposition to the produced doc abstract.
. . .
On my thoughts
Contextual retrieval represents a easy but highly effective enchancment to conventional RAG techniques. By enriching every chunk with contextual textual content, pinpointing its semantic place inside its supply doc, we dramatically scale back the paradox of every chunk, and thus enhance the standard of the data handed to the LLM. Mixed with hybrid search, this system permits us to protect semantics, key phrases, and context concurrently.
Beloved this put up? Let’s be associates! Be a part of me on:
📰Substack 💌 Medium 💼LinkedIn ☕Purchase me a espresso!
All pictures by the creator, besides talked about in any other case.
















