AI Hints
Home›LangGraph Tutorial›Getting Started
LangGraph · Lesson 3 of 120

Your First Graph

One node.

⏱ 2 min read▶ Video at 2:05🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

Let's build the smallest useful graph. It takes a customer's name, and writes a welcome message. Just one step. But it shows every part of a LangGraph app.

Your First Graph
From the video: Your First Graph
  • The smallest graph
  • One step
  • Every part

Hello, graph

State describes the data the graph carries, a name and a greeting. Greet is a node, a plain function. It reads the state, and returns an update. The builder adds the node, and two edges, from start to greet, and from greet to the end. Compile turns it into a runnable graph. Run it. The graph returns the final state, with the greeting filled in.

first_graph.pypython
from typing import TypedDict
from langgraph.graph import StateGraph, START, END

class State(TypedDict):
    name: str
    greeting: str

def greet(state: State):
    return {"greeting": f"Welcome to Riverside Books, {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": "Sam"}))
Output
{'name': 'Sam', 'greeting': 'Welcome to Riverside Books, Sam!'}

The four parts

Every LangGraph app has the same four parts. A state, that describes the data. Nodes, that do the work. Edges, that connect them. And a compile step, that turns the builder into a graph you can run.

  1. State
  2. Nodes
  3. Edges
  4. Compile
  5. Invoke
The four parts
From the video: The four parts

Summary

A graph has state, nodes, edges, and a compile step. Invoke runs it, and returns the final state. Next, a closer look at state.

Key takeaways
  • StateGraph(State)
  • add_node, add_edge
  • compile, invoke
Next up: State — the shared notebook