LangGraph Module 1: Introduction and Your First Graph
Introduction
LangGraph is a low-level framework for building stateful, long-running agents and workflows as graphs. Where LangChain's create_agent gives you a ready-made agent loop, LangGraph lets you define the loop yourself: what steps exist, how they connect, and what state they share. This first module of the nine-module LangGraph course builds a tiny graph with no LLM so you can see the core ideas clearly.
Prerequisites
- Python 3.10 or newer
- Basic Python, including functions and type hints
Step-by-Step Guide
Step 1: Learn the three core ideas
State is the data shared by the whole graph. Nodes are Python functions that read the state and return updates to it. Edges decide which node runs next. You define these, compile the graph, and then invoke it with an initial state.
Step 2: Install LangGraph
LangGraph installs on its own and works without LangChain, though the documentation uses LangChain components for models and tools in later modules.
pip install -U langgraphStep 3: Define the state and nodes
State is described with a TypedDict. Each node takes the state and returns a dictionary containing only the keys it wants to change; LangGraph merges that into the state. Nodes should return updates, not modify the state in place.
from typing_extensions import TypedDict
class State(TypedDict):
text: str
def shout(state: State) -> dict:
return {"text": state["text"].upper()}
def add_exclamation(state: State) -> dict:
return {"text": state["text"] + "!"}Step 4: Connect them and compile
Add the nodes to a StateGraph, connect them with edges starting at START and ending at END, then call compile. You must compile a graph before you can use it.
from langgraph.graph import StateGraph, START, END
builder = StateGraph(State)
builder.add_node("shout", shout)
builder.add_node("add_exclamation", add_exclamation)
builder.add_edge(START, "shout")
builder.add_edge("shout", "add_exclamation")
builder.add_edge("add_exclamation", END)
graph = builder.compile()Step 5: Run it and see how it relates to LangChain
invoke takes the initial state and returns the final state. LangChain's create_agent is built on LangGraph and returns a compiled graph, so everything you learn here applies to those agents too, and an agent can even be used as a node inside a larger graph.
print(graph.invoke({"text": "hello"}))
# {'text': 'HELLO!'}FAQs
- When should I use LangGraph instead of create_agent?
- Use create_agent when a standard tool-calling loop fits. Use LangGraph when you need custom control flow: specific steps, branching, loops, human approval points, parallel work, or durable state you manage yourself.
- Does a LangGraph graph have to involve an LLM?
- No. Nodes are ordinary functions, so graphs can orchestrate any mix of code, tools, and models.
Summary
A LangGraph graph is state plus nodes plus edges, compiled and invoked. Nodes return state updates, edges decide the order, and the same machinery powers LangChain agents.
Comments
Log in or create a free account to join the discussion.