Understanding ReAct Agents with LangGraph

šŸ“š This guide builds on practical examples and training resources (see References at the end: Udemy course + GitHub repo) to help you implement ReAct agents with LangGraph.
#langgraphreact-pattern
single

šŸŽÆ Core Concept: ReAct Pattern Implementation

What is ReAct?

ReAct (Reasoning and Acting) is an AI pattern that alternates between:

  • Reasoning: LLM analyzes the problem and decides what to do
  • Acting: External tools are executed to gather information or perform actions
  • Iterative Loop: Process continues until the task is complete

LangGraph Implementation Architecture

Key Components:

  • react.py: Tool definitions (TavilySearch, triple function) and LLM setup
  • nodes.py: Agent reasoning and tool execution nodes
  • main.py: StateGraph workflow orchestration

šŸ”§ Function Calling Deep Dive

Critical Understanding: Function Calling ≠ Function Execution

What bind_tools Actually Does:

  • āœ… Function Planning: LLM learns what tools exist and how to request them
  • āœ… Parameter Generation: LLM outputs JSON with function name and arguments
  • āŒ NOT Execution: Tools are never executed by the LLM itself

The Two-Phase Process:

# Phase 1: LLM Planning (bind_tools works here)
llm = ChatOpenAI(model="gpt-4o-mini").bind_tools(tools)
response = llm.invoke([...])  # Returns tool_calls in JSON

# Phase 2: Tool Execution (your environment)
tool_node = ToolNode(tools)
results = tool_node.invoke(state)  # Actually runs the tools

Security Implication:

  • LLM can only suggest tool usage
  • Your code controls if/when/how tools execute
  • Complete separation of planning vs execution

🌐 MCP vs Function Calling Comparison

AspectFunction CallingMCP (Model Context Protocol)
Tool DiscoveryStatic at startupDynamic at runtime
ArchitectureApp ↔ LLM ↔ Static ToolsApp ↔ LLM ↔ MCP Client ↔ Multiple Servers
FlexibilityFixed tools, restart needed for changesTools can be added/removed without restart
StandardizationVendor-specific implementationsUniversal protocol
Use CasesSimple apps, prototypingEnterprise, microservices, dynamic environments

Core Purpose (Same for Both):

LLM Limitations → Tool Integration → Enhanced Capabilities → Better Outputs

Both solve the fundamental problem of extending LLM capabilities beyond training data by enabling interaction with the "outside world."


šŸ“Š Adding Comprehensive Logging

Why Logging is Essential

Your implementation now includes detailed logging that shows:

  • State Evolution: How messages accumulate at each step
  • LLM Decision Making: What tools the LLM chooses and why
  • Tool Execution: Which tools run and their results
  • Flow Control: Decision logic at each branch point

Example Log Flow

šŸš€ LANGGRAPH FLOW STARTED
🧠 AGENT_REASON Node - Starting reasoning...
šŸ“¤ LLM Response: Has tool calls: True
šŸ¤” DECISION Point: Continue to ACT
šŸ”§ ACT Node - Starting tool execution...
šŸ“¤ Tool execution results: [temperature data]
🧠 AGENT_REASON Node - Starting reasoning... (iteration 2)
šŸ Decision: END (no more tools needed)

⚔ LangGraph Manual vs LangChain Built-in ReAct

Approach (LangGraph Manual)

# Full control and customization
def run_agent_reasoning(state: MessagesState) -> MessagesState:
    # Custom reasoning logic with detailed logging
    response = llm.invoke([...])
    return {"messages": [response]}

flow = StateGraph(MessagesState)
flow.add_node(AGENT_REASON, run_agent_reasoning)
# Explicit graph construction

Built-in Approach

# Quick but limited
from langchain.agents import create_react_agent, AgentExecutor
agent = create_react_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools)
result = agent_executor.invoke({"input": query})  # Black box

When to Use Each

Your LangGraph Approach (Better for):

  • šŸŽÆ Learning: Understand ReAct internals
  • šŸ”§ Customization: Need specific workflows
  • šŸ“Š Production: Require full control and optimization
  • šŸ› Debugging: Detailed state inspection needed

Built-in ReAct (Better for):

  • šŸš€ Prototyping: Quick setup and testing
  • šŸ“ Standard Use Cases: Default behavior sufficient
  • ā° Time Constraints: Minimal code needed

šŸ“š References & Resources

thongvmdev_M9VMOt
WRITTEN BY

thongvmdev

Share and grow together