How to beginner · 3 min read

How to use LangGraph for stateful agents

Quick answer
Use LangGraph by defining a StateGraph with typed state, adding nodes as functions that update state, and compiling it into an app to invoke stateful agents. This enables managing conversation or task state across multiple steps with simple Python code.

PREREQUISITES

  • Python 3.8+
  • pip install langgraph
  • Basic Python typing knowledge

Setup

Install the langgraph package via pip and prepare your Python environment.

bash
pip install langgraph
output
Collecting langgraph
  Downloading langgraph-0.1.0-py3-none-any.whl (15 kB)
Installing collected packages: langgraph
Successfully installed langgraph-0.1.0

Step by step

Define a typed State dictionary, create node functions that update the state, build a StateGraph, add nodes and edges, compile the graph, and invoke it with initial state.

python
from langgraph.graph import StateGraph, END
from typing import TypedDict

# Define the state schema
class State(TypedDict):
    messages: list[str]

# Node function that appends a response

def my_node(state: State) -> State:
    new_messages = state["messages"] + ["Hello from LangGraph!"]
    return {"messages": new_messages}

# Create the graph and add node
graph = StateGraph(State)
graph.add_node("my_node", my_node)
graph.set_entry_point("my_node")
graph.add_edge("my_node", END)

# Compile and invoke the graph
app = graph.compile()
result = app.invoke({"messages": []})
print(result)  # {'messages': ['Hello from LangGraph!']}
output
{'messages': ['Hello from LangGraph!']}

Common variations

  • Use multiple nodes to represent different agent steps or states.
  • Chain nodes with edges to create multi-step workflows.
  • Persist state externally by saving result between invocations.
  • Use async functions if your nodes perform I/O operations.
python
from langgraph.graph import StateGraph, END
from typing import TypedDict
import asyncio

class State(TypedDict):
    messages: list[str]

async def async_node(state: State) -> State:
    await asyncio.sleep(0.1)  # simulate async work
    new_messages = state["messages"] + ["Async response"]
    return {"messages": new_messages}

graph = StateGraph(State)
graph.add_node("async_node", async_node)
graph.set_entry_point("async_node")
graph.add_edge("async_node", END)
app = graph.compile()

import asyncio
result = asyncio.run(app.invoke({"messages": []}))
print(result)  # {'messages': ['Async response']}
output
{'messages': ['Async response']}

Troubleshooting

  • If you see TypeError about state, ensure your node functions accept and return the correct TypedDict type.
  • For async nodes, use async def and invoke with asyncio.run().
  • If the graph does not run, verify you set the entry point with set_entry_point().

Key Takeaways

  • Define your agent's state with a TypedDict for type safety and clarity.
  • Add nodes as pure functions that update and return the state dictionary.
  • Compile the StateGraph to create an app that manages stateful invocation.
  • Use edges to control flow and chain multiple agent steps.
  • Async nodes require async invocation with asyncio.run or equivalent.
Verified 2026-04
Verify ↗