πŸ€– AI/ML

ReAct

Reasoning and Acting

An agent framework that interleaves reasoning and acting. Effective for solving complex tasks.

πŸ“– Detailed Explanation

ReAct (Reasoning and Acting) is a prompting framework in which an LLM solves complex problems by interleaving reasoning and acting. Rather than simply generating an answer, it repeats a cycle of thinking, using tools, and observing the results to produce more accurate outcomes.

ReAct was published in 2022 by Google Research and Princeton. The existing Chain-of-Thought (CoT) performed only reasoning and could not make use of external information, while the Action-only approach used tools without context. ReAct combined the two to create synergy between reasoning and acting.

The core of ReAct is the "Thought-Action-Observation" loop. It first analyzes the current situation (Thought), calls the necessary tools or APIs (Action), and decides the next step after checking the result (Observation). This process repeats until the goal is achieved.

In practice, ReAct has become a core pattern for AI agents. Major frameworks such as LangChain and LlamaIndex support the ReAct pattern by default, and it automates complex tasks by combining with various tools such as web search, database queries, and API calls.

πŸ’» Code Example

This is an example of implementing a ReAct agent using LangChain.

# pip install langchain langchain-openai duckduckgo-search
from langchain_openai import ChatOpenAI
from langchain.agents import create_react_agent, AgentExecutor
from langchain import hub
from langchain_community.tools import DuckDuckGoSearchRun

# 1. 도ꡬ μ •μ˜
search_tool = DuckDuckGoSearchRun()
tools = [search_tool]

# 2. LLM μ΄ˆκΈ°ν™”
llm = ChatOpenAI(model="gpt-4o", temperature=0)

# 3. ReAct ν”„λ‘¬ν”„νŠΈ λ‘œλ“œ
prompt = hub.pull("hwchase17/react")

# 4. ReAct μ—μ΄μ „νŠΈ 생성
agent = create_react_agent(llm, tools, prompt)

# 5. μ—μ΄μ „νŠΈ μ‹€ν–‰κΈ°
agent_executor = AgentExecutor(
    agent=agent,
    tools=tools,
    verbose=True,  # Thought-Action-Observation 좜λ ₯
    max_iterations=5,
    handle_parsing_errors=True
)

# 6. μ‹€ν–‰
result = agent_executor.invoke({
    "input": "2024λ…„ 노벨 물리학상 μˆ˜μƒμžμ™€ κ·Έλ“€μ˜ 연ꡬ μ£Όμ œλŠ”?"
})

print(f"μ΅œμ’… λ‹΅λ³€: {result['output']}")

# 좜λ ₯ μ˜ˆμ‹œ:
# Thought: 2024λ…„ 노벨 물리학상 정보λ₯Ό 검색해야겠닀
# Action: duckduckgo_search
# Action Input: 2024 Nobel Prize Physics winner
# Observation: 검색 κ²°κ³Ό...
# Thought: μˆ˜μƒμž 정보λ₯Ό μ°Ύμ•˜λ‹€. μ •λ¦¬ν•΄μ„œ λ‹΅λ³€ν•˜κ² λ‹€
# Final Answer: ...

πŸ—£οΈ Say It Like This in Practice

πŸ’¬ In a Meeting
"Simple RAG can't reflect real-time information, so if we connect a web search tool to a ReAct agent, it can answer with up-to-date information too."
πŸ’¬ In an Interview
"The core of ReAct is the Thought-Action-Observation loop. By separating reasoning and acting, it maximizes the LLM's planning capability."
πŸ’¬ In a Technical Discussion
"The limitation of ReAct agents is that the context runs short as the loop gets longer. Adding intermediate summaries or a memory system can improve this."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

⚠️ Common Mistakes & Cautions

❌
Not setting an infinite loop guard: Running without max_iterations can cause the agent to loop indefinitely. Always set a maximum number of iterations.
❌
Poor tool descriptions: If a tool's description is unclear, the LLM cannot select the appropriate tool. Write clear and specific descriptions.
βœ…
Add error handling: Handle parsing errors with handle_parsing_errors=True, and implement fallback logic for when a tool fails.

πŸ”— Related Terms

πŸ“š Learn More