Skills2026年4月6日·1 分钟阅读

LangGraph — Stateful AI Agent Graphs by LangChain

Framework for building stateful, multi-actor AI agent applications as directed graphs. Supports cycles, branching, persistence, and human-in-the-loop patterns. By LangChain. 8,000+ stars.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 66/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Community
入口
LangGraph — Stateful AI Agent Graphs by LangChain
先审查命令
npx -y tokrepo@latest install dbf0bc9a-0bb1-4efb-a033-3bdd498f6a63 --target codex

先 dry-run,确认写入项后再运行此命令。

TL;DR
LangGraph models AI agents as stateful directed graphs with cycles, branching, and human-in-the-loop patterns.
§01

What it is

LangGraph is a framework by LangChain for building stateful, multi-actor AI agent applications as directed graphs. Instead of linear chains, LangGraph lets agents loop, branch, wait for human input, and checkpoint state automatically.

It targets AI engineers and teams building production agent systems that require retry logic, parallel tool calls, approval gates, or long-running workflows.

§02

How it saves time or tokens

LangGraph eliminates the boilerplate of building state machines for agent orchestration. Its built-in persistence means you do not need to implement checkpointing or crash recovery. The human-in-the-loop primitives (interrupt_before, interrupt_after) replace custom webhook and polling logic. By modeling agents as graphs, you can visualize and debug complex multi-step flows instead of tracing through nested function calls.

§03

How to use

  1. Install LangGraph:
pip install langgraph
  1. Define state and nodes:
from langgraph.graph import StateGraph, END
from typing import TypedDict

class State(TypedDict):
    messages: list
    next_step: str

def researcher(state: State):
    return {'messages': state['messages'] + ['research done']}

def writer(state: State):
    return {'messages': state['messages'] + ['draft complete']}
  1. Build and compile the graph:
graph = StateGraph(State)
graph.add_node('research', researcher)
graph.add_node('write', writer)
graph.add_edge('research', 'write')
graph.add_edge('write', END)
graph.set_entry_point('research')
app = graph.compile()
result = app.invoke({'messages': [], 'next_step': ''})
§04

Example

A ReAct agent with validation loop:

def should_continue(state: State):
    if 'valid' in state['messages'][-1]:
        return 'end'
    return 'retry'

graph = StateGraph(State)
graph.add_node('act', researcher)
graph.add_node('validate', writer)
graph.add_conditional_edges('validate', should_continue, {
    'retry': 'act',
    'end': END
})
graph.add_edge('act', 'validate')
graph.set_entry_point('act')
app = graph.compile()
§05

Related on TokRepo

§06

Common pitfalls

  • Creating infinite loops without a max-iteration guard causes runaway token spend. Always add a counter or timeout to conditional edges.
  • Storing large objects in graph state slows checkpoint serialization. Keep state lean and reference external storage for big payloads.
  • Mixing synchronous blocking calls inside async graph nodes degrades throughput. Use async-native tool implementations.

常见问题

What is the difference between LangGraph and LangChain?+

LangChain provides building blocks like LLM wrappers, tool schemas, and prompt templates. LangGraph is the orchestration layer that connects those blocks into stateful agent graphs with loops, branching, and persistence. LangChain recommends LangGraph for non-trivial agent applications.

Does LangGraph support persistence across restarts?+

Yes. State is automatically checkpointed between nodes. LangGraph supports PostgreSQL, SQLite, and Redis as checkpoint backends. If a node fails, the agent resumes from the last checkpoint.

How does human-in-the-loop work in LangGraph?+

Use interrupt_before or interrupt_after on any node. Execution pauses and returns control to your application. When you provide input (approval, edit, rejection), the graph resumes from that point.

Can LangGraph run without LangChain?+

Technically yes. LangGraph depends only on langchain-core, which is lightweight. You can build graph-based workflows without using LangChain's full LLM abstractions, though most users pair them.

What observability does LangGraph provide?+

When a LANGCHAIN_API_KEY is set, every node execution is traced in LangSmith with token usage, latency, and tool call details. LangSmith offers a free tier for small projects.

引用来源 (3)
🙏

来源与感谢

Created by LangChain. Licensed under MIT.

langgraph — ⭐ 8,000+

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产