Multi-Agent SystemsPublished Mar 2026•6 min read

Introduction to LangGraph: Building Stateful Graph-Based AI Workflows

Master the foundations of LangGraph — Nodes, Edges, TypedDict state, and your first compiled StateGraph in Python.

Md Mehedi Hasan
Md Mehedi Hasan
Senior AI/LLM Engineer · 5+ Yrs Exp
𝕏in
Interconnected geometric nodes and edges illustrating stateful LangGraph workflows

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:

  1. 1Receives a customer's question.
  2. 2Understands the customer's intent.
  3. 3Checks the order database if necessary.
  4. 4Generates an appropriate response.
  5. 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.

LangChainLangGraph
Build LLM applicationsBuild stateful AI workflows
Connect models and toolsControl execution between steps
Create chains and agentsCreate graph-based agents
Integrate data sourcesManage shared application state
Simplify LLM interactionsSupport 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:

plaintext
┌───────────────────────────┐
│     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:

json
{
  "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:

bash
mkdir langgraph-course
cd langgraph-course

python -m venv venv

Activate the virtual environment on Windows:

bash
venv\Scripts\activate

Or on Linux / macOS:

bash
source venv/bin/activate

Install LangGraph:

bash
pip install -U langgraph

Create a Python entry file:

bash
touch main.py

Your project structure:

plaintext
langgraph-course/
│
├── venv/
└── main.py

6. Creating Your First StateGraph

Let's build a simple AI Customer Support workflow. First, import the required classes:

python
from langgraph.graph import StateGraph, START, END

StateGraph 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:

python
def customer_support(state):
    message = state["customer_message"]
    return {
        "response": f"We received your message: {message}"
    }

Create and compile the graph:

python
# 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:

python
result = graph.invoke({
    "customer_message": "Where is my order?"
})
print(result)

Output:

text
{
    '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:

python
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:

text
{
    '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: TypedDict provides static type information; it does not perform runtime validation automatically.


Key Takeaways

By completing this tutorial, you have learned:

  1. 1What LangGraph is and why it is useful.
  2. 2Differences between LangChain and LangGraph.
  3. 3How graph-based workflows work.
  4. 4The concepts of nodes, edges, and state.
  5. 5How to install and set up LangGraph.
  6. 6How to create and compile a StateGraph.
  7. 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.

Tags:#LangGraph#Agentic AI#StateGraph#Python#LangChain#TypedDict

Enjoyed this engineering playbook?

Explore more deep-dive guides on production AI architecture.

View All 12 Playbooks →
View All →