# LangGraph — Build Stateful AI Agent Workflows > Framework for building stateful, multi-step AI agent workflows as graphs. LangGraph enables cycles, branching, human-in-the-loop, and persistent state for complex agent systems. ## Install Copy the content below into your project: ## Quick Use ```bash pip install langgraph ``` ```python from langgraph.graph import StateGraph, START, END from typing import TypedDict class State(TypedDict): messages: list next_step: str def research(state: State) -> State: # Agent researches the topic return {"messages": state["messages"] + ["Research complete"], "next_step": "write"} def write(state: State) -> State: # Agent writes based on research return {"messages": state["messages"] + ["Draft written"], "next_step": "review"} def review(state: State) -> State: # Agent reviews the draft return {"messages": state["messages"] + ["Review done"], "next_step": "end"} # Build graph graph = StateGraph(State) graph.add_node("research", research) graph.add_node("write", write) graph.add_node("review", review) graph.add_edge(START, "research") graph.add_edge("research", "write") graph.add_edge("write", "review") graph.add_edge("review", END) app = graph.compile() result = app.invoke({"messages": ["Write about AI agents"], "next_step": "research"}) ``` ## What is LangGraph? LangGraph is a framework by LangChain for building stateful, multi-step AI agent workflows as directed graphs. Unlike simple chains, LangGraph supports cycles (agent loops), conditional branching, human-in-the-loop approval, and persistent state across steps. It is the go-to framework for complex agent orchestration that goes beyond linear pipelines. **Answer-Ready**: LangGraph builds stateful AI agent workflows as graphs. Supports cycles, branching, human-in-the-loop, and persistent state. By LangChain team. Used for multi-step agents, tool-calling loops, and approval workflows. LangGraph Cloud for managed deployment. 8k+ GitHub stars. **Best for**: Teams building complex multi-step agent workflows. **Works with**: OpenAI, Claude, any LLM via LangChain. **Setup time**: Under 5 minutes. ## Core Concepts ### 1. State ```python class AgentState(TypedDict): messages: list # Conversation history context: str # Retrieved context plan: list # Agent's plan iteration: int # Loop counter ``` ### 2. Nodes (Steps) Each node is a function that transforms state: ```python def analyze(state: AgentState) -> AgentState: # LLM call, tool use, or pure logic return {"messages": state["messages"] + [analysis]} ``` ### 3. Edges (Transitions) ```python # Unconditional graph.add_edge("research", "write") # Conditional branching def should_continue(state): if state["iteration"] > 3: return "end" return "retry" graph.add_conditional_edges("check", should_continue, {"retry": "research", "end": END}) ``` ### 4. Cycles (Agent Loops) ```python # Agent keeps iterating until satisfied graph.add_edge("act", "observe") graph.add_conditional_edges("observe", check_done, {"continue": "act", "done": END}) ``` ### 5. Human-in-the-Loop ```python from langgraph.checkpoint.memory import MemorySaver checkpointer = MemorySaver() app = graph.compile(checkpointer=checkpointer, interrupt_before=["deploy"]) # Runs until "deploy" node, then pauses for human approval result = app.invoke(state, config={"configurable": {"thread_id": "1"}}) # Human reviews... app.invoke(None, config={"configurable": {"thread_id": "1"}}) # Resume ``` ## Common Patterns | Pattern | Graph Shape | |---------|------------| | ReAct Agent | think → act → observe → (loop) | | Research + Write | research → outline → write → review | | Approval Workflow | draft → review → [approve/reject] → publish | | Multi-Agent | coordinator → [agent_a, agent_b] → merge | | Retry with Feedback | execute → validate → [pass/fail → retry] | ## LangGraph vs Alternatives | Feature | LangGraph | CrewAI | AutoGen | |---------|-----------|--------|---------| | Graph-based | Yes | No (sequential/parallel) | No | | Cycles | Yes | Limited | Yes | | Human-in-loop | Built-in | No | Yes | | State persistence | Built-in | No | Limited | | Managed cloud | LangGraph Cloud | No | No | ## FAQ **Q: Do I need LangChain to use LangGraph?** A: No, LangGraph is a standalone library. It integrates well with LangChain but doesn't require it. **Q: Can I use Claude with LangGraph?** A: Yes, use `ChatAnthropic` from langchain-anthropic as the LLM in your nodes. **Q: What is LangGraph Cloud?** A: Managed hosting for LangGraph applications with API endpoints, monitoring, and auto-scaling. ## Source & Thanks > Created by [LangChain](https://github.com/langchain-ai). Licensed under MIT. > > [langchain-ai/langgraph](https://github.com/langchain-ai/langgraph) — 8k+ stars ## 快速使用 ```bash pip install langgraph ``` 用图定义有状态的多步 AI Agent 工作流。 ## 什么是 LangGraph? LangChain 出品的有状态 Agent 工作流框架。支持循环、分支、人工审批和持久状态,超越线性管线。 **一句话总结**:有状态 Agent 工作流图框架,支持循环/分支/人工审批/持久状态,LangChain 出品,LangGraph Cloud 托管,8k+ stars。 **适合人群**:构建复杂多步 Agent 工作流的团队。 ## 核心概念 1. State — 贯穿全图的状态对象 2. Nodes — 转换状态的步骤函数 3. Edges — 有条件/无条件转移 4. Cycles — Agent 循环直到满足条件 5. 人工审批 — 中断等待人类决策 ## 常见问题 **Q: 需要 LangChain?** A: 不需要,独立库,但集成良好。 ## 来源与致谢 > [langchain-ai/langgraph](https://github.com/langchain-ai/langgraph) — 8k+ stars, MIT --- Source: https://tokrepo.com/en/workflows/b985ba42-4ba9-452d-93f1-9efa434df107 Author: Agent Toolkit