Day 8/30: ReAct Loop in LangGraph A developer building a support bot with LangGraph's ReAct loop encountered a memory issue where the bot forgets user input from two turns ago. The StateGraph lacks built-in context, requiring integration with MCP tools for memory. The developer warns of infinite loop risks without proper termination conditions. So you've built an agentic AI system with LangGraph and MCP, and it's working great - until it starts forgetting what the user said two turns ago. You're trying to implement a simple support bot that can answer follow-up questions, but it keeps responding as if it has no memory. You've checked the LangGraph documentation, and it seems like the StateGraph should be able to handle this kind of context. But for some reason, it's just not working. Let's take a step back and look at how LangGraph's ReAct loop is supposed to work. The idea is that your agent will reason about the current state of the world, act based on that reasoning, observe the consequences of its action, and then repeat the process. This loop is the core of how LangGraph agents make decisions and learn from their environment. In your support bot, the ReAct loop might look something like this: python import langgraph as lg Create a new StateGraph graph = lg.StateGraph Add some nodes and edges to the graph graph.add node "greeting", "Hello How can I help you?" graph.add node "question", "You asked a question" graph.add node "answer", "Here's an answer to your question" Add some conditional edges between the nodes graph.add conditional edges "greeting", "question", lambda x: x "user input" = "" , "question", "answer", lambda x: x "user input" = "" Define a checkpoint to save the current state of the graph def checkpoint state : Save the state to a database or file print "Checkpointing state:", state Run the ReAct loop while True: Reason: get the current state of the graph state = graph.get current state Act: select the next node to visit based on the current state next node = graph.get next node state Observe: get the user's input and update the state user input = input "User: " state "user input" = user input Repeat: update the graph and checkpoint the state graph.update state state checkpoint state This code sets up a simple StateGraph with three nodes: a greeting, a question, and an answer. The ReAct loop runs indefinitely, reasoning about the current state of the graph, acting based on that reasoning, observing the user's input, and repeating the process. But here's the thing: if you run this code, you'll notice that the bot doesn't actually remember what the user said two turns ago. That's because the StateGraph is designed to be a relatively simple, reactive system - it doesn't have any built-in memory or context. To fix this, you need to add some kind of memory or context to the StateGraph . One way to do this is by using LangGraph's MCP tools to create a more complex, contextual model of the user's input. We'll dive into that tomorrow, but for now, let's just say that it's a good idea to use a combination of StateGraph and MCP to build a more robust, contextual AI system. One practical gotcha to watch out for when working with the ReAct loop is that it can be easy to get stuck in an infinite loop if your agent doesn't have a clear way to terminate or exit the loop. For example, if your agent is designed to keep responding to user input indefinitely, you may need to add some kind of timeout or exit condition to prevent it from running forever. Looking ahead, we'll be exploring more advanced techniques for building contextual AI systems with LangGraph and MCP - including how to integrate multiple models and systems to create more sophisticated, human-like behavior.