Getting Started With LangGraph With A Simple Program
What is LangGraph?
LangGraph is a framework built on top of LangChain that allows developers to create AI applications using graph-based workflows. Instead of writing long sequential programs where every step is manually controlled using normal function calls, LangGraph allows you to design applications as connected workflows made up of nodes and edges.
The core idea behind LangGraph is that modern AI systems are rarely simple one-step programs. Real AI applications usually involve multiple stages such as:
- receiving user input
- processing context
- calling tools
- querying databases
- updating memory
- generating responses
- making decisions
- routing tasks conditionally
Managing these workflows using normal procedural code can quickly become difficult and messy. LangGraph solves this problem by organizing the entire application as a graph.
A graph in computer science is simply a collection of connected nodes. LangGraph uses this same concept to model AI workflows.
In simple terms:
- Nodes represent tasks or functions
- Edges represent connections between tasks
- State represents shared data flowing through the workflow
This makes AI workflows easier to visualize, organize, debug, and extend.
Understanding Nodes in LangGraph
A node is one of the most important concepts in LangGraph. A node represents a unit of work inside the workflow. In most cases, a node is simply a Python function that performs some operation.
For example, a node may:
- call an LLM
- summarize text
- classify data
- search the web
- retrieve documents
- update memory
- validate output
- call an external API
Each node receives the current state, performs some processing, and then returns updated state.
A simple node may look conceptually like this:
def process_input(state):
return {
"message": "Hello"
}
Although this looks like a normal Python function, inside LangGraph it becomes part of a larger workflow system. You can think of nodes as individual processing stations inside a factory assembly line where each station performs one specific task before passing data to the next station.
Understanding Edges in LangGraph
Edges define how execution moves between nodes. If nodes represent tasks, then edges represent the path connecting those tasks together.
For example:
START → analyze_input → generate_response → END
This means:
- Start execution
- Run
analyze_input - Then run
generate_response - Finish workflow
Edges control the execution flow of the application.
LangGraph supports:
- sequential execution
- conditional routing
- loops
- branching workflows
- multi-path execution
This flexibility is one of the biggest strengths of LangGraph.
For example, an AI support chatbot might behave differently depending on the user request:
If billing issue → billing node
If technical issue → technical support node
If refund request → refund workflow
Instead of writing deeply nested if-else logic manually, LangGraph allows these flows to be modeled visually and structurally using graph connections.
Understanding State in LangGraph
State is another extremely important concept in LangGraph. State represents the shared data that flows between nodes during execution.
Every node can:
- read state
- modify state
- add new values
- pass updated state forward
This allows multiple nodes to collaborate while sharing information.
For example, suppose an AI assistant workflow contains:
- user input node
- search node
- summarization node
- response generation node
The state may contain values such as:
The state may contain values such as:
{
"user_question": "...",
"search_results": "...",
"summary": "...",
"final_answer": "..."
}
As execution moves through the graph, different nodes continuously update this shared state. This idea is extremely powerful because modern AI systems often require memory and context persistence across multiple steps. Without state management, each node would behave independently and lose previous information.
Why LangGraph is Useful for AI Systems
Traditional software applications often follow a fixed sequence of execution. However, AI systems are usually more dynamic and unpredictable.
For example, an AI agent may need to:
- Analyze a user request
- Decide whether external information is required
- Search the web
- Query a database
- Call tools
- Validate results
- Generate a final response
This is not a simple linear workflow anymore. It becomes a decision-driven execution system. LangGraph is specifically designed for such applications.
It helps developers build systems where:
- execution can branch dynamically
- workflows can loop
- nodes can collaborate
- memory can persist across steps
- tools can be integrated naturally
This makes LangGraph extremely suitable for:
- AI agents
- autonomous systems
- chatbots
- research assistants
- tool-calling workflows
- multi-agent systems
- retrieval-augmented generation (RAG)
- long-running AI processes
LangGraph as a Workflow Engine
A LangGraph application behaves very much like a workflow engine for AI systems.
A traditional workflow engine manages:
- tasks
- execution order
- shared data
- conditional logic
LangGraph does the same thing, but specifically for AI applications. The difference is that LangGraph is deeply integrated with modern LLM ecosystems and AI tooling.
For example, a LangGraph workflow may include:
User Input
↓
LLM Processing
↓
Tool Calling
↓
Memory Update
↓
Final Response
Each step becomes a node inside the graph.
This modular structure makes AI systems easier to:
- scale
- maintain
- debug
- test
- extend
Instead of building one giant AI function containing hundreds of lines of logic, developers can break workflows into smaller reusable components.
Relationship Between LangChain and LangGraph
Many beginners become confused about the relationship between LangChain and LangGraph.
LangChain mainly focuses on:
- LLM integrations
- prompt management
- chains
- memory
- retrieval
- tools
LangGraph extends these ideas further by introducing graph-based execution and workflow orchestration.
You can think of the relationship like this:
LangChain → Building AI Components
LangGraph → Connecting Components Into Intelligent Workflows
LangGraph does not replace LangChain. Instead, it builds on top of it. In many real-world applications, both are used together.
A Simple Program
Step 1 — Create Project Folder
Create a folder:
langgraph-demo
Open it in Visual Studio Code.
Step 2 — Create Virtual Environment
Open terminal inside VS Code:
python -m venv .venv
Activate it. Windows
.venv\Scripts\activate
Step 3 — Install LangGraph
Run:
pip install langgraph langchain
Step 4 — Create Python File
Create:
main.py
Step 5 — Hello World LangGraph Example
Add this code:
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
# Shared state object
class MyState(TypedDict):
message: str
# Node function
def hello_node(state: MyState):
print("Hello from LangGraph!")
return {
"message": "LangGraph executed successfully"
}
# Create graph
graph_builder = StateGraph(MyState)
# Add node
graph_builder.add_node("hello", hello_node)
# Add flow
graph_builder.add_edge(START, "hello")
graph_builder.add_edge("hello", END)
# Compile graph
graph = graph_builder.compile()
# Execute graph
result = graph.invoke({})
print(result)
###Step 6 — Run the Program
Execute:
python main.py
Expected Output
You should see:
Hello from LangGraph!
{'message': 'LangGraph executed successfully'}
Understanding the LangGraph Hello World Code
The first line imports TypedDict, which is used to define the structure of the shared state object.
from typing import TypedDict
Next, we import core LangGraph components:
from langgraph.graph import StateGraph, START, END
Here:
StateGraphcreates the workflow graphSTARTmarks where execution beginsENDmarks where execution finishes
The shared state is defined here:
class MyState(TypedDict):
message: str
This means the graph will carry a shared state containing a string field called message.
Next comes the node function:
def hello_node(state: MyState):
A node is simply a Python function inside the graph.
This node:
print("Hello from LangGraph!")
prints a message and then returns updated state:
return {
"message": "LangGraph executed successfully"
}
The returned dictionary becomes part of the shared graph state.
Next, the graph is created:
graph_builder = StateGraph(MyState)
This initializes a graph using MyState as the shared state structure.
The node is then added to the graph:
graph_builder.add_node("hello", hello_node)
This registers the node with the name "hello".
Next, execution flow is defined:
graph_builder.add_edge(START, "hello")
graph_builder.add_edge("hello", END)
The workflow becomes:
START → hello → END
This means execution starts, runs the hello node, and then finishes.
The graph is then compiled:
graph = graph_builder.compile()
Compilation converts the graph definition into an executable workflow.
Finally, the graph is executed:
result = graph.invoke({})
The empty dictionary represents the initial state.
LangGraph runs the workflow, executes the node, updates the state, and returns the final result.
The final output is printed using:
print(result)
which displays:
{'message': 'LangGraph executed successfully'}