{"id":1688,"date":"2025-02-06T07:02:22","date_gmt":"2025-02-06T07:02:22","guid":{"rendered":"https:\/\/mailitics.com\/index.php\/2025\/02\/06\/supercharge-your-rag-with-multi-agent-self-rag\/"},"modified":"2025-02-06T07:02:22","modified_gmt":"2025-02-06T07:02:22","slug":"supercharge-your-rag-with-multi-agent-self-rag","status":"publish","type":"post","link":"https:\/\/mailitics.com\/index.php\/2025\/02\/06\/supercharge-your-rag-with-multi-agent-self-rag\/","title":{"rendered":"Supercharge Your RAG with Multi-Agent Self-RAG"},"content":{"rendered":"<p>    Supercharge Your RAG with Multi-Agent Self-RAG<br \/>\n \t<BR><br \/>\n<BR><\/BR><br \/>\n    <!-- no image --><br \/>\n \t<BR><br \/>\n<BR><\/BR><\/p>\n<div>\n<h2 class=\"wp-block-heading\" id=\"c356\"><strong>Introduction<\/strong><\/h2>\n<p class=\"wp-block-paragraph\" id=\"cf60\">Many of us might have tried to build a RAG application and noticed it falls significantly short of addressing real-life needs. Why is that? It\u2019s because many real-world problems require multiple steps of information retrieval and reasoning. We need our agent to perform those as humans normally do, yet most RAG applications fall short of this.<\/p>\n<p class=\"wp-block-paragraph\" id=\"063e\">This article explores how to supercharge your RAG application by making its data retrieval and reasoning process similar to how a human would, under a multi-agent framework. The framework presented here is based on the\u00a0<a href=\"https:\/\/langchain-ai.github.io\/langgraph\/tutorials\/rag\/langgraph_self_rag\/\" rel=\"noreferrer noopener\" target=\"_blank\">Self-RAG strategy<\/a>\u00a0but has been significantly modified to enhance its capabilities. Prior knowledge of the original strategy is not necessary for reading this article.<\/p>\n<h2 class=\"wp-block-heading\" id=\"9cbe\"><strong>Real-life Case<\/strong><\/h2>\n<p class=\"wp-block-paragraph\" id=\"13b4\">Consider this: I was going to fly from Delhi to Munich (let\u2019s assume I am taking the flight from an EU airline), but I was denied boarding somehow. Now I want to know what the compensation should be.<\/p>\n<p class=\"wp-block-paragraph\" id=\"8796\">These two webpages contain relevant information, I go ahead adding them to my vector store, trying to have my agent answer this for me by retrieving the right information.<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\"><a href=\"https:\/\/www.eu-startups.com\/2024\/04\/comparative-analysis-eu-vs-us-flight-compensation-policies-sponsored\/\" target=\"_blank\" rel=\"noreferrer noopener\">Flight compensation policy<\/a><\/li>\n<li class=\"wp-block-list-item\">\n<a href=\"https:\/\/www.geodatos.net\/en\/distances\/cities\/germany\/bavaria\/munich\" target=\"_blank\" rel=\"noreferrer noopener\">Distance between Munich and other major cities in the world<\/a>\u00a0(note: it does not reflect the distance between airports, but it serves the purpose for our demonstration)<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\" id=\"87ad\">Now, I pass this question to the vector store:\u00a0<strong><em>\u201chow much can I receive if I am denied boarding, for flights from Delhi to Munich?\u201d<\/em><\/strong>.<\/p>\n<pre class=\"wp-block-code\"><code>\u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013&lt;br&gt;Overview of US Flight Compensation Policies To get compensation for delayed flights, you should contact your airline via their customer service or go to the customer service desk. At the same time, you should bear in mind that you will only receive compensation if the delay is not weather-related and is within the carrier`s control. According to the US Department of Transportation, US airlines are not required to compensate you if a flight is cancelled or delayed. You can be compensated if you are bumped or moved from an overbooked flight. If your provider cancels your flight less than two weeks before departure and you decide to cancel your trip entirely, you can receive a refund of both pre-paid baggage fees and your plane ticket. There will be no refund if you choose to continue your journey. In the case of a delayed flight, the airline will rebook you on a different flight. According to federal law, you will not be provided with money or other compensation. Comparative Analysis of EU vs. US Flight Compensation Policies&lt;br&gt;\u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013&lt;br&gt;&lt;strong&gt;(AUTHOR-ADDED NOTE: IMPORTANT, PAY ATTENTION TO THIS)&lt;\/strong&gt;&lt;br&gt;Short-distance flight delays \u2013 if it is up to 1,500 km, you are due 250 Euro compensation.&lt;br&gt;Medium distance flight delays \u2013 for all the flights between 1,500 and 3,500 km, the compensation should be 400 Euro.&lt;br&gt;Long-distance flight delays \u2013 if it is over 3,500 km, you are due 600 Euro compensation. To receive this kind of compensation, the following conditions must be met; Your flight starts in a non-EU member state or in an EU member state and finishes in an EU member state and is organised by an EU airline. Your flight reaches the final destination with a delay that exceeds three hours. There is no force majeure.&lt;br&gt;\u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013&lt;br&gt;Compensation policies in the EU and US are not the same, which implies that it is worth knowing more about them. While you can always count on Skycop flight cancellation compensation, you should still get acquainted with the information below.&lt;br&gt;\u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013 \u2013&lt;br&gt;Compensation for flight regulations EU: The EU does regulate flight delay compensation, which is known as EU261. US: According to the US Department of Transportation, every airline has its own policies about what should be done for delayed passengers. Compensation for flight delays EU: Just like in the United States, compensation is not provided when the flight is delayed due to uncontrollable reasons. However, there is a clear approach to compensation calculation based on distance. For example, if your flight was up to 1,500 km, you can receive 250 euros. US: There are no federal requirements. That is why every airline sets its own limits for compensation in terms of length. However, it is usually set at three hours. Overbooking EU: In the EU, they call for volunteers if the flight is overbooked. These people are entitled to a choice of: Re-routing to their final destination at the earliest opportunity. Refund of their ticket cost within a week if not travelling. Re-routing at a later date at the person`s convenience.<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"5cb4\">Unfortunately, they contain only generic flight compensation policies, without telling me how much I can expect when denied boarding from Delhi to Munich specifically. If the RAG agent takes these as the sole context, it can only provide a generic answer about flight compensation policy, without giving the answer we want.<\/p>\n<p class=\"wp-block-paragraph\" id=\"97c7\">However, while the documents are not immediately useful, there is an important insight contained in the 2nd piece of context:\u00a0<strong><em>compensation varies according to flight distance<\/em><\/strong>. If the RAG agent thinks more like human, it should follow these steps to provide an answer:<\/p>\n<ol class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Based on the retrieved context, reason that compensation varies with flight distance<\/li>\n<li class=\"wp-block-list-item\">Next, retrieve the flight distance between Delhi and Munich<\/li>\n<li class=\"wp-block-list-item\">Given the distance (which is around 5900km), classify the flight as a long-distance one<\/li>\n<li class=\"wp-block-list-item\">Combined with the previously retrieved context, figure out I am due 600 EUR, assuming other conditions are fulfilled<\/li>\n<\/ol>\n<p class=\"wp-block-paragraph\" id=\"ea58\">This example demonstrates how a simple RAG, in which a single retrieval is made, fall short for several reasons:<\/p>\n<ol class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">\n<strong>Complex Queries<\/strong>: Users often have questions that a simple search can\u2019t fully address. For example, \u201cWhat\u2019s the best smartphone for gaming under $500?\u201d requires consideration of multiple factors like performance, price, and features, which a single retrieval step might miss.<\/li>\n<li class=\"wp-block-list-item\">\n<strong>Deep Information<\/strong>: Some information lies across documents. For example, research papers, medical records, or legal documents often include references that need to be made sense of, before one can fully understand the content of a given article. Multiple retrieval steps help dig deeper into the content.<\/li>\n<\/ol>\n<p class=\"wp-block-paragraph\" id=\"e8d0\">Multiple retrievals supplemented with human-like reasoning allow for a more nuanced, comprehensive, and accurate response, adapting to the complexity and depth of user queries.<\/p>\n<h2 class=\"wp-block-heading\" id=\"a66e\">Multi-Agent Self-RAG<\/h2>\n<p class=\"wp-block-paragraph\" id=\"d9c4\">Here I explain the reasoning process behind this strategy, afterwards I will provide the code to show you how to achieve this!<\/p>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\" id=\"e167\">Note: For readers interested in knowing how my approach differs from the\u00a0<a href=\"https:\/\/langchain-ai.github.io\/langgraph\/tutorials\/rag\/langgraph_self_rag\/\" rel=\"noreferrer noopener\" target=\"_blank\">original Self-RAG<\/a>, I will describe the discrepancies in quotation boxes like this. But general readers who are unfamiliar with the original Self-RAG can skip them.<\/p>\n<\/blockquote>\n<p class=\"wp-block-paragraph\" id=\"9184\">In the below graphs, each circle represents a step (aka\u00a0<strong>Node<\/strong>), which is performed by a dedicated agent working on the specific problem. We orchestrate them to form a multi-agent RAG application.<\/p>\n<h3 class=\"wp-block-heading\" id=\"96b2\">1st iteration: Simple RAG<\/h3>\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" data-dominant-color=\"fdf5ea\" data-has-transparency=\"false\" style=\"--dominant-color: #fdf5ea;\" decoding=\"async\" width=\"895\" height=\"226\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_7cjs7z09W_HEySmLc0-whw-1.png?resize=895%2C226&#038;ssl=1\" alt=\"\" class=\"wp-image-597358 not-transparent\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_7cjs7z09W_HEySmLc0-whw-1.png 895w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_7cjs7z09W_HEySmLc0-whw-1-300x76.png 300w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_7cjs7z09W_HEySmLc0-whw-1-768x194.png 768w\" sizes=\"(max-width: 895px) 100vw, 895px\"><figcaption class=\"wp-element-caption\">A simple RAG chain<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\" id=\"1faf\">This is just the vanilla RAG approach I described in \u201cReal-life Case\u201d, represented as a graph. After\u00a0<code>Retrieve documents<\/code>, the\u00a0<code>new_documents<\/code>\u00a0will be used as input for\u00a0<code>Generate Answer<\/code>. Nothing special, but it serves as our starting point.<\/p>\n<h3 class=\"wp-block-heading\" id=\"830d\">2nd iteration: Digest documents with \u201cGrade documents\u201d<\/h3>\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" data-dominant-color=\"f6f3ef\" data-has-transparency=\"false\" style=\"--dominant-color: #f6f3ef;\" decoding=\"async\" width=\"1024\" height=\"261\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_S7emqxgrROrusM2R4K0N2w-1024x261.png?resize=1024%2C261&#038;ssl=1\" alt=\"\" class=\"wp-image-597359 not-transparent\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_S7emqxgrROrusM2R4K0N2w-1024x261.png 1024w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_S7emqxgrROrusM2R4K0N2w-300x76.png 300w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_S7emqxgrROrusM2R4K0N2w-768x195.png 768w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_S7emqxgrROrusM2R4K0N2w.png 1132w\" sizes=\"(max-width: 1024px) 100vw, 1024px\"><figcaption class=\"wp-element-caption\">Reasoning like human do<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\" id=\"ec3e\">Remember I said in the \u201cReal-life Case\u201d section, that as a next step, the agent should\u00a0<strong><em>\u201creason that compensation varies with flight distance\u201d<\/em><\/strong>? The\u00a0<code>Grade documents<\/code>\u00a0step is exactly for this purpose.<\/p>\n<p class=\"wp-block-paragraph\" id=\"a6bb\">Given the\u00a0<code>new_documents<\/code>, the agent will try to output two items:<\/p>\n<ol class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">\n<code>useful_documents<\/code>: Comparing the question asked, it determines if the documents are useful, and retain a memory for those deemed useful for future reference. As an example, since our question does not concern compensation policies for US, documents describing those are discarded, leaving only those for EU<\/li>\n<li class=\"wp-block-list-item\">\n<code>hypothesis<\/code>: Based on the documents, the agent forms a hypothesis about how the question can be answered, that is, flight distance needs to be identified<\/li>\n<\/ol>\n<p class=\"wp-block-paragraph\" id=\"3055\">Notice how the above reasoning resembles human thinking! But still, while these outputs are useful, we need to instruct the agent to use them as input for performing the next document retrieval. Without this, the answer provided in\u00a0<code>Generate answer<\/code>\u00a0is still not useful.<\/p>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\" id=\"5e39\"><code>useful_documents<\/code>\u00a0are appended for each document retrieval loop, instead of being overwritten, to keep a memory of documents that are previously deemed useful.\u00a0<code>hypothesis<\/code>\u00a0is formed from\u00a0<code>useful_documents<\/code>\u00a0and\u00a0<code>new_documents<\/code>\u00a0to provide an \u201cabstract reasoning\u201d to inform how query is to be transformed subsequently.<\/p>\n<p class=\"wp-block-paragraph\" id=\"8496\">The\u00a0<code>hypothesis<\/code>\u00a0is especially useful when no useful documents can be identified initially, as the agent can still form hypothesis from documents not immediately deemed as useful \/ only bearing indirect relationship to the question at hand, for informing what questions to ask next<\/p>\n<\/blockquote>\n<h3 class=\"wp-block-heading\" id=\"2ecf\">3rd iteration: Brainstorm new questions to ask<\/h3>\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" data-dominant-color=\"f8f6f3\" data-has-transparency=\"false\" style=\"--dominant-color: #f8f6f3;\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"355\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_xI3fGpV0M6BfLPIZDtD_2Q-1-1024x355.png?resize=1024%2C355&#038;ssl=1\" alt=\"\" class=\"wp-image-597361 not-transparent\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_xI3fGpV0M6BfLPIZDtD_2Q-1-1024x355.png 1024w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_xI3fGpV0M6BfLPIZDtD_2Q-1-300x104.png 300w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_xI3fGpV0M6BfLPIZDtD_2Q-1-768x266.png 768w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_xI3fGpV0M6BfLPIZDtD_2Q-1.png 1320w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\"><figcaption class=\"wp-element-caption\">Suggest questions for additional information retrieval<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\" id=\"c68b\">We have the agent reflect upon whether the answer is useful and grounded in context. If not, it should proceed to\u00a0<code>Transform query<\/code>\u00a0to ask further questions.<\/p>\n<p class=\"wp-block-paragraph\" id=\"75ea\">The output\u00a0<code>new_queries<\/code>\u00a0will be a list of new questions that the agent consider useful for obtaining extra information. Given the\u00a0<code>useful_documents<\/code>\u00a0(compensation policies for EU), and\u00a0<code>hypothesis<\/code>\u00a0(need to identify flight distance between Delhi and Munich), it asks questions like\u00a0<strong>\u201cWhat is the distance between Delhi and Munich?\u201d<\/strong><\/p>\n<p class=\"wp-block-paragraph\" id=\"487a\">Now we are ready to use the\u00a0<code>new_queries<\/code>\u00a0for further retrieval!<\/p>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\" id=\"016e\">The\u00a0<code>transform_query<\/code>\u00a0node will use\u00a0<code>useful_documents<\/code>\u00a0(which are accumulated per iteration, instead of being overwritten) and\u00a0<code>hypothesis<\/code>\u00a0as input for providing the agent directions to ask new questions.<\/p>\n<p class=\"wp-block-paragraph\" id=\"91bc\">The new questions will be a list of questions (instead of a single question) separated from the original\u00a0<code>question<\/code>, so that the original question is kept in state, otherwise the agent could lose track of the original question after multiple iterations.<\/p>\n<\/blockquote>\n<h3 class=\"wp-block-heading\" id=\"d80b\">Final iteration: Further retrieval with new questions<\/h3>\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" data-dominant-color=\"f6f5f3\" data-has-transparency=\"false\" style=\"--dominant-color: #f6f5f3;\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"439\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_g2kgeEqpTlWhHntGo7ip6Q-1-1024x439.png?resize=1024%2C439&#038;ssl=1\" alt=\"\" class=\"wp-image-597363 not-transparent\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_g2kgeEqpTlWhHntGo7ip6Q-1-1024x439.png 1024w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_g2kgeEqpTlWhHntGo7ip6Q-1-300x129.png 300w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_g2kgeEqpTlWhHntGo7ip6Q-1-768x329.png 768w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_g2kgeEqpTlWhHntGo7ip6Q-1.png 1126w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\"><figcaption class=\"wp-element-caption\">Issuing new queries to retrieve extra documents<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\" id=\"2e3f\">The output\u00a0<code>new_queries<\/code>\u00a0from\u00a0<code>Transform query<\/code>\u00a0will be passed to the\u00a0<code>Retrieve documents<\/code>\u00a0step, forming a retrieval loop.<\/p>\n<p class=\"wp-block-paragraph\" id=\"7d3a\">Since the question\u00a0<strong><em>\u201cWhat is the distance between Delhi and Munich?\u201d<\/em><\/strong><em>\u00a0<\/em>is asked, we can expect the flight distance is then retrieved as\u00a0<code>new_documents<\/code>, and subsequently graded as\u00a0<code>useful_documents<\/code>, further used as an input for\u00a0<code>Generate answer<\/code>.<\/p>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\" id=\"0784\">The\u00a0<code>&lt;em&gt;grade_documents&lt;\/em&gt;<\/code>\u00a0node will compare the documents against both the original\u00a0<code>&lt;em&gt;question&lt;\/em&gt;<\/code>\u00a0and\u00a0<code>&lt;em&gt;new_questions&lt;\/em&gt;<\/code>\u00a0list, so that documents that are considered useful for\u00a0<code>&lt;em&gt;new_questions&lt;\/em&gt;<\/code>, even if not so for the original\u00a0<code>&lt;em&gt;question&lt;\/em&gt;<\/code>, are kept.<\/p>\n<p class=\"wp-block-paragraph\" id=\"e4f0\">This is because those documents might help answer the original\u00a0<code>&lt;em&gt;question&lt;\/em&gt;<\/code>\u00a0indirectly, by being relevant to\u00a0<code>&lt;em&gt;new_questions&lt;\/em&gt;<\/code><em>\u00a0(like \u201c<\/em>What is the distance between Delhi and Munich?\u201d)<\/p>\n<\/blockquote>\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" data-dominant-color=\"f6f5f3\" data-has-transparency=\"false\" style=\"--dominant-color: #f6f5f3;\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"439\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_g2kgeEqpTlWhHntGo7ip6Q-3-1024x439.png?resize=1024%2C439&#038;ssl=1\" alt=\"\" class=\"wp-image-597365 not-transparent\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_g2kgeEqpTlWhHntGo7ip6Q-3-1024x439.png 1024w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_g2kgeEqpTlWhHntGo7ip6Q-3-300x129.png 300w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_g2kgeEqpTlWhHntGo7ip6Q-3-768x329.png 768w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_g2kgeEqpTlWhHntGo7ip6Q-3.png 1126w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\"><figcaption class=\"wp-element-caption\">Final answer!<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\" id=\"a411\">Equipped with this new context about flight distance, the agent is now ready to provide the right answer:\u00a0<strong>600 EUR<\/strong>!<\/p>\n<p class=\"wp-block-paragraph\" id=\"ea2b\">Next, let us now dive into the code to see how this multi-agent RAG application is created.<\/p>\n<h2 class=\"wp-block-heading\" id=\"df4e\">Implementation<\/h2>\n<p class=\"wp-block-paragraph\" id=\"b681\">The source code can be found\u00a0<a href=\"https:\/\/github.com\/yip-kl\/enhanced-self-rag\" rel=\"noreferrer noopener\" target=\"_blank\">here<\/a>. Our multi-agent RAG application involves iterations and loops, and LangGraph is a great library for building such complex multi-agent application. If you are not familiar with LangGraph, you are strongly suggested to have a look at\u00a0<a href=\"https:\/\/langchain-ai.github.io\/langgraph\/tutorials\/introduction\/\" rel=\"noreferrer noopener\" target=\"_blank\">LangGraph\u2019s Quickstart guide<\/a>\u00a0to understand more about it!<\/p>\n<p class=\"wp-block-paragraph\" id=\"f661\">To keep this article concise, I will focus on the key code snippets only.<\/p>\n<p class=\"wp-block-paragraph\" id=\"c0af\"><strong>Important note<\/strong>: I am using OpenRouter as the\u00a0<a href=\"https:\/\/towardsdatascience.com\/tag\/llm\/\">Llm<\/a>\u00a0interface, but the code can be easily adapted for other LLM interfaces. Also, while in my code I am using Claude 3.5 Sonnet as model, you can use any LLM as long as it support\u00a0<code>tools<\/code>\u00a0as parameter (check this list\u00a0<a href=\"https:\/\/openrouter.ai\/models?fmt=cards&amp;order=newest&amp;supported_parameters=tools\" rel=\"noreferrer noopener\" target=\"_blank\">here<\/a>), so you can also run this with other models, like DeepSeek V3 and OpenAI o1!<\/p>\n<h3 class=\"wp-block-heading\" id=\"ab53\">State definition<\/h3>\n<p class=\"wp-block-paragraph\" id=\"3879\">In the previous section, I have defined various elements e.g.\u00a0<code>new_documents<\/code>,\u00a0<code>hypothesis<\/code>\u00a0that are to be passed to each step (aka\u00a0<strong>Nodes<\/strong>), in LangGraph\u2019s terminology these elements are called\u00a0<strong>State<\/strong>.<\/p>\n<p class=\"wp-block-paragraph\" id=\"3a1d\">We define the State formally with the following snippet.<\/p>\n<pre class=\"wp-block-code\"><code>from typing import List, Annotated&lt;br&gt;from typing_extensions import TypedDict&lt;br&gt;&lt;br&gt;def append_to_list(original: list, new: list) -&gt; list:&lt;br&gt;    original.append(new)&lt;br&gt;    return original&lt;br&gt;&lt;br&gt;def combine_list(original: list, new: list) -&gt; list:&lt;br&gt;    return original + new&lt;br&gt;&lt;br&gt;class GraphState(TypedDict):&lt;br&gt;    \"\"\"&lt;br&gt;    Represents the state of our graph.&lt;br&gt;&lt;br&gt;    Attributes:&lt;br&gt;        question: question&lt;br&gt;        generation: LLM generation&lt;br&gt;        new_documents: newly retrieved documents for the current iteration&lt;br&gt;        useful_documents: documents that are considered useful&lt;br&gt;        graded_documents: documents that have been graded&lt;br&gt;        new_queries: newly generated questions&lt;br&gt;        hypothesis: hypothesis&lt;br&gt;    \"\"\"&lt;br&gt;&lt;br&gt;    question: str&lt;br&gt;    generation: str&lt;br&gt;    new_documents: List[str]&lt;br&gt;    useful_documents: Annotated[List[str], combine_list]&lt;br&gt;    graded_documents: List[str]&lt;br&gt;    new_queries: Annotated[List[str], append_to_list]&lt;br&gt;    hypothesis: str<\/code><\/pre>\n<h3 class=\"wp-block-heading\" id=\"188d\">Graph definition<\/h3>\n<p class=\"wp-block-paragraph\" id=\"5fc4\">This is where we combine the different steps to form a \u201c<strong>Graph<\/strong>\u201d, which is a representation of our multi-agent application. The definitions of various steps (e.g.\u00a0<code>grade_documents<\/code>) are represented by their respective functions.<\/p>\n<pre class=\"wp-block-code\"><code>from langgraph.graph import END, StateGraph, START&lt;br&gt;from langgraph.checkpoint.memory import MemorySaver&lt;br&gt;from IPython.display import Image, display&lt;br&gt;&lt;br&gt;workflow = StateGraph(GraphState)&lt;br&gt;&lt;br&gt;# Define the nodes&lt;br&gt;workflow.add_node(\"retrieve\", retrieve)  # retrieve&lt;br&gt;workflow.add_node(\"grade_documents\", grade_documents)  # grade documents&lt;br&gt;workflow.add_node(\"generate\", generate)  # generatae&lt;br&gt;workflow.add_node(\"transform_query\", transform_query)  # transform_query&lt;br&gt;&lt;br&gt;# Build graph&lt;br&gt;workflow.add_edge(START, \"retrieve\")&lt;br&gt;workflow.add_edge(\"retrieve\", \"grade_documents\")&lt;br&gt;workflow.add_conditional_edges(&lt;br&gt;    \"grade_documents\",&lt;br&gt;    decide_to_generate,&lt;br&gt;    {&lt;br&gt;        \"transform_query\": \"transform_query\",&lt;br&gt;        \"generate\": \"generate\",&lt;br&gt;    },&lt;br&gt;)&lt;br&gt;workflow.add_edge(\"transform_query\", \"retrieve\")&lt;br&gt;workflow.add_conditional_edges(&lt;br&gt;    \"generate\",&lt;br&gt;    grade_generation_v_documents_and_question,&lt;br&gt;    {&lt;br&gt;        \"useful\": END,&lt;br&gt;        \"not supported\": \"transform_query\",&lt;br&gt;        \"not useful\": \"transform_query\",&lt;br&gt;    },&lt;br&gt;)&lt;br&gt;&lt;br&gt;# Compile&lt;br&gt;memory = MemorySaver()&lt;br&gt;app = workflow.compile(checkpointer=memory)&lt;br&gt;display(Image(app.get_graph(xray=True).draw_mermaid_png()))<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"4135\">Running the above code, you should see this graphical representation of our RAG application. Notice how it is essentially equivalent to the graph I have shown in the final iteration of \u201cEnhanced Self-RAG Strategy\u201d!<\/p>\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" data-dominant-color=\"f8f7fa\" data-has-transparency=\"false\" style=\"--dominant-color: #f8f7fa;\" loading=\"lazy\" decoding=\"async\" width=\"515\" height=\"557\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_bRGuqH1Zm577pt6nDJIPWQ.png?resize=515%2C557&#038;ssl=1\" alt=\"\" class=\"wp-image-597366 not-transparent\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_bRGuqH1Zm577pt6nDJIPWQ.png 515w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_bRGuqH1Zm577pt6nDJIPWQ-277x300.png 277w\" sizes=\"auto, (max-width: 515px) 100vw, 515px\"><figcaption class=\"wp-element-caption\">Visualizing the multi-agent RAG graph<\/figcaption><\/figure>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\" id=\"baed\">After\u00a0<code>generate<\/code>, if the answer is considered \u201cnot supported\u201d, the agent will proceed to\u00a0<code>transform_query<\/code>\u00a0intead of to\u00a0<code>generate<\/code>\u00a0again, so that the agent will look for additional information rather than trying to regenerate answers based on existing context, which might not suffice for providing a \u201csupported\u201d answer<\/p>\n<\/blockquote>\n<p class=\"wp-block-paragraph\" id=\"7519\">Now we are ready to put the multi-agent application to test! With the below code snippet, we ask this question\u00a0<code>how much can I receive if I am denied boarding, for flights from Delhi to Munich?<\/code><\/p>\n<pre class=\"wp-block-code\"><code>from pprint import pprint&lt;br&gt;config = {\"configurable\": {\"thread_id\": str(uuid4())}}&lt;br&gt;&lt;br&gt;# Run&lt;br&gt;inputs = {&lt;br&gt;    \"question\": \"how much can I receive if I am denied boarding, for flights from Delhi to Munich?\",&lt;br&gt;    }&lt;br&gt;for output in app.stream(inputs, config):&lt;br&gt;    for key, value in output.items():&lt;br&gt;        # Node&lt;br&gt;        pprint(f\"Node '{key}':\")&lt;br&gt;        # Optional: print full state at each node&lt;br&gt;        # print(app.get_state(config).values)&lt;br&gt;    pprint(\"n---n\")&lt;br&gt;&lt;br&gt;# Final generation&lt;br&gt;pprint(value[\"generation\"])<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"f058\">While output might vary (sometimes the application provides the answer without any iterations, because it \u201cguessed\u201d the distance between Delhi and Munich), it should look something like this, which shows the application went through multiple rounds of data retrieval for RAG.<\/p>\n<pre class=\"wp-block-code\"><code>---RETRIEVE---&lt;br&gt;\"Node 'retrieve':\"&lt;br&gt;'n---n'&lt;br&gt;---CHECK DOCUMENT RELEVANCE TO QUESTION---&lt;br&gt;---GRADE: DOCUMENT NOT RELEVANT---&lt;br&gt;---GRADE: DOCUMENT RELEVANT---&lt;br&gt;---GRADE: DOCUMENT NOT RELEVANT---&lt;br&gt;---GRADE: DOCUMENT NOT RELEVANT---&lt;br&gt;---ASSESS GRADED DOCUMENTS---&lt;br&gt;---DECISION: GENERATE---&lt;br&gt;\"Node 'grade_documents':\"&lt;br&gt;'n---n'&lt;br&gt;---GENERATE---&lt;br&gt;---CHECK HALLUCINATIONS---&lt;br&gt;'---DECISION: GENERATION IS NOT GROUNDED IN DOCUMENTS, RE-TRY---'&lt;br&gt;\"Node 'generate':\"&lt;br&gt;'n---n'&lt;br&gt;---TRANSFORM QUERY---&lt;br&gt;\"Node 'transform_query':\"&lt;br&gt;'n---n'&lt;br&gt;---RETRIEVE---&lt;br&gt;\"Node 'retrieve':\"&lt;br&gt;'n---n'&lt;br&gt;---CHECK DOCUMENT RELEVANCE TO QUESTION---&lt;br&gt;---GRADE: DOCUMENT NOT RELEVANT---&lt;br&gt;---GRADE: DOCUMENT NOT RELEVANT---&lt;br&gt;---GRADE: DOCUMENT RELEVANT---&lt;br&gt;---GRADE: DOCUMENT NOT RELEVANT---&lt;br&gt;---GRADE: DOCUMENT NOT RELEVANT---&lt;br&gt;---GRADE: DOCUMENT NOT RELEVANT---&lt;br&gt;---GRADE: DOCUMENT NOT RELEVANT---&lt;br&gt;---ASSESS GRADED DOCUMENTS---&lt;br&gt;---DECISION: GENERATE---&lt;br&gt;\"Node 'grade_documents':\"&lt;br&gt;'n---n'&lt;br&gt;---GENERATE---&lt;br&gt;---CHECK HALLUCINATIONS---&lt;br&gt;---DECISION: GENERATION IS GROUNDED IN DOCUMENTS---&lt;br&gt;---GRADE GENERATION vs QUESTION---&lt;br&gt;---DECISION: GENERATION ADDRESSES QUESTION---&lt;br&gt;\"Node 'generate':\"&lt;br&gt;'n---n'&lt;br&gt;('Based on the context provided, the flight distance from Munich to Delhi is '&lt;br&gt; '5,931 km, which falls into the long-distance category (over 3,500 km). '&lt;br&gt; 'Therefore, if you are denied boarding on a flight from Delhi to Munich '&lt;br&gt; 'operated by an EU airline, you would be eligible for 600 Euro compensation, '&lt;br&gt; 'provided that:n'&lt;br&gt; '1. The flight is operated by an EU airlinen'&lt;br&gt; '2. There is no force majeuren'&lt;br&gt; '3. Other applicable conditions are metn'&lt;br&gt; 'n'&lt;br&gt; \"However, it's important to note that this compensation amount is only valid \"&lt;br&gt; 'if all the required conditions are met as specified in the regulations.')<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"be75\">And the final answer is what we aimed for!<\/p>\n<pre class=\"wp-block-code\"><code>Based on the context provided, the flight distance from Munich to Delhi is&lt;br&gt;5,931 km, which falls into the long-distance category (over 3,500 km).&lt;br&gt;Therefore, if you are denied boarding on a flight from Delhi to Munich&lt;br&gt;operated by an EU airline, you would be eligible for 600 Euro compensation,&lt;br&gt;provided that:&lt;br&gt;1. The flight is operated by an EU airline&lt;br&gt;2. There is no force majeure&lt;br&gt;3. Other applicable conditions are met&lt;br&gt;&lt;br&gt;However, it's important to note that this compensation amount is only valid&lt;br&gt;if all the required conditions are met as specified in the regulations.<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"6d83\">Inspecting the State, we see how the\u00a0<code>hypothesis<\/code>\u00a0and\u00a0<code>new_queries<\/code>\u00a0enhance the effectiveness of our multi-agent RAG application by mimicking human thinking process.<\/p>\n<p class=\"wp-block-paragraph\" id=\"bc4b\"><strong>Hypothesis<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>print(app.get_state(config).values.get('hypothesis',\"\"))<\/code><\/pre>\n<pre class=\"wp-block-code\"><code>--- Output ---&lt;br&gt;To answer this question accurately, I need to determine:&lt;br&gt;&lt;br&gt;1. Is this flight operated by an EU airline? (Since Delhi is non-EU and Munich is EU)&lt;br&gt;2. What is the flight distance between Delhi and Munich? (To determine compensation amount)&lt;br&gt;3. Are we dealing with a denied boarding situation due to overbooking? (As opposed to delay\/cancellation)&lt;br&gt;&lt;br&gt;From the context, I can find information about compensation amounts based on distance, but I need to verify:&lt;br&gt;- If the flight meets EU compensation eligibility criteria&lt;br&gt;- The exact distance between Delhi and Munich to determine which compensation tier applies (250\u20ac, 400\u20ac, or 600\u20ac)&lt;br&gt;- If denied boarding compensation follows the same amounts as delay compensation&lt;br&gt;&lt;br&gt;The context doesn't explicitly state compensation amounts specifically for denied boarding, though it mentions overbooking situations in the EU require offering volunteers re-routing or refund options.&lt;br&gt;&lt;br&gt;Would you like me to proceed with the information available, or would you need additional context about denied boarding compensation specifically?<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"a30b\"><strong>New Queries<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>for questions_batch in app.get_state(config).values.get('new_queries',\"\"):&lt;br&gt;    for q in questions_batch:&lt;br&gt;        print(q)<\/code><\/pre>\n<pre class=\"wp-block-code\"><code>--- Output ---&lt;br&gt;What is the flight distance between Delhi and Munich?&lt;br&gt;Does EU denied boarding compensation follow the same amounts as flight delay compensation?&lt;br&gt;Are there specific compensation rules for denied boarding versus flight delays for flights from non-EU to EU destinations?&lt;br&gt;What are the compensation rules when flying with non-EU airlines from Delhi to Munich?&lt;br&gt;What are the specific conditions that qualify as denied boarding under EU regulations?<\/code><\/pre>\n<h2 class=\"wp-block-heading\" id=\"71a4\">Conclusion<\/h2>\n<p class=\"wp-block-paragraph\" id=\"fac8\">Simple RAG, while easy to build, might fall short in tackling real-life questions. By incorporating human thinking process into a multi-agent RAG framework, we are making RAG applications much more practical.<\/p>\n<p class=\"wp-block-paragraph\" id=\"04e2\"><em>*Unless otherwise noted, all images are by the author<\/em><\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\">\n<p>The post <a href=\"https:\/\/towardsdatascience.com\/supercharge-your-rag-with-multi-agent-self-rag\/\">Supercharge Your RAG with Multi-Agent Self-RAG<\/a> appeared first on <a href=\"https:\/\/towardsdatascience.com\/\">Towards Data Science<\/a>.<\/p>\n<\/div>\n<p> \t<BR><br \/>\n <BR><\/BR><br \/>\n    Julian Yip<br \/>\n \t<BR><br \/>\n<BR><\/BR><br \/>\n<a href=\"https:\/\/towardsdatascience.com\/supercharge-your-rag-with-multi-agent-self-rag\/\">Go to original source<\/a><br \/>\n \t<BR><br \/>\n <BR><\/BR><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Supercharge Your RAG with Multi-Agent Self-RAG Introduction Many of us might have tried to build a RAG application and noticed it falls significantly short of addressing real-life needs. Why is that? It\u2019s because many real-world problems require multiple steps of information retrieval and reasoning. We need our agent to perform those as humans normally do, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[678,62,71,87,447,1647,1648],"tags":[448,1649,362],"class_list":["post-1688","post","type-post","status-publish","format-standard","hentry","category-agentic-ai","category-aimldsaimlds","category-large-language-models","category-llm","category-llm-agent","category-multi-agent-systems","category-retrieval-augmented","tag-agent","tag-compensation","tag-rag"],"_links":{"self":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts\/1688"}],"collection":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/comments?post=1688"}],"version-history":[{"count":0,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts\/1688\/revisions"}],"wp:attachment":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/media?parent=1688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/categories?post=1688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/tags?post=1688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}