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:
import langgraph as lg
graph = lg.StateGraph()
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")
graph.add_conditional_edges(
("greeting", "question", lambda x: x["user_input"] != ""),
("question", "answer", lambda x: x["user_input"] != "")
)
def checkpoint(state):
print("Checkpointing state:", state)
while True:
state = graph.get_current_state()
next_node = graph.get_next_node(state)
user_input = input("User: ")
state["user_input"] = user_input
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.