If you’ve been exploring LangChain lately or following our article thread, you’ve probably come across LangGraph. It’s a new framework that lets you build agents that think in steps, follow logical paths, and keep track of what they’ve already done. Instead of chaining prompts together and hoping things work out, LangGraph gives your agent a structure — a clear map of how information moves and changes as it works.
We’ve already covered the basics of LangGraph Agents in a previous article, where we explained how agents can reason through tasks and coordinate tools. In this piece, we’re going one layer deeper to talk about something that makes those agents truly capable: state.
Before we dive in, here’s something you’ll love:
Learn LangChain the clear, concise, and practical way.
Whether you’re just starting out or already building, Langcasts gives you guides, tips, hands-on walkthroughs, and in-depth classes to help you master every piece of the AI puzzle. No fluff, just actionable learning to get you building smarter and faster. Start your AI journey today at Langcasts.com.
Think of state as your agent’s short-term memory. It’s what keeps track of what has been said, what’s been done, and what comes next. Without it, your agent would forget everything the moment a step finishes. With it, your agent can carry context, recall information, and act with purpose.
In this guide, you’ll learn what state is, why it matters, and how to use it to build your first stateful agent. We’ll start simple, walk through a hands-on example, and share tips to help you avoid common mistakes. If you prefer to learn visually, you can also check out the LangGraph team’s video on this topic here.
By the end, you’ll know how to make your agent not just respond, but remember and reason through its work.
What is “State” in LangGraph?
In simple terms, state is the data your agent keeps track of as it runs. It’s everything your agent knows at a given moment — the current input, the tools it has used, what it has said, and what it still needs to do.
Imagine your agent as a person solving a problem step by step. Each time they finish a task, they make a quick note before moving on — that note is the state. It helps them remember what just happened so they can make the next move intelligently.
LangGraph uses this same idea. Every time your agent completes an action, the state gets updated. That update becomes the foundation for whatever comes next. Without it, your agent would start every step as if it had no history, no context, and no clue what it was doing before.
In LangGraph, state is a structured object. You define it yourself, usually as a simple Python class or dictionary. This makes it flexible and transparent. You decide what to store: maybe it’s the conversation history, a running summary, or a few key variables. LangGraph then passes that state between nodes in your workflow, keeping everything connected and consistent.
Key Concepts You Should Know
A few core ideas that make LangGraph work.
-
State Schema
The state schema is the blueprint for your agent’s memory. It defines what kind of data your state holds, things like user messages, results from tools, or progress markers. You can create this schema using Python’s
TypedDict
,dataclass
, or Pydantic models. Keeping it structured helps you stay organized and makes debugging easier. - State Reducer:The reducer is a function that dictates how a new piece of data returned by a node combines with the existing data in the state. For example, for a conversation history, the reducer ensures new messages are appended to the list, not overwritten. If you don't define one, the old data is simply replaced by the new data.
- Nodes A node is a single step in your agent’s workflow. Each node takes the current state as input, performs some logic, like calling an LLM, running a tool, or making a decision, and then returns an updated dictionary of the state. You can think of nodes as the individual actions your agent takes while moving through its process.
- Edges Edges connect the nodes. They define how the workflow moves from one node to another. Sometimes this path is simple—one node always leads to the next. Other times it’s conditional, where the agent decides which branch to follow based on what’s inside the state.
- Graph Execution When you put nodes and edges together, you get a graph. The graph defines the logic of your workflow—what happens first, what comes next, and what conditions guide the flow. LangGraph takes care of running this graph step by step, passing the state through each node and updating it as it goes.
- Persistence and Memory Sometimes, you’ll want your agent to remember information even after it finishes a run. That’s where persistence comes in. LangGraph lets you save and reload the state so your agent can pick up where it left off. This is what allows long-running agents, ongoing conversations, or workflows that span multiple sessions.
- Tools and Agent State LangGraph also gives you the flexibility to connect tools—like APIs, databases, or custom functions—that can read and update the state directly. This is powerful because it lets your agent not just think but act. For example, a research agent might store search results in the state, or a support bot might log each response for later analysis.
Once you understand these building blocks, the rest of LangGraph starts to make sense. Everything revolves around how state flows through nodes and edges. In the next section, we’ll build a simple agent with state.
Step-by-Step Walkthrough: Build a Simple Agent with State
Now that you know what state is, let’s build a small example to see how it actually works.
Step 1: Install LangGraph
First, make sure you have LangGraph and LangChain installed:
pip install langgraph langchain openai
This gives you the tools you need to create and run your first graph.
Step 2: Define the State
Your state is just a Python structure that stores information as your agent runs. Let’s create a simple one that holds a list of messages.
from typing import TypedDict, List
class ChatState(TypedDict):
messages: List[str]
Here, the state has a single key — messages
— that will keep track of the conversation.
Step 3: Create the Nodes
Each node is a step in your workflow. Let’s make one node that receives user input and another that simulates the agent’s response.
def user_input_node(state: ChatState) -> ChatState:
user_message = input("You: ")
state["messages"].append(f"User: {user_message}")
# Return the state dictionary with the new message
return state
def agent_response_node(state: ChatState) -> ChatState:
last_message = state["messages"][-1]
reply = f"I see, you said: '{last_message.split(': ')[1]}'"
state["messages"].append(f"Agent: {reply}")
# Return the state dictionary with the agent's reply
return state
The first node records what the user says. The second reads that message and creates a response.
Step 4: Build the Graph
Now you connect the nodes so LangGraph knows the order of execution.
from langgraph.graph import StateGraph
graph = StateGraph(ChatState)
graph.add_node("input", user_input_node)
graph.add_node("respond", agent_response_node)
graph.add_edge("input", "respond")
graph.set_entry_point("input")
You’ve just built a tiny graph where “input” leads to “respond.”
Step 5: Run It
Finally, compile and run your graph.
app = graph.compile()
state = {"messages": []}
for _ in range(2):
state = app(state)
Run this in your terminal, and you’ll see a simple back-and-forth between you and the agent. Each time, the state updates with new messages, so the agent remembers what’s been said.
Step 6: Inspect the State
After running it, print the state to see what’s stored:
print(state)
You’ll see something like:
{'messages': ['User: Hello', "Agent: I see, you said: 'Hello'", ...]}
That’s your agent’s memory — the state — keeping track of the conversation as it unfolds.
Even with this tiny example, you’ve learned the core idea: the state moves through each step, carrying the information your agent needs to work intelligently. Once you understand that, you can build far more advanced agents that plan, loop, or collaborate across tasks, all powered by the same simple principle.
Real-World Examples of Using State
Now that you’ve seen how state works in a simple example, let’s look at how it powers real agent workflows.
1. A Chatbot That Remembers
Most chatbots forget everything as soon as you hit enter. With state, your chatbot can remember past questions, user preferences, and even previous answers. This makes conversations smoother and more natural. For example, when a user says, “Remind me to buy groceries later,” the agent can store that request in the state and recall it when needed.
2. A Research Assistant
Imagine a research agent that gathers information from multiple sources. Each time it finds new data, it adds it to the state. Later steps in the graph can summarize, organize, or compare those results. The state might contain keys like raw_search_results
and research_summary
. The state becomes the shared workspace where all the research lives.
3. A Task Planner
A planning agent can track progress through the state. It might start with a to-do list, then update each item as it completes a step. For example, if the goal is to “plan a trip to Japan,” the agent can store flight options, hotel picks, and itineraries inside the state, keeping everything in sync across steps.
4. Multi-Agent Collaboration
LangGraph also supports multiple agents working together. They can share a single state, passing updates as they complete tasks. One agent might handle data gathering, another analysis, and another reporting, all reading and writing to the same shared memory.
5. Support and Feedback Systems
In customer support scenarios, state can track the full history of a conversation. This allows the agent to stay consistent, avoid repeating questions, and escalate problems with full context. It can even store feedback to improve responses over time.
These examples show why state is so valuable. Whether it’s chatting, planning, researching, or collaborating, the same idea applies: the state is where your agent’s understanding lives.
Frequently Asked Questions
1. Do I always need to use state?
No. If your agent only handles single, simple requests, you can skip it. But once your workflow involves multiple steps, memory, or decisions, state becomes essential.
2. Is state the same as memory?
Not exactly. Memory is a part of the state — it’s what keeps track of conversations or events. The state, on the other hand, can hold anything your workflow needs, like results, flags, or user data.
3. Can I update the state manually?
Yes. You can modify the state directly in your nodes or through tools. This gives you full control over what your agent remembers or forgets.
4. What happens if a node fails?
LangGraph allows you to inspect the state and retry. Because the state is stored between steps, you don’t lose your progress when something goes wrong.
Conclusion
State is what makes LangGraph agents feel alive. It gives them the ability to remember, reason, and stay consistent across every step of their workflow. Once you understand it, you’re no longer just connecting prompts; you’re building systems that think and adapt.
We’ve already explored what LangGraph agents are in our earlier article, and now you’ve learned what makes them truly capable. Start small, experiment with your own state designs, and watch how your agents grow from simple responders into thoughtful problem-solvers.
Top comments (0)