07 Oct What is LangGraph? A Beginner’s Introduction
The problem
Imagine you build an AI study helper. A student asks a question, and the app should:
- Decide if the question needs a web search
- Search if needed
- Write an answer
- Check the answer, and retry if it is weak
A simple chain of steps can’t do this, because it needs decisions and loops. Real AI apps work like a flowchart, not a straight line. LangGraph lets you build that flowchart in code.
Definition
LangGraph is a Python library for building AI applications as graphs. You define the steps, and how the app moves between them. It is made by the LangChain team, but you can use it on its own.
Graph basics (no math needed)
Think of a metro map:
- Stations are the nodes, where work happens (call an LLM, run a tool, clean text)
- Tracks are the edges, which decide where to go next
- Passengers’ shared bag is the state, the data every station can read and update
That is the whole idea. The next chapter covers these three in depth.
Why not just write normal Python?
You can, but LangGraph gives you things that are painful to build yourself:
- Loops and branches that stay readable as the app grows
- Memory that saves progress between messages
- Human-in-the-loop, where the app pauses and waits for approval
- Streaming of results as they are produced
- Visualization of your workflow as a diagram
A first taste
Install it:
pip install langgraph
Now a tiny graph with one node:
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
class State(TypedDict):
name: str
def greet(state: State):
return {"name": "Hello, " + state["name"]}
builder = StateGraph(State)
builder.add_node("greet", greet)
builder.add_edge(START, "greet")
builder.add_edge("greet", END)
graph = builder.compile()
print(graph.invoke({"name": "Studyopedia"}))
Output:
{'name': 'Hello, Studyopedia'}
How it works:
- State defines the shared data (one field, name)
- greet is a node: a normal function that reads the state and returns updates
- add_edge(START, “greet”) and add_edge(“greet”, END) draw the path: start, then greet, then end
- compile() turns the blueprint into a runnable app
- invoke() runs it with starting data
No AI yet. That comes in Chapter 8. For now, notice the pattern: define state, add nodes, connect edges, compile, run. Every LangGraph app follows it.
LangGraph vs LangChain
- LangChain: best for simple, linear pipelines. The flow goes step by step, and memory and pauses are limited.
- LangGraph: best for complex, stateful workflows. The flow can branch, loop, and cycle, and memory and pauses are built in.
They work well together, and LangGraph can use LangChain’s models and tools.
Summary
- LangGraph builds AI apps as graphs of nodes, edges, and state
- It shines when your app needs decisions, loops, memory, or human approval
- Every app follows the same five steps: state, nodes, edges, compile, run
Practice
- Change the greet node to return “Welcome to ” + state[“name”]
- Add a second node, shout, that uppercases the name, and connect it after greet
If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.
For Videos, Join Our YouTube Channel:Â Join Now
Read More:
- Generative AI Tutorial
- AI Ethics
- Machine Learning Tutorial
- Deep Learning Tutorial
- Ollama Tutorial
- Retrieval Augmented Generation (RAG) Tutorial
- ChatGPT Tutorial
- Microsoft Copilot Tutorial
No Comments