代理代码

人工智能 社区

技能解读

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

何时使用 LCEL 与 createagent

LCEL 适用于非代理流:确定性管道(RAG、汇总、分类、结构化提取)。管道运行一次、端到端、无循环、无工具调用 LLM 驱动控制流。如果任务涉及法学硕士决定调用哪些工具,请不要使用 LCEL - 使用 create_agent(...) + 中间件。试图将代理行为固定到 LCEL 链上是这里最常见的错误。分界线:*LLM 选择接下来发生的事情,还是代码?*如果是 LLM,代理。如果是代码,LCEL。

文档不会警告您的事情

chain.invoke(x),其中x是一个字符串,但链期望字典会在某些配置中静默强制,而在其他配置中失败 - 显式传递字典。 init_chat_model从env(OPENAI_API_KEY,ANTHROPIC_API_KEY)读取提供者信用。不会提示;缺少 env 在第一次调用时引发,而不是在构造时引发。解析器是链的一部分。 chat_model.invoke(...) 返回一个AIMessage;通过 StrOutputParser() 进行管道传输以获取纯字符串。

生产经验法则

始终用.with_retry(stop_after_attempt=3, wait_exponential_jitter=True)包裹生产链,以适应瞬态模型故障。对于提供者冗余,请使用.with_fallbacks([cheaper_model_chain])——如果主链提升,后备链就会运行。后备是一个“完整的链条”,而不仅仅是一个模型。对于类型化输出,请在组成链之前使用model.with_structured_output(PydanticModel)。验证是自动的;你得到的是 Pydantic 实例,而不是字典。

何时达到什么目标

需要 · 工具 LLM + 工具,决定下一步做什么 · create_agent(不是 LCEL)确定性转换(文本 → 结构化) · LCEL 带有向量存储上的 with_structured_output RAG · LCEL 带有检索器 + 问题的 RunnableParallel 带分支的多步管道 · LCEL 带有 RunnableBranch 或升级到 StateGraph(如果分支需要)状态

使用 mcpdoc 获取的文档 URL

https://docs.langchain.com/oss/python/langchain/lcel.md — LCEL 入门 https://docs.langchain.com/oss/python/langchain/structured-output.md — with_structured_output https://docs.langchain.com/oss/python/langchain/runnables.md — 可运行类型和方法 https://docs.langchain.com/oss/python/langchain/retrievers.md — 检索器接口 https://docs.langchain.com/oss/python/langchain/chat-models.md — init_chat_model 和提供者模型名称

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

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

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

# LangChain (LCEL): editorial guidance

For API reference (full Runnable list, parser types, retriever interfaces), use the **`mcpdoc` MCP tools**: `fetch_docs("https://docs.langchain.com/oss/python/langchain/...")`. This skill is the *opinions* layer.

## When to use LCEL vs `create_agent`

LCEL is for **non-agentic flows**: deterministic pipelines (RAG, summarization, classification, structured extraction). The pipeline runs once, end-to-end, no loop, no tool-calling LLM driving control flow.

If the task involves an LLM deciding which tools to call, **don't use LCEL** — use `create_agent(...)` + middleware. Trying to bolt agentic behavior onto an LCEL chain is the most common mistake here. The dividing line: *does the LLM choose what happens next, or does the code?* If LLM, agents. If code, LCEL.

## Things the docs won't warn you about

- `chain.invoke(x)` where `x` is a string but the chain expects a dict will silently coerce in some configurations and fail in others — pass a dict explicitly.
- `init_chat_model` reads provider creds from env (`OPENAI_API_KEY`, `ANTHROPIC_API_KEY`). It will not prompt; missing env raises at first call, not at construction.
- Parsers are part of the chain. `chat_model.invoke(...)` returns an `AIMessage`; pipe through `StrOutputParser()` to get a plain string.
- **Middleware does NOT apply to LCEL chains.** Middleware is `create_agent`-only. For chains, use chain-level `chain.with_retry(...)` and `chain.with_fallbacks([...])` instead.
- `with_structured_output` does **not** stream — it accumulates the full response and returns the validated object. If you need streaming AND typed output, you can't have both via this API.

## Production rules of thumb

- **Always wrap production chains with `.with_retry(stop_after_attempt=3, wait_exponential_jitter=True)`** for resilience to transient model failures.
- **For provider redundancy, use `.with_fallbacks([cheaper_model_chain])`** — fallback chains run if the primary raises. The fallback is a *full chain*, not just a model.
- **For typed output, use `model.with_structured_output(PydanticModel)`** before composing into the chain. Validation is automatic; you get the Pydantic instance, not a dict.
- **For RAG, add a guardrail stage** that returns "I don't know" when `len(context) == 0`. Without it, the LLM hallucinates from empty context.
- **For RAG, consider a reranker between retriever and prompt.** Recall@k improves substantially. The retriever's first 20 results passed through a reranker that picks the top 5 outperforms a retriever that fetches 5 directly.

## When to reach for what

| Need | Tool |
|---|---|
| LLM + tools, deciding what to do next | `create_agent` (NOT LCEL) |
| Deterministic transformation (text → structured) | LCEL with `with_structured_output` |
| RAG over a vector store | LCEL with `RunnableParallel` of retriever + question |
| Multi-step pipeline with branches | LCEL with `RunnableBranch` or upgrade to `StateGraph` if branches need state |
| Streaming token output | LCEL chain (most parsers stream); NOT `with_structured_output` |
| Async at scale | LCEL `.ainvoke` / `.astream` |

## Doc URLs to fetch with mcpdoc

- `https://docs.langchain.com/oss/python/langchain/lcel.md` — LCEL primer
- `https://docs.langchain.com/oss/python/langchain/structured-output.md` — `with_structured_output`
- `https://docs.langchain.com/oss/python/langchain/runnables.md` — Runnable types and methods
- `https://docs.langchain.com/oss/python/langchain/retrievers.md` — retriever interfaces
- `https://docs.langchain.com/oss/python/langchain/chat-models.md` — `init_chat_model` and provider model names

When you need a specific class signature or kwarg, fetch from these. Don't guess at constructor args.

---
> 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 即可参与讨论、点赞、订阅更新。