# Day 1/30: ReAct Pattern Explained

> Source: <https://dev.to/yashwanth_kasi/day-130-react-pattern-explained-3nbf>
> Published: 2026-07-12 08:25:11+00:00

I was recently tasked with building a support bot that could handle customer inquiries about our company's products. The bot was supposed to be able to understand the context of the conversation and respond accordingly. However, I soon realized that my initial implementation had a major flaw - the bot would often forget the context of the conversation and respond with irrelevant answers. For example, if a customer asked about the features of a specific product, the bot would respond with a generic description of the product, without taking into account the customer's previous questions.

This is when I stumbled upon the ReAct pattern, a fundamental concept in building agentic AI systems. The ReAct pattern is a simple yet powerful idea that helps AI agents maintain context and make decisions based on their current state. In the context of my support bot, the ReAct pattern helped me understand how to design the bot's behavior to take into account the conversation history and respond accordingly.

The ReAct pattern consists of three main components: **Reasoning**, **Action**, and **Context**. The **Reasoning** component is responsible for evaluating the current state of the agent and determining the best course of action. The **Action** component is responsible for executing the chosen action, and the **Context** component is responsible for updating the agent's state based on the outcome of the action.

In the case of my support bot, the **Reasoning** component would evaluate the customer's input and determine the best response based on the conversation history. The **Action** component would then generate the response, and the **Context** component would update the conversation history to reflect the customer's new question.

Here's an example of how I implemented the ReAct pattern in my support bot using LangGraph and MCP:

``` python
import langgraph as lg
from mcp import tools

# Define the support bot's state graph
state_graph = lg.StateGraph()

# Add nodes for each possible state
state_graph.add_node("start")
state_graph.add_node("product_inquiry")
state_graph.add_node("pricing_inquiry")

# Add conditional edges between nodes
state_graph.add_conditional_edges(
    ("start", "product_inquiry", lambda x: x["intent"] == "product_inquiry"),
    ("product_inquiry", "pricing_inquiry", lambda x: x["intent"] == "pricing_inquiry")
)

# Define the reasoning function
def reasoning(state, input_data):
    # Evaluate the customer's input and determine the best course of action
    if input_data["intent"] == "product_inquiry":
        return "product_inquiry"
    elif input_data["intent"] == "pricing_inquiry":
        return "pricing_inquiry"
    else:
        return "start"

# Define the action function
def action(state, input_data):
    # Generate a response based on the current state and customer input
    if state == "product_inquiry":
        return "What product would you like to know more about?"
    elif state == "pricing_inquiry":
        return "What is your budget for the product?"
    else:
        return "Welcome to our support bot! How can I help you today?"

# Define the context function
def context(state, input_data):
    # Update the conversation history based on the customer's input
    if input_data["intent"] == "product_inquiry":
        return {"conversation_history": ["product_inquiry"]}
    elif input_data["intent"] == "pricing_inquiry":
        return {"conversation_history": ["pricing_inquiry"]}
    else:
        return {"conversation_history": []}

# Create an MCP tool to interact with the support bot
tool = tools.Tool(
    state_graph=state_graph,
    reasoning_function=reasoning,
    action_function=action,
    context_function=context
)

# Test the support bot
input_data = {"intent": "product_inquiry", "text": "I'm interested in learning more about your products"}
response = tool.interact(input_data)
print(response)  # Output: What product would you like to know more about?
```

One practical gotcha I learned while implementing the ReAct pattern is that it's essential to carefully design the state graph and conditional edges to ensure that the agent can transition between states correctly. If the state graph is not well-designed, the agent may get stuck in an infinite loop or fail to respond to certain inputs.

As I continue to work on my support bot, I'm excited to explore more advanced topics in agentic AI, such as how to integrate multiple AI models and handle complex decision-making scenarios. Tomorrow, I'll be diving deeper into the world of LangGraph and MCP, and I'm looking forward to sharing my discoveries with you.
