1. What is LangGraph?
LangGraph is a framework for building stateful, controllable, and multi-step AI applications using graph-based workflows.
It allows us to define how an AI application should execute tasks, make decisions, call tools, maintain memory, and move between different steps. Unlike a simple LLM application that follows a fixed sequence, LangGraph lets applications make decisions dynamically based on their current state.
Example: AI Customer Support Agent
Imagine building an AI customer support system that:
- 1Receives a customer's question.
- 2Understands the customer's intent.
- 3Checks the order database if necessary.
- 4Generates an appropriate response.
- 5Escalates the issue to a human if needed.
LangGraph helps us organize and control this entire process cleanly.
2. Why LangGraph? LangChain vs LangGraph
LangChain and LangGraph are complementary frameworks, but they solve different problems.
| LangChain | LangGraph |
|---|---|
| Build LLM applications | Build stateful AI workflows |
| Connect models and tools | Control execution between steps |
| Create chains and agents | Create graph-based agents |
| Integrate data sources | Manage shared application state |
| Simplify LLM interactions | Support persistence and human intervention |
When should you use them?
- Use LangChain when you need to integrate LLMs, prompts, tools, and data sources.
- Use LangGraph when you need control over complex workflows, agent decisions, memory, and execution.
You can also use LangChain components directly inside LangGraph applications.
3. Understanding Graph-Based Workflows
A graph is a structure consisting of connected components. In LangGraph, we use graphs to represent the execution flow of an AI application.
Consider our AI Customer Support Agent:
┌───────────────────────────┐
│ Customer Question │ "Where is my order?"
└─────────────┬─────────────┘
▼
┌───────────────────────────┐
│ Understand Intent │ Identify the customer's request
└─────────────┬─────────────┘
▼
┌───────────────────────────┐
│ Check Order │ Retrieve order information
└─────────────┬─────────────┘
▼
┌───────────────────────────┐
│ Generate Response │ Answer the customer
└─────────────┬─────────────┘
▼
┌───────────────────────────┐
│ Final Response │
└───────────────────────────┘Instead of writing the entire application as one large function, we divide it into smaller steps and connect them. This makes applications easier to understand, maintain, and extend.
4. Nodes, Edges, and State
LangGraph has three fundamental concepts.
Nodes
Nodes are individual functions that perform specific tasks. For example:
understand_intent: Understand the customer's question.check_order: Retrieve order information.generate_response: Generate the final answer.
Edges
Edges define how execution moves from one node to another. For example:
understand_intent → check_order → generate_response
Edges determine the execution flow of the graph.
State
State is the shared data structure that stores information throughout graph execution. For our customer support application, state might contain:
{
"customer_message": "Where is my order?",
"intent": "order_tracking",
"order_status": "Shipped",
"response": ""
}Each node can read the current state and return updates to it. In simple words:
- Nodes = What the application does.
- Edges = Where the application goes next.
- State = What the application remembers.
5. Installing and Setting Up LangGraph
First, create a Python project and virtual environment:
mkdir langgraph-course
cd langgraph-course
python -m venv venvActivate the virtual environment on Windows:
venv\Scripts\activateOr on Linux / macOS:
source venv/bin/activateInstall LangGraph:
pip install -U langgraphCreate a Python entry file:
touch main.pyYour project structure:
langgraph-course/
│
├── venv/
└── main.py6. Creating Your First StateGraph
Let's build a simple AI Customer Support workflow. First, import the required classes:
from langgraph.graph import StateGraph, START, ENDStateGraph allows us to create a graph-based workflow. START represents the beginning of the graph, while END represents its completion.
Now define a simple node:
def customer_support(state):
message = state["customer_message"]
return {
"response": f"We received your message: {message}"
}Create and compile the graph:
# Initialize the graph
builder = StateGraph(dict)
# Register the node
builder.add_node("customer_support", customer_support)
# Connect START to our node
builder.add_edge(START, "customer_support")
# Connect our node to END
builder.add_edge("customer_support", END)
# Compile the graph
graph = builder.compile()Invoke the graph:
result = graph.invoke({
"customer_message": "Where is my order?"
})
print(result)Output:
{
'customer_message': 'Where is my order?',
'response': 'We received your message: Where is my order?'
}We have successfully created and executed our first LangGraph workflow.
7. Defining Typed State
Using dict works, but it doesn't provide clear information about which fields our application expects. For larger applications, we should define a structured state using Python's TypedDict.
Let's update our customer support application:
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
# Define the graph state
class CustomerSupportState(TypedDict):
customer_message: str
intent: str
response: str
# Define a node
def understand_customer(state: CustomerSupportState):
message = state["customer_message"]
return {
"intent": "order_tracking",
"response": f"We are checking your request: {message}"
}
# Initialize the graph
builder = StateGraph(CustomerSupportState)
# Add node
builder.add_node("understand_customer", understand_customer)
# Define edges
builder.add_edge(START, "understand_customer")
builder.add_edge("understand_customer", END)
# Compile
graph = builder.compile()
# Execute
result = graph.invoke({
"customer_message": "Where is my order?"
})
print(result)Output:
{
'customer_message': 'Where is my order?',
'intent': 'order_tracking',
'response': 'We are checking your request: Where is my order?'
}Why use TypedDict?
- Defines the structure of graph state clearly.
- Specifies the expected data types for every field.
- Makes code easier to understand across teams.
- Provides better editor autocomplete and static type checking.
- Helps maintain larger LangGraph applications cleanly.
Note:
TypedDictprovides static type information; it does not perform runtime validation automatically.
Key Takeaways
By completing this tutorial, you have learned:
- 1What LangGraph is and why it is useful.
- 2Differences between LangChain and LangGraph.
- 3How graph-based workflows work.
- 4The concepts of nodes, edges, and state.
- 5How to install and set up LangGraph.
- 6How to create and compile a
StateGraph. - 7How to define structured state using
TypedDict.
Next step: build multi-node graphs, connect them with conditional edges, and implement dynamic routing inside an AI Customer Support Agent.
Enjoyed this engineering playbook?
Explore more deep-dive guides on production AI architecture.
