Machine Learning Mastery explains how to build long-context RAG without wasting tokens
Machine Learning Mastery has published a useful guide to long-context RAG. Even models with huge context windows still face two problems: the Lost in the Middle effect and the high cost of processing. The article proposes five practical techniques, from reranking and context caching to hybrid search and query expansion, so that answers are more accurate and the pipeline is cheaper and faster.
AI-processed from Machine Learning Mastery; edited by Hamidun News
On April 15, 2026, Machine Learning Mastery published a practical guide to building long-context RAG without wasting tokens and without hurting answer quality. The article's main takeaway is simple: even models with windows of hundreds of thousands or millions of tokens do not make RAG obsolete, but force developers to rebuild its architecture.
Why a long context does not save you
Not long ago, the standard advice for RAG was to split documents into small chunks, turn them into embeddings and retrieve the most relevant fragments. That was dictated by the limits of the LLMs themselves: context was expensive and ran out quickly. Now developers have models with million-token windows, and the temptation is obvious: just load the entire policy manual, all the instructions or the whole knowledge base into the prompt.
In practice, though, this approach quickly runs into two problems. The first is the Lost in the Middle effect, where the model is worse at noticing important information buried in the middle of a long context. The second is cost: running a huge body of text through the model on every request is slow and expensive.
As a result, long-context RAG stops being a question of "how to cram in more text" and becomes a question of "how to give the model exactly the context it will actually pay attention to."
Five techniques that work
Machine Learning Mastery offers not a single silver bullet but a set of practical methods that can be combined in one pipeline. The idea is not just to search for documents, but to control the order, volume and manner in which context is fed to the model. This is a noticeable shift from the old RAG approach, where retrieved chunks were sent to the prompt almost automatically, with no extra logic or quality control at each step.
- Reranking after retrieval: first take an expanded set of candidates, then re-sort them with a separate model.
- Context caching: load a large document into the cache once, then pass the model only the question and a reference to that context.
- Dynamic chunking + metadata filters: cut documents into meaningful blocks and filter them by section, date, page and other metadata.
- Hybrid retrieval: combine semantic search with exact keyword search so that literal matches in technical queries are not lost.
- Query expansion: extend the original question with additional phrasings if the user asked it too broadly or too casually.
The most interesting part is not the list itself but how the techniques connect. For example, the article recommends first pulling not the top 5 but, say, the top 20 candidates from the vector database, running them through a reranker, and only then placing the best documents in the prompt strategically: the most relevant one at the start, the second most important at the end, and the rest in between. That way the developer does not fight the limits of the model's attention but adapts to them.
How the pipeline changes
The article shows well that modern RAG for long context is becoming closer to search engineering than to a simple "retrieve-and-dump." If you have a static knowledge base, such as a large manual or internal policy, context caching removes the repeated processing of hundreds of thousands of tokens and noticeably lowers latency. This is especially useful for support chatbots and internal assistants that answer questions over the same document corpus again and again.
Another important layer is metadata and hybrid search. Semantic retrieval understands meaning well, but it can miss the exact name of a parameter, an error code or a section number. Keyword search, by contrast, latches onto literal matches. That is why the author advises combining both channels and merging their results through rank fusion. On top of this comes filtering by document structure: irrelevant sections, dates or pages are cut first, and only then is similarity search run. This reduces noise and makes the final context noticeably cleaner.
Finally, query expansion helps when the user phrases a question differently from how the information is written in the documents. If someone asks "what to do if the fire alarm goes off," the system can additionally generate variants such as "evacuation procedure," "assembly point" or "emergency exit." As a result, retrieval finds not only direct matches but also instructions that are close in meaning. This is especially valuable for corporate knowledge, compliance documents and technical documentation.
What this means
The Machine Learning Mastery article captures an important shift: long-context models have not killed RAG, they have made it more mature. The teams that win now are not those that load the maximum amount of text into the prompt, but those that know how to rank, filter, cache and reuse context well. For product teams, this means lower token costs, lower latency and more predictable answers over long document corpora. This is becoming the new standard for enterprise AI assistants.
- Oct 31, 2026Author puts his script before an AI jury: the verdict cost 850 thousand tokens
- Oct 29, 2026OvisOCR3: an 0.8B-parameter model that reads documents better than specialized systems
- Oct 29, 2026Tencent releases Youtu-Parsing-Omni: one model parses documents, audio and video
- Oct 28, 2026Liquid AI releases LFM2.5-24B-A2B: an agent model with 2B active parameters
Key Questions Answered
Why a long context does not save you?
Not long ago, the standard advice for RAG was to split documents into small chunks, turn them into embeddings and retrieve the most relevant fragments. That was dictated by the limits of the LLMs themselves: context was expensive and ran out quickly. Now developers have models with million-token windows, and the temptation is obvious: just load the entire policy manual, all the instructions or the whole knowledge base into the prompt. In practice, though, this approach quickly runs into two problems. The first is the Lost in the Middle effect, where the model is worse at no
What are the key details on Five techniques that work?
Machine Learning Mastery offers not a single silver bullet but a set of practical methods that can be combined in one pipeline. The idea is not just to search for documents, but to control the order, volume and manner in which context is fed to the model. This is a noticeable shift from the old RAG approach, where retrieved chunks were sent to the prompt almost automatically, with no extra logic or quality control at each step. Reranking after retrieval: first take an expanded set of candidates, then re-sort them with a separate model. Context caching: load a large document
How the pipeline changes?
The article shows well that modern RAG for long context is becoming closer to search engineering than to a simple "retrieve-and-dump." If you have a static knowledge base, such as a large manual or internal policy, context caching removes the repeated processing of hundreds of thousands of tokens and noticeably lowers latency. This is especially useful for support chatbots and internal assistants that answer questions over the same document corpus again and again. Another important layer is metadata and hybrid search. Semantic retrieval understands meaning well, but it can
Test Frontier LLMs & Autonomous Agents in One Workspace
Run Claude Opus 5.5, GPT-6, Gemini 3.1, and DeepSeek side-by-side in Nomad AI (nomadnet.ai) — unified model routing, deep research, and multimodal workflows under one subscription.
The AI world, distilled — once a week
Seven stories that actually mattered, hand-picked. No noise, no reposts, no press releases.
Done! Check your inbox for a confirmation.