Lesson 3 / 29
Your First Graph
Build, compile and run a two-node graph.
Define, wire, compile, invoke
The recipe has four steps: define the state schema; write node functions; create a StateGraph(State), add nodes and edges, starting from START and ending at END; compile and invoke with an initial state. Nodes should do one thing, return only what they change, and avoid hidden global state. Giving nodes clear names helps in traces and error messages. You can also call app.get_graph().draw_mermaid() to get a diagram of the flow for documentation.
A two-node graph, run
I ran this offline with langgraph 1.2.12 and langchain-core 1.6.6 in a Python virtual environment. No API key or model is needed because plain Python functions stand in for the model, so the output is repeatable. The graph runs clean then count. The state carries the text and a list of the steps taken, and the final state shows both nodes ran in order.
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
class State(TypedDict):
text: str
steps: list
def clean(state: State):
return {"text": state["text"].strip().lower(), "steps": state["steps"] + ["clean"]}
def count(state: State):
return {"text": f"{state['text']} ({len(state['text'].split())} words)", "steps": state["steps"] + ["count"]}
g = StateGraph(State)
g.add_node("clean", clean)
g.add_node("count", count)
g.add_edge(START, "clean")
g.add_edge("clean", "count")
g.add_edge("count", END)
app = g.compile()
print(app.invoke({"text": " Hello Big WORLD ", "steps": []}))
Output:
{'text': 'hello big world (3 words)', 'steps': ['clean', 'count']}Name nodes for humans
Names like classify, draft_reply and send_reply make traces and error messages readable.
Quick check: What does compile() return?
- A database
- A trained model
- A JSON file
- A runnable app with invoke and stream
Answer
A runnable app with invoke and stream — Compiling validates the graph and produces the executable application.