Course Content
LangGraph Agents
7 sections · 49 lessons
What is the Graph API in LangGraph, and how do you build a basic graph end-to-end?
What you need to know
Python
1from typing import Annotated, TypedDict2from langchain.chat_models import init_chat_model3from langgraph.graph import StateGraph, START, END4from langgraph.graph.message import add_messages5from langgraph.checkpoint.memory import InMemorySaver67class State(TypedDict):8 messages: Annotated[list, add_messages]910llm = init_chat_model("anthropic:claude-sonnet-4-5")1112def chatbot(state: State) -> dict:13 return {"messages": [llm.invoke(state["messages"])]}1415builder = StateGraph(State) # 1. schema16builder.add_node("chatbot", chatbot) # 2. nodes17builder.add_edge(START, "chatbot") # 3. edges18builder.add_edge("chatbot", END)19graph = builder.compile(checkpointer=InMemorySaver()) # 4. compile2021config = {"configurable": {"thread_id": "demo-1"}}22graph.invoke({"messages": [{"role": "user", "content": "Hi, I'm Asha"}]}, config)23out = graph.invoke({"messages": [{"role": "user", "content": "What's my name?"}]}, config)24print(out["messages"][-1].content)It needs pip install langgraph langchain langchain-anthropic and an API key. init_chat_model accepts any "provider:model" string.
What each step does
- Schema —
Statehas one key,messages, with theadd_messagesreducer so new messages are appended. - Node —
chatbotreturns only the new message; the reducer adds it to the history. - Edges —
STARTtochatbottoEND. - Compile — checks the wiring and attaches the
InMemorySavercheckpointer. - Run — the same
thread_idon the second call means the graph loads the first turn, so the model knows the name "Asha".
Graph API or Functional API
| Graph API | Functional API |
|---|---|
| Explicit nodes and edges | @entrypoint function with @task calls |
| Drawable diagram, clear routing | Plain if and for in Python |
| Best for branching, multi-agent, reviewable flows | Best for adding persistence to existing code |
Both share checkpointers, interrupts and streaming.
A real-life example
A bank's internal team wants a chatbot that answers questions about its leave policy and remembers the conversation. Version one is exactly the graph above plus a retrieve node before chatbot. It took an afternoon. Because it used the Graph API from the start, adding an "escalate to HR" branch a month later was one conditional edge and one new node, not a rewrite of a while loop.
Follow-up questions to expect
- "What does
compile()check?" — That there is an edge fromSTARTand that every edge points to a node that exists. It does not reject a node that nothing points to, so unreachable nodes need a test or a look at the diagram. - "What do you get back from
compile()?" — ACompiledStateGraph. It supportsinvoke,stream,batch,ainvoke,astream,get_stateandget_state_history. - "Do you need a checkpointer?" — Only for memory across calls, human-in-the-loop or resume. A one-shot workflow can compile without one.