Introduction
Building an agent from scratch is great to understand the concept, but production needs a framework. LangGraph (by the LangChain team) is the most powerful framework currently available for stateful, graph-based agent workflows.
1. LangChain vs LangGraph
| LangChain | LangGraph | |
|---|---|---|
| Paradigm | Chains (linear) | Graphs (cyclic) |
| State | Stateless | Stateful |
| Best for | Simple pipelines, RAG | Complex agents, multi-step |
| Control flow | Sequential | Conditional routing |
2. LangGraph Fundamentals
from langgraph.graph import StateGraph, MessagesState, START, END
def call_llm(state: MessagesState):
response = model.invoke(state["messages"])
return {"messages": [response]}
def use_tools(state: MessagesState):
# Execute tool calls
...
# Build graph
graph = StateGraph(MessagesState)
graph.add_node("llm", call_llm)
graph.add_node("tools", use_tools)
graph.add_edge(START, "llm")
graph.add_conditional_edges("llm", should_use_tools, {"yes": "tools", "no": END})
graph.add_edge("tools", "llm")
agent = graph.compile()
Summary
- LangGraph = state machine for agent workflows
- Nodes = functions, Edges = transitions, State = shared data
- Conditional routing for complex decision trees
- Human-in-the-loop built-in support
- Persistence for long-running workflows
Exercises
- Implement research agent with LangGraph
- Add human-in-the-loop approval step
- Implement retry logic when tool fails
- Build an agent with 3+ conditional branches