Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Workflow:Langchain ai Langgraph ReAct Agent Creation

From Leeroopedia
Knowledge Sources
Domains LLM_Ops, Agent_Orchestration, Tool_Calling
Last Updated 2026-02-11 15:00 GMT

Overview

End-to-end process for creating a tool-calling ReAct agent using LangGraph's prebuilt `create_react_agent` API with a language model and custom tools.

Description

This workflow covers building a ReAct (Reasoning + Acting) agent that iteratively calls a language model, decides whether to invoke tools, executes those tools, and feeds results back to the model until a final response is produced. The `create_react_agent` function abstracts the full agent loop into a single call, internally constructing a StateGraph with an "agent" node (LLM), a "tools" node (ToolNode), and conditional edges that route between them. The agent supports structured output, pre/post model hooks, dynamic model selection, and human-in-the-loop interrupts.

Usage

Execute this workflow when you need an LLM-powered agent that can use external tools to answer questions or perform actions. This is the highest-level API for building agents in LangGraph, suitable for chatbots with tool access, information retrieval agents, task automation agents, and any scenario where an LLM needs to iteratively reason and act. Use it when you want a production-ready agent loop without manually constructing the graph.

Execution Steps

Step 1: Define Tools

Create the tools that the agent will have access to. Tools are Python functions decorated with `@tool` from LangChain, or any callable with a docstring. Each tool must have a clear name and description that the LLM uses to decide when and how to call it. Tools can access graph state via `InjectedState` annotations and the persistent store via `InjectedStore`.

Key considerations:

  • Every tool needs a descriptive docstring for the LLM to understand its purpose
  • Use type annotations for tool parameters to enable proper schema generation
  • Set `return_direct=True` on tools that should terminate the agent loop immediately
  • Tools can return `Command` objects for advanced state updates and routing

Step 2: Configure the Language Model

Select and configure a chat model that supports tool calling (e.g., ChatAnthropic, ChatOpenAI). The model can be provided as a static instance, a string identifier (e.g., `"anthropic:claude-3-7-sonnet"`), or a dynamic callable that selects the model based on runtime state. Optionally configure a system prompt that provides context and instructions to the LLM.

Key considerations:

  • The model must support the tool calling interface (bind_tools)
  • A string identifier auto-initializes the model via `init_chat_model`
  • Dynamic model callables receive `(state, runtime)` and return a model instance
  • System prompts can be strings, `SystemMessage` objects, or callable prompt templates

Step 3: Create the ReAct Agent

Call `create_react_agent()` with the model, tools, and optional configuration. This constructs the full agent graph internally with an "agent" node that calls the LLM and a "tools" node that executes tool calls. The function returns a compiled `CompiledStateGraph` ready for execution.

Key considerations:

  • Pass `checkpointer` for persistent conversation memory
  • Pass `store` for cross-session data persistence
  • Use `version="v2"` for parallel tool execution via Send API
  • Set `response_format` for structured final output (Pydantic schema)
  • Add `pre_model_hook` for message trimming or summarization before each LLM call
  • Add `post_model_hook` for validation or human review after each LLM response

Step 4: Execute the Agent

Run the agent using `invoke()` for a single response or `stream()` for real-time output. Provide input as a messages list in the state. The agent loops between the LLM and tools until the model produces a response without tool calls, at which point it returns the final state.

Key considerations:

  • Input format: `{"messages": [("user", "your question")]}`
  • The agent automatically manages the ReAct loop (reason → act → observe → repeat)
  • `remaining_steps` prevents infinite loops (configurable via `recursion_limit`)
  • Use streaming to observe intermediate reasoning and tool execution in real-time

Step 5: Handle Agent Output

Process the agent's response from the returned state. The final state contains the full message history including all reasoning steps, tool calls, and tool results. If `response_format` was specified, the structured response is available in `structured_response`.

Key considerations:

  • The `messages` list contains the complete conversation including tool interactions
  • Filter for `AIMessage` without `tool_calls` to get the final response
  • Access `structured_response` when using structured output format
  • Use the message history for debugging, logging, or display

Execution Diagram

GitHub URL

Workflow Repository