{"slug": "day-8-30-react-loop-in-langgraph", "title": "Day 8/30: ReAct Loop in LangGraph", "summary": "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.", "body_md": "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`\n\nshould be able to handle this kind of context. But for some reason, it's just not working.\n\nLet's take a step back and look at how LangGraph's `ReAct`\n\nloop 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.\n\nIn your support bot, the `ReAct`\n\nloop might look something like this:\n\n``` python\nimport langgraph as lg\n\n# Create a new StateGraph\ngraph = lg.StateGraph()\n\n# Add some nodes and edges to the graph\ngraph.add_node(\"greeting\", \"Hello! How can I help you?\")\ngraph.add_node(\"question\", \"You asked a question\")\ngraph.add_node(\"answer\", \"Here's an answer to your question\")\n\n# Add some conditional edges between the nodes\ngraph.add_conditional_edges(\n    (\"greeting\", \"question\", lambda x: x[\"user_input\"] != \"\"),\n    (\"question\", \"answer\", lambda x: x[\"user_input\"] != \"\")\n)\n\n# Define a checkpoint to save the current state of the graph\ndef checkpoint(state):\n    # Save the state to a database or file\n    print(\"Checkpointing state:\", state)\n\n# Run the ReAct loop\nwhile True:\n    # Reason: get the current state of the graph\n    state = graph.get_current_state()\n\n    # Act: select the next node to visit based on the current state\n    next_node = graph.get_next_node(state)\n\n    # Observe: get the user's input and update the state\n    user_input = input(\"User: \")\n    state[\"user_input\"] = user_input\n\n    # Repeat: update the graph and checkpoint the state\n    graph.update_state(state)\n    checkpoint(state)\n```\n\nThis code sets up a simple `StateGraph`\n\nwith three nodes: a greeting, a question, and an answer. The `ReAct`\n\nloop runs indefinitely, reasoning about the current state of the graph, acting based on that reasoning, observing the user's input, and repeating the process.\n\nBut 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`\n\nis designed to be a relatively simple, reactive system - it doesn't have any built-in memory or context.\n\nTo fix this, you need to add some kind of memory or context to the `StateGraph`\n\n. One way to do this is by using LangGraph's `MCP`\n\ntools 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`\n\nand `MCP`\n\nto build a more robust, contextual AI system.\n\nOne practical gotcha to watch out for when working with the `ReAct`\n\nloop 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.\n\nLooking 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.", "url": "https://wpnews.pro/news/day-8-30-react-loop-in-langgraph", "canonical_source": "https://dev.to/yashwanth_kasi/day-830-react-loop-in-langgraph-4nm0", "published_at": "2026-07-18 05:16:07+00:00", "updated_at": "2026-07-18 05:59:54.468396+00:00", "lang": "en", "topics": ["ai-agents", "developer-tools", "machine-learning"], "entities": ["LangGraph", "MCP", "StateGraph", "ReAct"], "alternates": {"html": "https://wpnews.pro/news/day-8-30-react-loop-in-langgraph", "markdown": "https://wpnews.pro/news/day-8-30-react-loop-in-langgraph.md", "text": "https://wpnews.pro/news/day-8-30-react-loop-in-langgraph.txt", "jsonld": "https://wpnews.pro/news/day-8-30-react-loop-in-langgraph.jsonld"}}