🤖 AI/ML

Agentic RAG

Agentic RAG

RAG in which an agent actively performs retrieval and generation. Effective for complex questions.

📖 Detailed Explanation

Agentic RAG is an advanced retrieval-generation system that adds autonomous decision-making capability to conventional RAG (Retrieval-Augmented Generation). Going beyond simply receiving a query, retrieving, and responding, an AI agent plans the retrieval strategy, evaluates the retrieval results, and, when necessary, performs additional retrieval or explores other data sources.

It began to attract serious attention from 2024 as a way to overcome the limitations of conventional RAG. Simple RAG had limitations with complex questions or cases requiring multi-step reasoning, and LlamaIndex's Agent feature and LangChain's Self-Query Retriever became established as representative implementations of Agentic RAG.

The core capabilities of Agentic RAG are Query Decomposition (breaking down complex questions), Adaptive Retrieval (adjusting strategy based on retrieval results), Self-Reflection (self-evaluating response quality), and Multi-Source Routing (selecting data sources according to question type). For example, the question "Compare Samsung Electronics' 2023 revenue with SK hynix's revenue" is decomposed into two sub-queries, each of which is retrieved and then combined.

In an enterprise knowledge management system, it can connect multiple sources such as internal documents, emails, and Slack messages to answer complex questions. In the legal/financial sector, it tracks the history of regulatory changes to provide up-to-date information, and in customer service, it synthesizes FAQs, manuals, and previous tickets to generate accurate answers.

💻 Code Example

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core.agent import ReActAgent
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.llms.openai import OpenAI

# 여러 데이터 소스에서 인덱스 생성
hr_docs = SimpleDirectoryReader("./data/hr").load_data()
finance_docs = SimpleDirectoryReader("./data/finance").load_data()
product_docs = SimpleDirectoryReader("./data/product").load_data()

hr_index = VectorStoreIndex.from_documents(hr_docs)
finance_index = VectorStoreIndex.from_documents(finance_docs)
product_index = VectorStoreIndex.from_documents(product_docs)

# 각 인덱스를 도구로 변환
hr_tool = QueryEngineTool(
    query_engine=hr_index.as_query_engine(),
    metadata=ToolMetadata(
        name="hr_policy",
        description="인사 정책, 휴가, 복리후생 관련 질문에 사용"
    )
)

finance_tool = QueryEngineTool(
    query_engine=finance_index.as_query_engine(),
    metadata=ToolMetadata(
        name="financial_data",
        description="회사 재무제표, 매출, 예산 관련 질문에 사용"
    )
)

product_tool = QueryEngineTool(
    query_engine=product_index.as_query_engine(),
    metadata=ToolMetadata(
        name="product_info",
        description="제품 스펙, 가격, 재고 관련 질문에 사용"
    )
)

# Agentic RAG: ReAct 에이전트 생성
llm = OpenAI(model="gpt-4", temperature=0)
agent = ReActAgent.from_tools(
    [hr_tool, finance_tool, product_tool],
    llm=llm,
    verbose=True,  # 추론 과정 출력
    max_iterations=10
)

# 복잡한 질문도 자동으로 분해하여 처리
response = agent.chat(
    "우리 회사 주력 제품의 2023년 매출은 얼마이고, "
    "해당 제품팀의 휴가 정책은 어떻게 되나요?"
)
print(response)

🗣️ Examples of Practical Conversations

📊 In a RAG System Improvement Meeting

"With conventional RAG, we can't properly answer comparison questions like 'revenue change compared to last quarter.' If we switch to Agentic RAG, it breaks the question down, retrieves the data for each of the two quarters separately, and even performs the comparative analysis automatically."

💼 In a technical interview

"Please explain the difference between Agentic RAG and regular RAG. Regular RAG is a one-pass retrieval-generation pipeline, while Agentic RAG has an agent dynamically decide the retrieval strategy and repeat retrieval when necessary. Self-RAG and CRAG are similar concepts."

🔧 In Architecture Design

"If there are 5 or more data sources, it's better to add a Router and go with Agentic RAG. It's designed to select the appropriate source according to the question type, and to explore additional sources if the retrieval results are insufficient."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Cautions

⏱️
Increased Response Latency

Multi-step reasoning and repeated retrieval make response times 2 to 5 times longer than regular RAG. For services where real-time performance matters, consider timeout settings and streaming responses.

💰
Increased API Costs

LLM calls occur multiple times during the agent's reasoning process. Complex questions can trigger 10 or more calls, so a max_iterations limit and cost monitoring are essential.

🔍
Dependence on Retrieval Quality

If the baseline retrieval quality is low, it is difficult for Agentic RAG to produce good results as well. First optimize the vector index quality, chunking strategy, and embedding model, and then add Agentic features.

🔗 Related Terms

📚 Learn More