Skip to main content

langgraph-react-agent

· 2 min read

LangGraph Logo

The ReAct agent was first introduced in the 2023 paper ReACT: Synergizing Reasoning and Acting in Language Models. It's an AI agent framework that combines reasoning with acting, enabling LLMs to integrate chain-of-thought (CoT) reasoning with external tool usage. The ReAct framework enhances LLM capabilities in complex tasks and decision-making processes.

LangGraph implements ReAct agents through the create_react_agent function, which builds a state graph (StateGraph) that automatically loops between LLM responses and tool calls based on the model's output.

This post explores LangGraph's source code to understand how ReAct agents are implemented under the hood.

Workflow Nodes and Flow Design

The ReAct workflow uses these key components:

  • Tool detection: Uses tool_calling_enabled = len(tool_classes) > 0 to determine if tools are available
  • Pre-model hook: Preprocesses state before agent execution. Useful for message trimming, summarization, and managing long conversation history to prevent overly long or irrelevant LLM inputs
  • Post-model hook: Processes state after agent execution but before the next step. Enables human-in-the-loop review, safety checks, validation, and enhanced guardrails

Without Tool Calling (tool_calling_enabled = False)

flowchart TD
pre_model_hook --> agent --> post_model_hook --> generate_structured_response

With Tool Calling (tool_calling_enabled = True)

The agent and tools can loop multiple times until no more tool calls are needed.

flowchart TD
pre_model_hook --> agent
agent --> post_model_hook
post_model_hook --"pending tool calls"--> tools
post_model_hook --"tools complete + structured output"--> generate_structured_response
post_model_hook --"tools complete + no structured output"--> END
generate_structured_response --> END
tools --> pre_model_hook

Summary

LangGraph's create_react_agent uses a StateGraph to flexibly combine agent, tools, hooks, and structured output nodes. It leverages conditional edges to enable multi-turn loops and branching, providing complete support for the ReAct agent's reasoning-acting cycle.

References