Day 1/30: ReAct Pattern Explained A developer building a customer support bot encountered context loss issues and adopted the ReAct pattern to maintain conversation state. The pattern, implemented with LangGraph and MCP, uses Reasoning, Action, and Context components to enable the bot to respond appropriately based on conversation history. 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.