Machine Learning Mastery explained how to build long-context RAG without extra tokens
Machine Learning Mastery published a practical breakdown of long-context RAG. Even models with huge context windows still face two problems: the Lost in the…
AI-processed from Machine Learning Mastery; edited by Hamidun News
On April 15, 2026, Machine Learning Mastery published a practical breakdown on how to build long-context RAG without wasting extra tokens and without losing answer quality. The main conclusion of the article is simple: even models with windows of hundreds of thousands and millions of tokens do not eliminate RAG, but force you to rebuild its architecture.
Why Long Context Doesn't Save
Not long ago, the standard advice for RAG sounded like this: split documents into small chunks, convert them into embeddings, and retrieve the most relevant fragments. This was driven by the limitations of the LLMs themselves: context was expensive and quickly ran out. Now developers have models with windows of a million tokens, and the temptation is obvious — just load the entire regulation, all instructions, or the entire knowledge base as a whole into the prompt.
But in practice, this approach quickly runs into two problems. The first is the Lost in the Middle effect, when the model worse notices important information if it's hidden in the middle of a long context. The second is cost: running a huge array of text through the model on each request is slow and expensive.
As a result, long-context RAG transforms from a task of "how to squeeze in more text" into a task of "how to give the model exactly the context it will actually pay attention to."
Five Working Techniques
Machine Learning Mastery offers not one silver bullet, but a set of practical tricks that can be combined in one pipeline. The idea is not just to search for documents, but to manage the order, volume, and method of delivering context to the model. This is a notable shift from the old RAG approach, where after retrieval the found chunks were almost automatically sent to the prompt without additional logic at each step and quality control.
- 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 and then only pass the question and reference to this context to the model.
- Dynamic chunking + metadata filters — cut documents into meaningful blocks and filter them by sections, dates, pages, and other metadata.
- Hybrid retrieval — combine semantic search and exact keyword search to avoid losing literal matches in technical queries.
- Query expansion — expand the original question with additional formulations if the user asked it too generally or conversationally.
The most interesting part is not the list itself, but how the techniques are interconnected. For example, the article recommends first retrieving not top-5, but, say, top-20 candidates from the vector database, then running them through a reranker and only after that placing the best documents in the prompt strategically: the most relevant one at the beginning, the second most important one at the end, the rest in between. This way the developer doesn't fight the limitations of the model's attention, but adapts to them.
How the Pipeline Changes
The article clearly shows that modern RAG for long context is becoming closer to search engineering than to simple "retrieve-and-dump". If you have a static knowledge base, like a voluminous manual or internal regulations, context caching eliminates the need to reprocess hundreds of thousands of tokens and significantly reduces latency. This is especially useful for support chatbots and internal assistants that repeatedly answer from the same corpus of documents.
Another important layer is metadata and hybrid search. Semantic retrieval understands meaning well, but may miss the exact name of a parameter, error code, or section number. Keyword search, conversely, latches onto literal matches. Therefore, the author advises combining both channels and bringing their results together through rank fusion. On top of this, filtering by document structure is added: unnecessary sections, dates, or pages are first cut off, and only then is similarity search run. This reduces noise and makes the final context noticeably cleaner.
Finally, query expansion helps in cases where the user formulates a question differently from how the information is recorded in the documents. If a person asks "what to do if a fire alarm goes off," the system can additionally generate options like "evacuation procedure," "assembly point," or "emergency exit." Through this, retrieval finds not only direct matches, but also semantically close instructions. For corporate knowledge, compliance documents, and technical documentation, this is especially valuable.
What This Means
The Machine Learning Mastery article fixes an important shift: long-context models didn't kill RAG, they made it mature. Now it's not the teams that load the maximum amount of text into the prompt that win, but those who know how to properly rank, filter, cache, and reuse context. For product teams, this means lower token costs, lower latency, and more predictable answers on long document corpora. This is exactly what becomes the new standard for corporate AI assistants.
Need AI working inside your business — not just in your newsfeed?
I build production AI for companies — custom CRM, internal tools, autonomous agents, workflow automation. Owned by you, shaped to your process, no per-seat tax. Built by Zhemal Khamidun, CPO of AlpinaGPT (AI platform, 6,000+ users).
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.