Giới thiệu
Xây agent từ đầu rất tốt để hiểu concept, nhưng production cần framework. LangGraph (bởi LangChain team) là framework mạnh nhất hiện tại cho 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()
Tóm tắt
- LangGraph = state machine cho agent workflows
- Nodes = functions, Edges = transitions, State = shared data
- Conditional routing cho complex decision trees
- Human-in-the-loop built-in support
- Persistence cho long-running workflows
Bài tập
- Implement research agent với LangGraph
- Thêm human-in-the-loop approval step
- Implement retry logic khi tool fails
- Xây agent với 3+ conditional branches