Building an agentic RAG system
Our agent needs tools to work with. We will give it two: a RAG tool for searching Green Bites' internal knowledge base, and a DuckDuckGo tool for searching the web when the answer is not in our data.
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
# Tool 1: RAG over Green Bites knowledge base
rag_tool = QueryEngineTool(
query_engine=query_engine,
metadata=ToolMetadata(
name="green_bites_knowledge",
description=(
"Use this tool for ANY question about Green Bites restaurant "
"including menu items, policies, hours, ingredients, and "
"company information. Always try this tool FIRST."
),
),
)
# Tool 2: Web search for external information
web_search = DuckDuckGoSearchToolSpec()
web_tools = web_search.to_tool_list()
all_tools = [rag_tool] + web_toolsDefine RAG and web search tools for the agent
Absolutely. LlamaIndex supports any number of custom tools. You could add a SQL query tool for structured data, a calculator for numerical questions, or even an email tool to escalate complex issues. The agent will read each tool's description and choose the right one for each question. The key is writing clear, specific descriptions so the agent routes correctly.
Tool descriptions are critical for agent routing. The agent reads these descriptions to decide which tool to use. A vague description leads to wrong tool selection. Notice how our RAG tool description specifically says "Always try this tool FIRST", which guides the agent to prioritize internal knowledge.
from llama_index.core.agent import ReActAgent
agent = ReActAgent.from_tools(
tools=all_tools,
llm=Settings.llm,
verbose=True # Shows the Think → Act → Observe steps
)
# Multi-step question: needs both internal data AND web search
response = agent.chat(
"What is the main protein source in the Mushroom Burger, "
"and what are its health benefits?"
)
print(response)
# Agent will:
# 1. Think: I need to find the Mushroom Burger ingredients
# 2. Act: Search Green Bites knowledge base
# 3. Observe: Main ingredient is portobello mushrooms
# 4. Think: Now I need health benefits of portobello mushrooms
# 5. Act: Search the web via DuckDuckGo
# 6. Observe: Rich in B vitamins, potassium, selenium...
# 7. Final Answer: Combines both sourcesCreate a ReAct agent and ask a multi-step question
Multi-step agent reasoning
The agent chains multiple tool calls to build a complete answer.
Quiz: Quiz
Loading practice…
Matching exercise: Match agentic RAG Components
Loading practice…