LangGraph智能体开发
langgraph-agents
LangGraph 有状态 AI 智能体,采用基于图的工作流。适用于创建带检查点、人机协同(human-in-the-loop)、流式执行或子图组合的状态机智能体。
- License 许可
- MIT
- 最近更新
- 2026-11-15
- Stars
- ★ 60
- Forks
- 6
01 前言 Front Matter
- name
- langgraph-agents
- created
- 1767830400000
- modified
- 1778284800000
- reviewed
- 1777075200000
- description
- LangGraph stateful AI agents with graph-based workflows. Use when creating state-machine agents with checkpoints, human-in-the-loop, streaming execution, or subgraph composition.
- allowed-tools
- Bash(python *), Bash(uv *), Read, Write, Edit, Grep, Glob, TodoWrite
- user-invocable
- false
02 资源文件 27
- 参考文档configs/winui-config.yaml2.5 KB
- 参考文档REFERENCE.md4.6 KB
- 参考文档references/_sections.md1.9 KB
- 参考文档references/avalonia/avalonia-controls-and-compiled-bindings.md1.6 KB
- 参考文档references/avalonia/build-run-and-launch-verification.md2.7 KB
- 参考文档references/avalonia/community-ecosystem-controls-and-helpers.md1.4 KB
- 参考文档references/avalonia/foundation-avalonia-app-structure.md1.4 KB
- 参考文档references/avalonia/foundation-environment-audit-and-remediation.md1.3 KB
- 参考文档references/avalonia/foundation-setup-and-project-selection.md2.0 KB
- 参考文档references/avalonia/foundation-template-first-recovery.md1.7 KB
- 参考文档references/avalonia/platform-targets-and-deployment.md1.9 KB
- 参考文档references/avalonia/shell-navigation-and-windowing.md1.6 KB
- 参考文档references/avalonia/styling-resources-and-theme-variants.md1.5 KB
- 参考文档references/common/accessibility-input-and-localization.md1002 B
- 参考文档references/common/controls-layout-and-adaptive-ui.md1.1 KB
- 参考文档references/common/motion-animations-and-polish.md707 B
- 参考文档references/common/performance-diagnostics-and-responsiveness.md846 B
- 参考文档references/common/styling-theming-and-icons.md932 B
- 参考文档references/common/testing-debugging-and-review-checklists.md3.4 KB
- 参考文档references/winui/build-run-and-launch-verification.md4.9 KB
- 参考文档references/winui/community-toolkit-controls-and-helpers.md2.1 KB
- 参考文档references/winui/foundation-environment-audit-and-remediation.md3.6 KB
- 参考文档references/winui/foundation-setup-and-project-selection.md4.4 KB
- 参考文档references/winui/foundation-template-first-recovery.md3.3 KB
- 参考文档references/winui/foundation-winui-app-structure.md2.4 KB
- 参考文档references/winui/shell-navigation-and-windowing.md3.3 KB
- 参考文档references/winui/windows-app-sdk-lifecycle-notifications-and-deployment.md2.8 KB
03 SKILL.md 正文
LangGraph Agents
When to Use This Skill
| Use this skill when... | Use a sibling skill instead when... |
|---|---|
| Building stateful agents as graphs of nodes/edges with checkpointing | Writing simple LCEL chains without state — use langchain-development |
| Adding human-in-the-loop approval, streaming, or time-travel debugging | Doing basic tool binding without a graph — use langchain-development |
| Composing multi-agent systems as subgraphs | Needing hierarchical planning + file-system context — use deep-agents |
| Wiring graphs into an initialised project | Scaffolding a brand-new project — use langchain-init (/langchain:init) |
Core Expertise
LangGraph is a low-level orchestration framework for stateful agents:
- Graph-based workflow definition (nodes and edges)
- Durable execution with checkpointing
- Human-in-the-loop interactions
- Short-term and long-term memory
- Streaming and time-travel debugging
- LangSmith observability integration
Installation
# Core LangGraph package
npm install @langchain/langgraph
# Required dependencies
npm install @langchain/core
npm install @langchain/openai # or your preferred model provider
# Optional: Checkpointing backends
npm install @langchain/langgraph-checkpoint-sqlite
Graph Fundamentals
State Definition
import { Annotation, StateGraph } from "@langchain/langgraph";
// Define state schema using Annotation
const StateAnnotation = Annotation.Root({
messages: Annotation<BaseMessage[]>({
reducer: (prev, next) => [...prev, ...next],
default: () => [],
}),
currentStep: Annotation<string>({
reducer: (_, next) => next,
default: () => "start",
}),
});
type State = typeof StateAnnotation.State;
Basic Graph
import { StateGraph, START, END } from "@langchain/langgraph";
const graph = new StateGraph(StateAnnotation)
.addNode("agent", agentNode)
.addNode("tools", toolsNode)
.addEdge(START, "agent")
.addConditionalEdges("agent", routeAgent)
.addEdge("tools", "agent")
.compile();
Nodes
// Nodes are async functions that receive and return state
async function agentNode(state: State): Promise<Partial<State>> {
const response = await model.invoke(state.messages);
return {
messages: [response],
};
}
async function toolsNode(state: State): Promise<Partial<State>> {
const lastMessage = state.messages[state.messages.length - 1];
const toolCalls = lastMessage.tool_calls || [];
const results = await Promise.all(
toolCalls.map(tc => tools[tc.name].invoke(tc.args))
);
return {
messages: results.map((r, i) =>
new ToolMessage({ content: r, tool_call_id: toolCalls[i].id })
),
};
}
Conditional Edges
function routeAgent(state: State): string {
const lastMessage = state.messages[state.messages.length - 1];
if (lastMessage.tool_calls?.length) {
return "tools";
}
return END;
}
// Add conditional routing
graph.addConditionalEdges("agent", routeAgent, {
tools: "tools",
[END]: END,
});
Prebuilt Agents
ReAct Agent
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { ChatOpenAI } from "@langchain/openai";
const model = new ChatOpenAI({ model: "gpt-4o" });
const agent = createReactAgent({
llm: model,
tools: [searchTool, calculatorTool],
});
// Run the agent
const result = await agent.invoke({
messages: [{ role: "user", content: "What's the weather in NYC?" }],
});
With System Prompt
const agent = createReactAgent({
llm: model,
tools: [searchTool],
stateModifier: "You are a helpful research assistant.",
});
Agentic Optimizations
| Context | Pattern |
|---|---|
| Quick iteration | Use MemorySaver for development |
| Production | Use SqliteSaver or external DB |
| Debug state | graph.getState(config) |
| Time travel | graph.getStateHistory(config) |
| Trace execution | Enable LANGCHAIN_TRACING_V2 |
| Reduce tokens | Stream updates, not full state |
| Human approval | interruptBefore: ["dangerous_node"] |
Quick Reference
Core Imports
| Import | Package |
|---|---|
StateGraph |
@langchain/langgraph |
Annotation |
@langchain/langgraph |
START, END |
@langchain/langgraph |
MemorySaver |
@langchain/langgraph |
createReactAgent |
@langchain/langgraph/prebuilt |
Graph Methods
| Method | Description |
|---|---|
.addNode(id, fn) |
Add a node |
.addEdge(from, to) |
Add unconditional edge |
.addConditionalEdges(from, fn) |
Add conditional routing |
.compile() |
Build executable graph |
.invoke(input, config) |
Run to completion |
.stream(input, config) |
Stream execution |
.getState(config) |
Get current state |
.updateState(config, update) |
Modify state |
Stream Modes
| Mode | Output |
|---|---|
"values" |
Full state after each step |
"updates" |
Only changed values |
"messages" |
Message chunks for streaming UI |
"debug" |
Detailed execution info |
Config Options
| Option | Description |
|---|---|
thread_id |
Conversation/session ID |
checkpoint_id |
Specific checkpoint to resume |
recursion_limit |
Max graph iterations (default: 25) |
For checkpointing backends, human-in-the-loop interrupts, streaming modes, subgraph composition, long-term memory, and composite graph patterns, see REFERENCE.md.
04 同仓库更多 Skills
session-workflow-capture
Workflow-tool scripts and workflow friction from this session, filed as issues here or in the plugins repo. Use when a session ran Workflow scripts.
kitty-show-image
Show the user an image with a note on what to judge, via a file send or a labelled kitty tab. Use when the user must see an image, or icat fails with no /dev/tty.
kitty-session-persistence
Snapshot and restore kitty terminal sessions on macOS. Use when surviving WindowServer hangs without losing tabs, configuring snapshots, or restoring after a force-reboot.
kitty-remote-control
Safe kitty remote control from an agent — read panes with kitty @ ls/get-text, notify, and never mutate the live terminal unasked. Use when running inside kitty.
docs-reference-check
Dangling doc-reference gate: flag markdown naming repo paths or relative links that no longer exist. Use when docs cite moved/deleted files, or wiring a doc-drift lint.
YouTube 视频上传与编辑
youtube-upload
通过 Data API v3 将视频上传到 YouTube,并在之后编辑其标题、描述、标签或缩略图。适用于发布 YouTube 视频或修改其标题。