# Agentic Multi-Stage RAG with Cohere Tools API

> This page describes how to build a powerful, multi-stage agent with the Cohere platform.

<AuthorsContainer
  authors={[
    {
      name: "Jason Jung",
      imageSrc: "https://fern-image-hosting.s3.amazonaws.com/cohere/0803e3d-Jason_Jung.jpg",
    },
  ]}
/>

<CookbookHeader href="https://github.com/cohere-ai/cohere-developer-experience/blob/main/notebooks/agents/agentic-RAG/agentic_multi_stage_rag_native.ipynb" />

## Motivation

Retrieval augmented generation (RAG) has been a go-to use case that enterprises have been adopting with large language models (LLMs). Even though it works well in general, there are edge cases where this can fail. Most commonly, when the retrieved document mentions the query but actually refers to another document, the model will fail to generate the correct answer.

We propose an agentic RAG system that leverages tool use to continue to retrieve documents if correct ones were not retrieved at first try. This is ideal for use cases where accuracy is a top priority and latency is not. For example, lawyers trying to find the most accurate answer from their contracts are willing to wait a few more seconds to get the answer instead of getting wrong answers fast.

## Objective

This notebook, we will explore how we can build a simple agentic RAG using Cohere's native API. We have prepared a fake dataset to demonstrate the use case.
We ask three questions that require different depths of retrieval. We will see how the model answers the question between simple and agentic RAG.

## Disclaimer

One of the challenges in building a RAG system is that it has many moving pieces: vector database, type of embedding model, use of reranker, number of retrieved documents, chunking strategy, and more. These components can make debugging and evaluating RAG systems difficult. Since this notebook focuses on the concept of agentic RAG, it will simplify other parts of the RAG system. For example, we will only retrieve top 1 document to demonstrate what happens when retrieved document does not contain the answer needed.

## Result

| Type                   | Question                                                  | Simple Rag                                                                                                                                                                                       | Agentic Rag                                                                                                                  |
| ---------------------- | --------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------- |
| Single-stage retrieval | Is there a state level law for wearing helmets?           | There is currently no state law requiring the use of helmets when riding a bicycle. However, some cities and counties do require helmet use.                                                     | There is currently no state law requiring helmet use. However, some cities and counties do require helmet use with bicycles. |
| Multi-stage retrieval  | I live in orting, do I need to wear a helmet with a bike? | In the state of Washington, there is no law requiring you to wear a helmet when riding a bike. However, some cities and counties do require helmet use, so it is worth checking your local laws. | Yes, you do need to wear a helmet with a bike in Orting if you are under 17.                                                 |

As you will see more below, the multi-stage retrieval is achieved by adding a new function `reference_extractor()` that extracts other references in the documents and updating the instruction so the agent continues to retrieve more documents.

```python PYTHON
import os
from pprint import pprint

import cohere
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
```

```python PYTHON