Building an RAG Solution with DeepSeek-r1, Tavily, and LangGraph

Author:DeanSacoransky and Deniz Askin
The Impact of DeepSeek-r1

DeepSeek publicly released the r1 model less than a month ago. Suddenly, everyone can publicly use a powerful reasoning model for application and model development.

In this article, we propose a simple intelligent agent workflow that empowers DeepSeek-r1 to perform information retrieval in an intelligent agent manner!

The system can accurately answer complex queries, such as:

“Is the headquarters of the Royal Bank of Canada located north of the headquarters of Sam Altman’s brother’s company?”

The complete intelligent agent output for this query, including the reasoning trail of the R1 model, can be found at the end of this blog post. The code has been published on our Github.

Our Motivation

Most people are focused on DeepSeek-r1’s ability to solve complex problems in mathematics, science, and coding.

However, the vast majority of us are not scientists or PhD students.

We have good news! There are currently real, high-value business use cases that can leverage reasoning models.

We believe that the easiest-to-implement use case for reasoning models right now is Retrieval-Augmented Generation (RAG). Reasoning models like DeepSeek-r1 allow for an immediate leap in performance for RAG systems.

We have built a simple yet elegant system that demonstrates the powerful capabilities of combining information retrieval (vector databases, web search, etc.) with reasoning in an intelligent agent manner, similar to the R1 version of Perplexity Pro.

DeepSeek-r1 Intelligent Agent Workflow
Building an RAG Solution with DeepSeek-r1, Tavily, and LangGraph
Figure 1. Our Agentic Workflow
Recursive Retrieval and Reasoning

Our workflow uses Tavily to retrieve information by browsing the web. This can be replaced with any information retrieval technology, such as vector databases.

We have implemented a “reasoning layer” based on DeepSeek-r1, as described below:

  1. Based on the user’s query and the retrieved content, r1 assesses whether the existing information is sufficient to answer the question.

  2. If sufficient, it generates an answer.

  3. If not sufficient, it identifies missing details and re-queries Tavily for more data.

This process recursively repeats until all necessary information is gathered, while discarding irrelevant data and retaining relevant information.

Why Use Reasoning Models in RAG?

The core of RAG requires complex reasoning because it involves synthesizing information from multiple sources, filtering out irrelevant information, integrating relevant information, tracing back, and generating answers.

We believe that most RAG systems in the future will use reasoning models and intelligent agent loops, such as the loop proposed in this article.

The practical contextual learning ability of reasoning models has qualitatively improved compared to previous models. This eliminates the need for “hacky” RAG techniques, such as long-context reordering, in favor of a simpler approach where you can:

  1. Retrieve a large context
  2. Put it into the context window
  3. Let the model discern what to focus on and what to ignore
Example
**Query:** Is the headquarters of the Royal Bank of Canada located north of the headquarters of Sam Altman's brother's company?
=== Step 1: Retrieval === **Searching:** Is the headquarters of the Royal Bank of Canada located north of the headquarters of Sam Altman's brother's company?
=== Step 2: Validation === **Total Retrieved Context:** The Royal Bank of Canada (RBC; French: Banque Royale du Canada) is a Canadian multinational financial services company and the largest bank in Canada by market capitalization. The bank serves over 20 million clients and has more than 100,000 employees worldwide. [2] The bank was founded in 1864 in Halifax, Nova Scotia, with its corporate headquarters in Toronto and its head office in Montreal. [2] The Royal Bank of Canada is a Canadian multinational bank and financial services company headquartered in Toronto, Ontario, with its head office in Montreal, Quebec. It is the largest bank in Canada by market capitalization and one of the largest companies in Canada by revenue and market capitalization. Where is the headquarters of the Royal Bank of Canada? RBC is one of Canada’s "Big Five" banks, which include the Bank of Montreal (BMO), Canadian Imperial Bank of Commerce (CIBC), Nova Scotia Bank (Scotiabank), and Toronto-Dominion Bank (TD). Where is the headquarters of the World Bank? Where is the headquarters of Bank of America? More world facts The skyscraper also contains the main branch of the bank in Toronto, a shopping arcade, and is part of the underground passage network connecting the building with the Fairmont Royal York Hotel, Brookfield Place, TD Centre, and Union Station. The bank's head office moved to the Four Pillars building in Montreal, Quebec in 1907 and remained there until it moved to the Royal Bank Building (also known as the Old Royal Bank Building) in the city in 1928. RBC Headquarters The first headquarters of the Royal Bank of Canada was located in the Merchants' Bank of Halifax building in Halifax, Nova Scotia, from 1864 to 1907.
**R1 Reasoning Trail:** Okay, let's solve this step by step. The user asks whether the headquarters of the Royal Bank of Canada (RBC) is located north of the headquarters of Sam Altman's brother's company. First, I need to confirm the location of RBC's headquarters. The provided context mentions that RBC has its corporate headquarters in Toronto and its head office in Montreal. The context also states that the corporate headquarters is in Toronto, and the head office is in Montreal. Therefore, the main corporate headquarters is in Toronto. Next, the question mentions Sam Altman's brother's company. Wait, Sam Altman is the CEO of OpenAI, but the user is talking about his brother's company. I need to figure out which company it is. Sam Altman's brother is Jack Altman, who is the co-founder and CEO of the HR software company Lattice. Lattice is headquartered in San Francisco, California. However, the provided context does not mention Sam Altman's brother or his company. The given information block only talks about RBC and other Canadian banks, the World Bank, and Bank of America, but does not provide any information about Sam Altman's relatives or their companies. Therefore, the answer depends on comparing the latitude of Toronto (RBC headquarters) and San Francisco (Lattice's location). Toronto's latitude is approximately 43.651070° N, while San Francisco's latitude is about 37.774929° N. Since 43.651° N is north of 37.7749° N, RBC's headquarters is indeed north of Lattice's headquarters. However, the context does not include any details about Sam Altman's brother's company. The useful information here is that RBC's headquarters is in Toronto. The missing information is the location of Sam Altman's brother's company. Without knowing where that company is, we cannot definitively answer this question. Therefore, the status should be "incomplete" since the information block does not provide the details needed to answer the question about his brother's company. The useful information is that the headquarters of RBC is in Toronto. **Missing Information:** The location of Sam Altman's brother's company.
=== Step 2b: Find Missing Information === **Searching:** The location of Sam Altman's brother's company
**New Retrieved Context:** The Altman Brothers company headquarters, office locations, and addresses | Craft.co. Advanced. Products. Solutions. Partners. Resources. Company. Switch menu. ... Title placeholder lorem ipsum dolor sit amet, consectetur adipiscing elit. Button CTA. The Altman Brothers are headquartered at 103 South Robertson Blvd, Los Angeles, USA. The Altman Companies are headquartered at 1905 Northwest Corporate Blvd, Boca Raton, FL, Suite 310, with an office location. Who are Sam Altman's brothers Jack and Max? Who are Sam Altman's brothers Jack and Max? His estranged sister Ann is an artist, writer, and podcast creator—but here’s everything you need to know about his two brothers Max and Jack. Sam Altman is the CEO of OpenAI and a co-founder of ChatGPT [image source: AFP] Who is Max Altman? HUMANNIE ### What we know about Sam Altman's sister Ann Jack's information, he is the youngest of the three brothers and has worked at Hydrazine Capital and Altman Capital. Altman's mother Connie had previously told The New Yorker: "I think Sam likes having his brothers around because they knew him before he was who he is, and they can challenge him in ways that others cannot."
=== Step 2: Validation === **Total Retrieved Context:** The Royal Bank of Canada’s corporate headquarters is located in Toronto, Ontario. The Altman Brothers company headquarters, office locations, and addresses | Craft.co. Advanced. Products. Solutions. Partners. Resources. Company. Switch menu. ... Title placeholder lorem ipsum dolor sit amet, consectetur adipiscing elit. Button CTA. The Altman Brothers are headquartered at 103 South Robertson Blvd, Los Angeles, USA. Sam Altman's brothers Jack and Max? Who are Sam Altman's brothers Jack and Max? His estranged sister Ann is an artist, writer, and podcast creator—but here’s everything you need to know about his two brothers Max and Jack. Sam Altman is the CEO of OpenAI and a co-founder of ChatGPT [image source: AFP] Who is Max Altman? HUMANNIE ### What we know about Sam Altman's sister Ann Jack's information, he is the youngest of the three brothers and has worked at Hydrazine Capital and Altman Capital. Altman's mother Connie had previously told The New Yorker: "I think Sam likes having his brothers around because they knew him before he was who he is, and they can challenge him in ways that others cannot."
**R1 Reasoning Trail:** Okay, let's solve this problem. The user asks whether the headquarters of the Royal Bank of Canada (RBC) is located north of the headquarters of Sam Altman's brother's company. First, I need to find the locations of both headquarters from the provided context. The context states: "The headquarters of the Royal Bank of Canada is located in Toronto, Ontario." So RBC is in Toronto. Next, what about Sam Altman's brother's company? The context mentions that "The Altman Brothers are headquartered at 103 South Robertson Blvd, Los Angeles." Wait, "The Altman Brothers"—this could be a company owned by Sam's brother. So their headquarters is in Los Angeles. Now, to determine if Toronto is north of Los Angeles. I know that Toronto is in Canada, which is north of the United States. Los Angeles is in southern California. So geographically, Toronto is indeed north of Los Angeles. Checking for any missing information. The context provides the locations of both headquarters. There is no missing data. The answer should be yes, RBC's headquarters is north of The Altman Brothers' headquarters.
Conclusion

We hope that our work here, combining reasoning models with intelligent agent loops into the RAG workflow, can serve as one of the many upcoming architectures to replace the original RAG methods for executing lengthy, nested, and complex queries with a far superior technology.

Feel free to try out our complete implementation on our Github to observe the significant performance improvement when using reasoning models like r1 in conjunction with information retrieval and intelligent agent systems.

Please contact us via LinkedInDean and Deniz for further discussion and collaboration.

Follow Us for the latest updates on AI technologies and tools!

Leave a Comment