本教程展示如何使用渐进式披露——一种智能体按需加载信息而非预先加载的上下文管理技术——来实现技能(基于提示的专业指令)。智能体通过工具调用加载技能,而非动态更改系统提示,仅发现和加载每个任务所需的技能。 **使用场景:**想象构建一个智能体来帮助在大型企业中跨不同业务垂直领域编写 SQL 查询。你的组织可能为每个垂直领域有单独的数据存储,或者有一个包含数千张表的单体数据库。无论哪种情况,预先加载所有 schema 都会淹没上下文窗口。渐进式披露通过仅在需要时加载相关 schema 来解决这个问题。这种架构还使不同的产品负责人和利益相关者能够独立贡献和维护其特定业务垂直领域的技能。 **你将构建什么:**一个具有两个技能(销售分析和库存管理)的 SQL 查询助手。智能体在其系统提示中看到轻量级的技能描述,然后仅在与用户查询相关时通过工具调用加载完整的数据库 schema 和业务逻辑。Documentation Index
Fetch the complete documentation index at: https://nvd-54.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
For a complete example of a SQL agent with query execution, error correction, and validation, see our SQL Agent tutorial. This tutorial focuses on the progressive disclosure pattern which can be applied to any domain.
工作原理
Here’s the flow when a user asks for a SQL query: Why progressive disclosure:- Reduces context usage - load only the 2-3 skills needed for a task, not all available skills
- Enables team autonomy - different teams can develop specialized skills independently (similar to other multi-agent architectures)
- Scales efficiently - add dozens or hundreds of skills without overwhelming context
- Simplifies conversation history - single agent with one conversation thread
- Latency: Loading skills on-demand requires additional tool calls, which adds latency to the first request that needs each skill
- Workflow control: Basic implementations rely on prompting to guide skill usage - you cannot enforce hard constraints like “always try skill A before skill B” without custom logic
设置
安装
This tutorial requires thelangchain package:
LangSmith
Set up LangSmith to inspect what is happening inside your agent. Then set the following environment variables:选择 LLM
Select a chat model from LangChain’s suite of integrations:- OpenAI
- Anthropic
- Azure
- Google Gemini
- Bedrock Converse
1. 定义技能
First, define the structure for skills. Each skill has a name, a brief description (shown in the system prompt), and full content (loaded on-demand):View complete skill definitions
View complete skill definitions
2. 创建技能加载工具
Create a tool to load full skill content on-demand:load_skill tool returns the full skill content as a string, which becomes part of the conversation as a ToolMessage. For more details on creating and using tools, see the Tools guide.
3. 构建技能中间件
Create custom middleware that injects skill descriptions into the system prompt. This middleware makes skills discoverable without loading their full content upfront.This guide demonstrates creating custom middleware. For a comprehensive guide on middleware concepts and patterns, see the custom middleware documentation.
load_skill tool is registered as a class variable, making it available to the agent.
Production consideration: This tutorial loads the skill list in
__init__ for simplicity. In a production system, you may want to load skills in the before_agent hook instead, allowing them to be refreshed periodically to reflect up-to-date changes (e.g., when new skills are added or existing ones are modified). See the before_agent hook documentation for details.4. 创建带技能支持的智能体
Now create the agent with the skill middleware and a checkpointer for state persistence:load_skill to retrieve full skill content when needed. The checkpointer maintains conversation history across turns.
5. 测试渐进式披露
Test the agent with a question that requires skill-specific knowledge:load_skill("sales_analytics") to get the full schema and business logic, and then used that information to write a correct query following the database conventions.
6. 进阶:使用自定义状态添加约束
Optional: Track loaded skills and enforce tool constraints
Optional: Track loaded skills and enforce tool constraints
You can add constraints to enforce that certain tools are only available after specific skills have been loaded. This requires tracking which skills have been loaded in custom agent state.Create the agent with the middleware that registers the constrained tool:Now if the agent tries to use
定义自定义状态
First, extend the agent state to track loaded skills:更新 load_skill 以修改状态
Modify theload_skill tool to update state when a skill is loaded:创建受约束的工具
Create a tool that’s only usable after a specific skill has been loaded:更新中间件和智能体
Update the middleware to use the custom state schema:write_sql_query before loading the required skill, it will receive an error message prompting it to load the appropriate skill (e.g., sales_analytics or inventory_management) first. This ensures the agent has the necessary schema knowledge before attempting to validate queries.完整示例
View complete runnable script
View complete runnable script
Here’s a complete, runnable implementation combining all the pieces from this tutorial:This complete example includes:
- Skill definitions with full database schemas
- The
load_skilltool for on-demand loading SkillMiddlewarethat injects skill descriptions into the system prompt- Agent creation with middleware and checkpointer
- Example usage showing how the agent loads skills and writes SQL queries
- Install required packages:
pip install langchain langchain-openai langgraph - Set your API key (e.g.,
export OPENAI_API_KEY=...) - Replace the model initialization with your preferred LLM provider
实现变体
View implementation options and trade-offs
View implementation options and trade-offs
This tutorial implemented skills as in-memory Python dictionaries loaded through tool calls. However, there are several ways to implement progressive disclosure with skills:Storage backends:
- In-memory (this tutorial): Skills defined as Python data structures, fast access, no I/O overhead
- File system (Claude Code approach): Skills as directories with files, discovered via file operations like
read_file - Remote storage: Skills in S3, databases, Notion, or APIs, fetched on-demand
- System prompt listing: Skill descriptions in system prompt (used in this tutorial)
- File-based: Discover skills by scanning directories (Claude Code approach)
- Registry-based: Query a skill registry service or API for available skills
- Dynamic lookup: List available skills via a tool call
- Single load: Load entire skill content in one tool call (used in this tutorial)
- Paginated: Load skill content in multiple pages/chunks for large skills
- Search-based: Search within a specific skill’s content for relevant sections (e.g., using grep/read operations on skill files)
- Hierarchical: Load skill overview first, then drill into specific subsections
- Small skills (< 1K tokens / ~750 words): Can be included directly in system prompt and cached with prompt caching for cost savings and faster responses
- Medium skills (1-10K tokens / ~750-7.5K words): Benefit from on-demand loading to avoid context overhead (this tutorial)
- Large skills (> 10K tokens / ~7.5K words, or > 5-10% of context window): Should use progressive disclosure techniques like pagination, search-based loading, or hierarchical exploration to avoid consuming excessive context
渐进式披露与上下文工程
Combining with few-shot prompting and other techniques
Combining with few-shot prompting and other techniques
Progressive disclosure is fundamentally a context engineering technique - you’re managing what information is available to the agent and when. This tutorial focused on loading database schemas, but the same principles apply to other types of context.
与少样本提示结合
For the SQL query use case, you could extend progressive disclosure to dynamically load few-shot examples that match the user’s query:Example approach:- User asks: “Find customers who haven’t ordered in 6 months”
- Agent loads
sales_analyticsschema (as shown in this tutorial) - Agent also loads 2-3 relevant example queries (via semantic search or tag-based lookup):
- Query for finding inactive customers
- Query with date-based filtering
- Query joining customers and orders tables
- Agent writes query using both schema knowledge AND example patterns
后续步骤
- Learn about middleware for more dynamic agent behaviors
- Explore context engineering techniques for managing agent context
- Explore the handoffs pattern for sequential workflows
- Read the subagents pattern for parallel task routing
- See multi-agent patterns for other approaches to specialized agents
- Use LangSmith to debug and monitor skill loading
将这些文档连接到 Claude、VSCode 等,通过 MCP 获取实时答案。

