跳到主要内容

文档索引

在以下地址获取完整的文档索引:https://docs.langchain.org.cn/llms.txt

在进一步探索之前,请使用此文件发现所有可用页面。

技能是可重用的代理能力,提供专门的工作流程和领域知识。 您可以使用 Agent Skills 为您的深度代理提供新的能力和专业知识。有关旨在提高代理在 LangChain 生态系统任务中表现的现成技能,请参阅 LangChain Skills 仓库。 深度代理技能遵循 Agent Skills 规范

什么是技能 (Skills)

技能是一个包含文件夹的目录,每个文件夹中包含一个或多个文件,这些文件提供了代理可以使用的上下文
  • 一个包含技能说明和元数据的 SKILL.md 文件
  • 额外的脚本(可选)
  • 额外的参考信息,例如文档(可选)
  • 额外的资产,例如模板和其他资源(可选)
任何额外的资产(脚本、文档、模板或其他资源)必须在 SKILL.md 文件中引用,并附上有关该文件内容及其使用方法的信息,以便代理能够决定何时使用它们。

技能如何工作

创建深度代理时,您可以传入包含技能的目录列表。当代理启动时,它会读取每个 SKILL.md 文件的 frontmatter(元数据)。 当代理收到提示词时,它会检查在满足该提示词时是否可以使用任何技能。如果找到了匹配的提示词,它会随后查看剩余的技能文件。这种仅在需要时才查看技能信息的模式称为“渐进式披露”。

示例

您可能拥有一个包含某种方式使用文档网站的技能的技能文件夹,以及另一个用于搜索 arXiv 预印本研究论文库的技能
    skills/
    ├── langgraph-docs
    │   └── SKILL.md
    └── arxiv_search
        ├── SKILL.md
        └── arxiv_search.py # code for searching arXiv
SKILL.md 文件始终遵循相同的模式:首先是 frontmatter 中的元数据,随后是该技能的指令。 以下示例展示了一个技能,它提供了关于如何在被提示时提供相关 LangGraph 文档的说明:
---
name: langgraph-docs
description: Use this skill for requests related to LangGraph in order to fetch relevant documentation to provide accurate, up-to-date guidance.
---

# langgraph-docs

## Overview

This skill explains how to access LangGraph Python documentation to help answer questions and guide implementation.

## Instructions

### 1. Fetch the Documentation Index

Use the fetch_url tool to read the following URL:
https://docs.langchain.org.cn/llms.txt

This provides a structured list of all available documentation with descriptions.

### 2. Select Relevant Documentation

Based on the question, identify 2-4 most relevant documentation URLs from the index. Prioritize:

- Specific how-to guides for implementation questions
- Core concept pages for understanding questions
- Tutorials for end-to-end examples
- Reference docs for API details

### 3. Fetch Selected Documentation

Use the fetch_url tool to read the selected documentation URLs.

### 4. Provide Accurate Guidance

After reading the documentation, complete the user's request.
有关更多示例技能,请参阅 Deep Agents 示例技能
重要提示有关编写技能文件时的约束和最佳实践,请参阅完整的 Agent Skills 规范。特别注意:
  • 如果 description 字段超过 1024 个字符,它将被截断。
  • 在 Deep Agents 中,SKILL.md 文件必须小于 10 MB。超过此限制的文件在加载技能时将被跳过。

完整示例

以下示例展示了使用所有可用 frontmatter 字段的 SKILL.md 文件
---
name: langgraph-docs
description: Use this skill for requests related to LangGraph in order to fetch relevant documentation to provide accurate, up-to-date guidance.
license: MIT
compatibility: Requires internet access for fetching documentation URLs
metadata:
  author: langchain
  version: "1.0"
allowed-tools: fetch_url
---

# langgraph-docs

## Overview

This skill explains how to access LangGraph Python documentation to help answer questions and guide implementation.

## Instructions

### 1. Fetch the documentation index

Use the fetch_url tool to read the following URL:
https://docs.langchain.org.cn/llms.txt

This provides a structured list of all available documentation with descriptions.

### 2. Select relevant documentation

Based on the question, identify 2-4 most relevant documentation URLs from the index. Prioritize:

- Specific how-to guides for implementation questions
- Core concept pages for understanding questions
- Tutorials for end-to-end examples
- Reference docs for API details

### 3. Fetch selected documentation

Use the fetch_url tool to read the selected documentation URLs.

### 4. Provide accurate guidance

After reading the documentation, complete the user's request.

用法

创建深度代理时传入技能目录
from urllib.request import urlopen
from deepagents import create_deep_agent
from deepagents.backends.utils import create_file_data
from langgraph.checkpoint.memory import MemorySaver

checkpointer = MemorySaver()

skill_url = "https://raw.githubusercontent.com/langchain-ai/deepagents/refs/heads/main/libs/cli/examples/skills/langgraph-docs/SKILL.md"
with urlopen(skill_url) as response:
    skill_content = response.read().decode('utf-8')

skills_files = {
    "/skills/langgraph-docs/SKILL.md": create_file_data(skill_content)
}

agent = create_deep_agent(
    model="google_genai:gemini-3.1-pro-preview",
    skills=["/skills/"],
    checkpointer=checkpointer,
)

result = agent.invoke(
    {
        "messages": [
            {
                "role": "user",
                "content": "What is langgraph?",
            }
        ],
        # Seed the default StateBackend's in-state filesystem (virtual paths must start with "/").
        "files": skills_files
    },
    config={"configurable": {"thread_id": "12345"}},
)
skills
list[str]
技能源路径列表。路径必须使用正斜杠指定,并且是相对于后端根目录的相对路径。
  • 如果省略,则不会加载任何技能。
  • 使用 StateBackend(默认)时,请通过 invoke(files={...}) 提供技能文件。使用 deepagents.backends.utils 中的 create_file_data() 来格式化文件内容;不支持原始字符串。
  • 对于 FilesystemBackend,技能是相对于后端 root_dir 从磁盘加载的。
对于同名技能,后面的源会覆盖前面的源(后加载的生效)。
SDK 仅加载您在 skills 中传入的源。它不会自动扫描 CLI 目录(例如 ~/.deepagents/...~/.agents/...)。有关 CLI 存储约定,请参阅 应用数据
如果您想在 SDK 代码中使用 CLI 风格的分层,请按优先级从低到高的顺序显式传入所有所需的源
[
"<user-home>/.deepagents/{agent}/skills/",
"<user-home>/.agents/skills/",
"<project-root>/.deepagents/skills/",
"<project-root>/.agents/skills/",
]
然后在创建代理时将该有序列表作为 skills 传入。

源优先级

当多个技能源包含同名技能时,在 skills 数组中靠后列出的源中的技能将具有优先权(后加载的生效)。这允许您对来自不同来源的技能进行分层。
# If both sources contain a skill named "web-search",
# the one from "/skills/project/" wins (loaded last).
agent = create_deep_agent(
    model="google_genai:gemini-3.1-pro-preview",
    skills=["/skills/user/", "/skills/project/"],
    ...
)

子代理的技能

当您使用 子代理 时,您可以配置每种类型的子代理可以访问哪些技能
  • 通用子代理:当您将 skills 传递给 create_deep_agent 时,自动继承主代理的技能。无需额外配置。
  • 自定义子代理:不会继承主代理的技能。需要在每个子代理定义中添加一个 skills 参数,并附上该子代理的技能源路径。
技能状态是完全隔离的:主代理的技能对子代理不可见,子代理的技能对主代理也不可见。
from deepagents import create_deep_agent

research_subagent = {
    "name": "researcher",
    "description": "Research assistant with specialized skills",
    "system_prompt": "You are a researcher.",
    "tools": [web_search],
    "skills": ["/skills/research/", "/skills/web-search/"],  # Subagent-specific skills
}

agent = create_deep_agent(
    model="google_genai:gemini-3.1-pro-preview",
    skills=["/skills/main/"],  # Main agent and GP subagent get these
    subagents=[research_subagent],  # Researcher gets only its own skills
)
有关子代理配置和技能继承的更多信息,请参阅 子代理

代理所见

当配置了技能时,系统会向代理的系统提示词中注入一个“技能系统”部分。代理利用此信息遵循三个步骤的过程
  1. 匹配 (Match)—当用户提示到达时,代理检查是否有任何技能的描述与任务匹配。
  2. 读取 (Read)—如果某个技能适用,代理会使用其技能列表中显示的路径读取完整的 SKILL.md 文件。
  3. 执行 (Execute)—代理遵循技能的指令,并根据需要访问任何支持文件(脚本、模板、参考文档)。
在您的 SKILL.md 的 frontmatter 中编写清晰、具体的描述。代理仅根据描述来决定是否使用某项技能——详细的描述会带来更好的技能匹配效果。

在沙盒中执行技能脚本

技能可以包含与 SKILL.md 文件并存的脚本,例如执行搜索或数据转换的 Python 文件。代理可以从任何后端读取这些脚本,但为了执行它们,代理需要访问 shell——只有 沙盒后端 提供此功能。 当您使用 CompositeBackend 将技能路由到 StoreBackend 进行持久化,同时使用沙盒作为默认后端时,技能文件位于存储中而非代码运行的沙盒中。为了使沙盒能够使用这些脚本,您必须使用 自定义中间件,在代理启动前将技能脚本上传到沙盒中:
import asyncio
from pathlib import Path
from typing import Any

from daytona import Daytona
from deepagents import create_deep_agent
from deepagents.backends import CompositeBackend, StoreBackend
from deepagents.backends.utils import create_file_data
from langchain.agents.middleware import AgentMiddleware, AgentState

from langchain_daytona import DaytonaSandbox
from langgraph.runtime import Runtime
from langgraph.store.memory import InMemoryStore

# Identical skill bundles for every user: one shared store namespace.
SKILLS_SHARED_NAMESPACE = ("skills", "builtin")


class SkillSandboxSyncMiddleware(AgentMiddleware[AgentState, Any, Any]):
    """Copy shared skill files from the store into the sandbox before each agent run."""

    def __init__(self, backend: CompositeBackend) -> None:
        super().__init__()
        self.backend = backend

    async def abefore_agent(self, state: AgentState, runtime: Runtime[Any]) -> None:
        store = runtime.store

        files: list[tuple[str, bytes]] = []
        for item in await store.asearch(SKILLS_SHARED_NAMESPACE):
            key = str(item.key)
            if ".." in key or any(c in key for c in ("*", "?")):
                msg = f"Invalid key: {key}"
                raise ValueError(msg)
            normalized = key if key.startswith("/") else f"/{key}"
            # CompositeBackend routes paths and batches uploads to the right backend.
            files.append((f"/skills{normalized}", item.value["content"].encode()))

        if files:
            await self.backend.aupload_files(files)


async def seed_skill_store(store: InMemoryStore) -> None:
    """Load canonical skill files from disk into the shared store namespace (run once at deploy).
    You can retrieve skills from any source (local filesystem, remote URL, etc.).
    """
    skills_dir = Path(__file__).resolve().parent / "skills"
    for file_path in sorted(p for p in skills_dir.rglob("*") if p.is_file()):
        rel = file_path.relative_to(skills_dir).as_posix()
        key = f"/{rel}"
        await store.aput(
            SKILLS_SHARED_NAMESPACE,
            key,
            create_file_data(file_path.read_text(encoding="utf-8")),
        )


async def main() -> None:
    store = InMemoryStore()
    await seed_skill_store(store)

    daytona = Daytona()
    sandbox = daytona.create()
    sandbox_backend = DaytonaSandbox(sandbox=sandbox)

    backend = CompositeBackend(
        default=sandbox_backend,
        routes={
            "/skills/": StoreBackend(
                store=store,
                namespace=lambda _rt: SKILLS_SHARED_NAMESPACE,
            ),
        },
    )

    try:
        agent = create_deep_agent(
            model="google_genai:gemini-3.1-pro-preview",
            backend=backend,
            skills=["/skills/"],
            store=store,
            middleware=[SkillSandboxSyncMiddleware(backend)],
        )

    finally:
        sandbox.stop()


if __name__ == "__main__":
    asyncio.run(main())
中间件的 before_agent 钩子会在每次代理调用前运行,从该共享命名空间读取技能文件并将其上传到沙盒文件系统中。一旦同步完成,代理就可以像沙盒中的任何其他文件一样,使用 execute 工具执行脚本。 有关同时进行 记忆 (memories) 双向同步的更完整示例,请参阅 使用自定义中间件同步技能和记忆

技能与记忆 (Memory) 的对比

技能和 记忆 (memory) (AGENTS.md 文件) 具有不同的用途
技能内存
目的通过渐进式披露发现的按需能力始终在启动时加载的持久上下文
加载方式仅在代理确定相关性时读取始终注入到系统提示词中
格式命名目录中的 SKILL.mdAGENTS.md 文件
分层用户 → 项目(后者生效)用户 → 项目(组合)
使用场景指令是任务特定的且可能篇幅较大上下文始终相关(项目约定、偏好)

何时使用技能与工具

以下是关于使用工具和技能的一些通用指南
  • 当有大量上下文时,请使用技能以减少系统提示词中的 token 数量。
  • 使用技能将能力捆绑成更大的操作,并提供超出单一工具描述之外的额外上下文。
  • 如果代理无法访问文件系统,请使用工具。

© . This site is unofficial and not affiliated with LangChain, Inc.