LangGraph Agents

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
from typing import Annotated, TypedDictfrom langchain.chat_models import init_chat_modelfrom langgraph.graph import StateGraph, START, ENDfrom langgraph.graph.message import add_messagesfrom langgraph.checkpoint.memory import InMemorySaverclass State(TypedDict):    messages: Annotated[list, add_messages]llm = init_chat_model("anthropic:claude-sonnet-4-5")def chatbot(state: State) -> dict:    return {"messages": [llm.invoke(state["messages"])]}builder = StateGraph(State)                       # 1. schemabuilder.add_node("chatbot", chatbot)              # 2. nodesbuilder.add_edge(START, "chatbot")                # 3. edgesbuilder.add_edge("chatbot", END)graph = builder.compile(checkpointer=InMemorySaver())   # 4. compileconfig = {"configurable": {"thread_id": "demo-1"}}graph.invoke({"messages": [{"role": "user", "content": "Hi, I'm Asha"}]}, config)out = graph.invoke({"messages": [{"role": "user", "content": "What's my name?"}]}, config)print(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

  1. Schema — State has one key, messages, with the add_messages reducer so new messages are appended.
  2. Node — chatbot returns only the new message; the reducer adds it to the history.
  3. Edges — START to chatbot to END.
  4. Compile — checks the wiring and attaches the InMemorySaver checkpointer.
  5. Run — the same thread_id on the second call means the graph loads the first turn, so the model knows the name "Asha".

Graph API or Functional API

Graph APIFunctional API
Explicit nodes and edges@entrypoint function with @task calls
Drawable diagram, clear routingPlain if and for in Python
Best for branching, multi-agent, reviewable flowsBest 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 from START and 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()?" — A CompiledStateGraph. It supports invoke, stream, batch, ainvoke, astream, get_state and get_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.