代理代码

人工智能 社区

技能解读

当前原文的自动摘译,尚未人工审校。

何时下拉至原始 StateGraph

大多数代理不需要这个。首先使用create_agent(...) + 中间件(参见langchain-agents-middleware技巧)。仅在以下情况下才下拉至StateGraph:单个图表中存在多个 LLM 调用,并且它们之间具有自定义路由。并行运行和合并的分支。非消息状态(自定义数据类、字典、流经节点的数据帧)。多图工作流程,其中一个图将另一个图调用为子图。

文档不会警告您的事情

返回{}的节点是一个空操作;返回None以明确地表示“没有状态改变”。如果任何分支终止,add_conditional_edges映射必须包含END——忽略它是一个无声的错误,而不是错误。 compile() 与 bind_tools 不是幂等的 — 重新绑定工具,然后重新编译。 langgraph dev 文件更改时重新加载;如果它停止重新加载,图表可能无法导入 - 检查终端是否有异常。

生产要点(经验法则)

对于任何具有持久状态的代理,始终在 config={"configurable": {"thread_id": ...}} 中传递 thread_id。缺失的thread_id会默默地启动一个新线程。 InMemorySaver 仅供开发者使用。生产 = PostgresSaver(多实例安全)或SqliteSaver(单节点,小批量)。国家随着InMemorySaver的进程而消亡。通过传递 None 作为具有相同 thread_id 的输入来恢复中断的线程。运行时从中断触发的地方开始。

使用 mcpdoc 获取的文档 URL

https://docs.langchain.com/oss/python/langgraph/graph-api.md — StateGraph,节点,边 https://docs.langchain.com/oss/python/langgraph/durable-execution.md — 检查指针 + thread_id https://docs.langchain.com/oss/python/langgraph/streaming.md — stream_mode 模式 https://docs.langchain.com/oss/python/langgraph/persistence.md — Postgres / Sqlite 检查指针 https://docs.langchain.com/oss/python/langgraph/subgraphs.md — 多图组合

流狐档案 运行与权限由流狐静态检查推断,不代表作者声明或实际测试结果。
领域
人工智能
作者声明的智能体
未找到明确声明;不据此推断已兼容或已测试
静态检查
88 / 100 · 启发式扫描,不代表运行安全
运行环境证据
未执行;当前没有隔离运行测试证据
作者 / 版本 / 许可证
@tomevault-io · 未声明许可证
上下文用量评级
低消耗
接入复杂程度
需简单配置
是否需要外部接口密钥
不需要
兼容的系统
macOS · Linux · Windows
运行环境要求
Node.js · Python
检测到的文件与系统行为
  • 只读
  • 命令执行
网络行为
允许外网请求
安装命令数
无(仅作为资料)

需要注意: 未声明工具权限范围;实际权限由运行环境决定。

作者说明文件(原文) SKILL.md

# LangGraph: editorial guidance

For API reference (signatures, imports, exhaustive method lists), use the **`mcpdoc` MCP tools**: `fetch_docs("https://docs.langchain.com/oss/python/langgraph/...")`. This skill is the *opinions* layer; the docs are the *facts* layer.

## When to drop down to raw `StateGraph`

Most agents do NOT need this. Use `create_agent(...)` + middleware first (see the `langchain-agents-middleware` skill). Drop down to `StateGraph` only when:

- Multiple LLM calls in a single graph with custom routing between them.
- Branches that run in parallel and merge.
- Non-message state (custom dataclasses, dicts, dataframes flowing through nodes).
- Multi-graph workflows where one graph calls another as a subgraph.

If your problem fits "one model, some tools, in a loop" — even if the loop is complex — `create_agent` is the right tool. Don't reach for `StateGraph` out of habit.

## Things the docs won't warn you about

- A node returning `{}` is a no-op; return `None` to signal "no state change" cleanly.
- `add_conditional_edges` mappings must include `END` if any branch terminates — leaving it out is a silent bug, not an error.
- `compile()` is not idempotent across `bind_tools` — rebind tools, then re-compile.
- `langgraph dev` reloads on file change; if it stops reloading, the graph likely failed to import — check the terminal for the exception.
- A graph with `interrupt()` calls but no checkpointer will throw at *invoke time*, not at *compile time*. Always pair `interrupt()` with `compile(checkpointer=...)`.

## Production essentials (rules of thumb)

- **Always pass a `thread_id`** in `config={"configurable": {"thread_id": ...}}` for any agent with persistent state. A missing `thread_id` silently starts a fresh thread.
- **`InMemorySaver` is for dev only.** Production = `PostgresSaver` (multi-instance safe) or `SqliteSaver` (single-node, low-volume). State dies with the process for `InMemorySaver`.
- **Resume an interrupted thread by passing `None` as input** with the same `thread_id`. The runtime picks up where the interrupt fired.
- **Custom state schemas need reducers.** `Annotated[list, add_messages]` appends; without the reducer, each node *replaces* the field instead of accumulating.
- **For node-level retries on raw `StateGraph`, use `RetryPolicy`.** For `create_agent` agents, prefer `ToolRetryMiddleware` / `ModelRetryMiddleware` — same effect, cleaner composition.

## Doc URLs to fetch with mcpdoc

- `https://docs.langchain.com/oss/python/langgraph/graph-api.md` — `StateGraph`, nodes, edges
- `https://docs.langchain.com/oss/python/langgraph/durable-execution.md` — checkpointers + `thread_id`
- `https://docs.langchain.com/oss/python/langgraph/streaming.md` — `stream_mode` modes
- `https://docs.langchain.com/oss/python/langgraph/persistence.md` — Postgres / Sqlite checkpointers
- `https://docs.langchain.com/oss/python/langgraph/subgraphs.md` — multi-graph composition

When the user is mid-task and you need a method signature or import path, fetch from these URLs. Don't guess.

---
> Source: [cwijayasundara/agent_cli_langchain](https://github.com/cwijayasundara/agent_cli_langchain) — distributed by [TomeVault](https://tomevault.io).
<!-- tomevault:4.0:skill_md:2026-05-23 -->

讨论

基于 GitHub Discussions。登录 GitHub 即可参与讨论、点赞、订阅更新。