Chuyển đến nội dung chính

Lesson 9: LangChain & LangGraph — Stateful Agent Workflows

From LangChain chains to LangGraph graphs: nodes, edges, conditional routing, state management. Build research agents with human-in-the-loop approval flow.

🧠 AI & ML — Lesson 8 Lesson 9: LangChain & LangGraph — Stateful Agent Workflows

Build AI Agents: From Zero to Production

Part 4: Agentic Frameworks

xdev.asia

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

LangChainLangGraph
ParadigmChains (linear)Graphs (cyclic)
StateStatelessStateful
Best forSimple pipelines, RAGComplex agents, multi-step
Control flowSequentialConditional 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

  1. Implement research agent with LangGraph
  2. Add human-in-the-loop approval step
  3. Implement retry logic when tool fails
  4. Build an agent with 3+ conditional branches