分析 KB_WORKFLOW_INSTRUCTIONS_RESEARCH 与 deep_research 各层 Prompt 的职责边界, 给出最小侵入性的修改建议与完整集成代码,含逐行 diff 对比。
deep_research 示例中,主 agent 的 system_prompt 由两段组成:
# deep_research 原始主 agent prompt 构成
INSTRUCTIONS = (
RESEARCH_WORKFLOW_INSTRUCTIONS # ① 研究流程编排规范
+ SUBAGENT_DELEGATION_INSTRUCTIONS # ② 并发调度规范
)
如果直接将这两段追加到 KB_WORKFLOW_INSTRUCTIONS_RESEARCH 末尾,会产生以下冲突:
RESEARCH_WORKFLOW_INSTRUCTIONS 又定义了一套六步研究流程,模型会在两套规则间不确定性选择write_todos() 指令,触发条件不一致SUBAGENT_DELEGATION_INSTRUCTIONS 专为多路 web 搜索设计,KB 场景无并发 research 需求RESEARCHER_INSTRUCTIONS 适配为 KB 工具版本KB_WORKFLOW_INSTRUCTIONS_RESEARCH 本身已经是完整的 orchestration 规范。
集成 deep_research 能力 = 定义一个高质量的 research-agent subagent,而不是向主 agent 追加编排规则。
| Prompt 组件 | 来源 | 集成归属 | 处理方式 | 原因 |
|---|---|---|---|---|
KB_WORKFLOW_INSTRUCTIONS_RESEARCH |
原有 | 主 agent system_prompt | 保留,仅 Section F 小幅增补 | 已是完整编排规范,无需合并外部规则 |
RESEARCH_WORKFLOW_INSTRUCTIONS |
deep_research | — | 不使用 | Web 研究编排规范,与 KB 路由规则冲突 |
SUBAGENT_DELEGATION_INSTRUCTIONS |
deep_research | — | 不使用 | 并发 web 搜索调度规范,KB 场景不适用 |
RESEARCHER_INSTRUCTIONS |
deep_research | research-agent subagent | 改写为 KB 工具版本 | 保留方法论,替换工具集,修改输出格式 |
RESEARCH_AGENT_INSTRUCTIONS |
新增 | research-agent subagent | 新建 | KB 工具版研究员 prompt,替代原 RESEARCHER_INSTRUCTIONS |
原始 Section F 已正确指向 task(subagent_type="research-agent"),但缺少:
这是本次集成的核心新增内容。基于 RESEARCHER_INSTRUCTIONS 的方法论,完整改写为适配 KB 工具集的版本。
RESEARCH_AGENT_INSTRUCTIONS = f"""You are a research analyst specializing in
business due-diligence and knowledge-base evidence gathering.
Today's date is {current_date}.
## Role
Your job is to gather comprehensive, citation-backed evidence from the knowledge
base and business context, then return a structured evidence package.
The orchestrator (main agent) will synthesize your output into the final
user-facing report — your role is evidence gathering and structuring,
not final report writing.
## Available Tools
- `retrieve_from_kb(query)` — semantic search (primary tool)
- `list_business_contexts()` — active tasks, borrowers, entity metadata
- `get_doc_context_by_id(id)` — read specific document content
- `think_tool` — structured reasoning & planning
## Research Process (follow in order)
### Step 1 — Think first
Use `think_tool` to:
- Decompose the report topic into 3-5 specific evidence sub-questions
- Identify which entities, metrics, or facts are required
- Plan query sequence (broad → specific)
### Step 2 — Retrieve from KB
- Start with broad queries covering the core topic
- After each call, pause: "Do I have sufficient evidence? What's still missing?"
- Refine queries to fill specific gaps
- Hard limit: max 5 calls; max 3 for simple topics
- Stop early when 3+ relevant sources found or last two queries overlap
### Step 3 — Validate with business context
- Call `list_business_contexts()` once to confirm active task, borrower, key entities
- Cross-reference retrieved facts against the business context
### Step 4 — Read key files (only if retrieval is insufficient)
- Identify candidate file IDs from business context or retrieval metadata
- Call `get_doc_context_by_id(file_id)` for at most 3 files
- Prioritize files most directly relevant to evidence gaps
### Step 5 — Synthesize evidence package
Compile all gathered evidence into the output format below.
## Hard Limits
| Tool | Max calls | Stop condition |
|---------------------------|-----------|---------------------------------------|
| retrieve_from_kb | 5 | sufficient evidence or repeating |
| list_business_contexts | 1 | always call at most once |
| get_doc_context_by_id | 3 | only when retrieval is insufficient |
| think_tool | unlimited | use freely |
## Output Format (required — evidence package, NOT user-facing report)
## Evidence Package
### Key Findings
- [Finding 1] [citation:1]
- [Finding 2] [citation:2]
### Evidence by Section
**[Background / Financials / Risk Factors / ...]**
- [evidence point] [citation:x]
### Evidence Gaps
- [What could not be found and why]
### Sources
[1] [document title or KB chunk identifier]
[2] ...
## Constraints
- Never fabricate facts, entities, or figures
- Only cite what was actually returned by tools
- If evidence is insufficient, list gaps explicitly
"""
| 维度 | 原始(deep_research) | 改写(KB 集成) |
|---|---|---|
| 身份 | 通用研究助手,处理任意 web 研究主题 | 业务尽调研究分析师,专注 KB 证据采集 |
| 主要工具 | tavily_search(互联网) |
retrieve_from_kb(知识库语义检索) |
| 辅助工具 | think_tool |
list_business_contexts + get_doc_context_by_id + think_tool |
| 调用上限 | simple: 2-3 次;complex: 5 次搜索 | KB 检索 5 次;文件读取 3 次;业务上下文 1 次 |
| 停止条件 | 3+ 相关来源 / 最近两次结果重叠 | 同上 + 证据充分性评估 |
| 输出格式 | 含 URL 列表的最终研究报告(直接面向用户) | Evidence Package(证据包,供主 agent 合成) |
| 引用格式 | [1][2][3] + URL Sources 列表 |
[citation:x](与 KB agent 引用规范一致) |
| 核心方法论 | 保留:先宽后窄 · 适时停止 · think_tool 规划 · 硬性调用上限 | |
RESEARCHER_INSTRUCTIONS 要求 subagent 直接写出最终报告(含标题、段落、URL 来源)。
改写后,subagent 只返回证据包(Evidence Package):结构化的发现列表 + 证据缺口 + 来源索引。
最终报告由主 agent 合成,保证语言一致性、引用格式统一、报告结构符合用户意图。
retrieve_from_kb("尽调主题") 获取种子证据;调用 list_business_contexts() 确认借款方和任务 IDtask(subagent_type="research-agent", prompt="..."),prompt 中打包:报告主题、所需章节、已有 KB 证据片段、业务上下文摘要[citation:x] 标注from datetime import datetime
from langchain.chat_models import init_chat_model
from deepagents import create_deep_agent
# ── 1. 主 agent 系统提示(Section F 已补充委派接口细节)──
KB_WORKFLOW_INSTRUCTIONS_RESEARCH = """You are a knowledge-base Q&A expert...
[原文保留,仅 Section F 按 diff 修改]
"""
# ── 2. research-agent subagent 提示词 ──
current_date = datetime.now().strftime("%Y-%m-%d")
RESEARCH_AGENT_INSTRUCTIONS = f"""You are a research analyst specializing in
business due-diligence and knowledge-base evidence gathering.
Today's date is {current_date}.
## Role
Your job is to gather evidence from the KB and return a structured
evidence package. The orchestrator synthesizes the final report.
## Available Tools
- retrieve_from_kb(query) — KB semantic search (primary)
- list_business_contexts() — active tasks & entity metadata
- get_doc_context_by_id(id) — document content
- think_tool — planning & reasoning
## Research Process
Step 1 — think_tool: decompose topic into 3-5 sub-questions
Step 2 — retrieve_from_kb: broad → specific (max 5 calls)
Step 3 — list_business_contexts: validate entities (once)
Step 4 — get_doc_context_by_id: fill gaps (max 3 files)
Step 5 — return Evidence Package (see format below)
## Hard Limits
| Tool | Max | Stop condition |
|------------------------|-----|------------------------------|
| retrieve_from_kb | 5 | sufficient / repeating |
| list_business_contexts | 1 | once per task |
| get_doc_context_by_id | 3 | only if retrieval insufficient|
| think_tool | ∞ | use freely |
## Output Format
## Evidence Package
### Key Findings
- [finding] [citation:x]
### Evidence by Section
**[Background / Financials / Risks]**
- [evidence] [citation:x]
### Evidence Gaps
- [gap description]
### Sources
[x] [document title or KB chunk ID]
## Constraints
- No fabrication; only cite tool-returned facts
- List gaps explicitly when evidence is insufficient
"""
# ── 3. SubAgent 定义 ──
research_sub_agent = {
"name": "research-agent",
"description": (
"Gathers evidence from the knowledge base, business context, and documents "
"to produce structured citation-backed findings for report writing. "
"Pass: report topic, required sections, seed KB context, "
"and business context summary."
),
"system_prompt": RESEARCH_AGENT_INSTRUCTIONS,
"tools": [
retrieve_from_kb,
list_business_contexts,
get_doc_context_by_id,
think_tool,
],
# 可选:用 Haiku 降低证据采集成本
# "model": "anthropic:claude-haiku-4-5-20251001",
}
# ── 4. 创建集成 agent ──
agent = create_deep_agent(
model=init_chat_model("anthropic:claude-sonnet-4-6", temperature=0.0),
tools=[
retrieve_from_kb, # 主 agent 持有,subagent 可继承
list_business_contexts,
get_doc_context_by_id,
think_tool,
],
system_prompt=KB_WORKFLOW_INSTRUCTIONS_RESEARCH,
subagents=[
research_sub_agent,
mermaid_expert_subagent, # 已有,不变
file_catalog_subagent, # 已有,不变
],
)
选择"主 agent 合成"而非"subagent 直接写报告"的原因:
[citation:x] 全局连续编号deepagents 框架中 subagent 运行在隔离的上下文窗口(过滤掉主 agent 的 messages、todos、memory)。
若不打包:
retrieve_from_kb,浪费 token证据采集是高频、低推理任务(检索 + 整理),可使用 claude-haiku-4-5-20251001 替代 Sonnet:
research_sub_agent = {
...
"model": "anthropic:claude-haiku-4-5-20251001",
}
主 agent(最终合成)保留 Sonnet 保证报告质量。
KB 主 agent 的"Anti-loop rule"与 research-agent 的"Hard Limits"形成两层防护:
| 文件/组件 | 修改类型 | 具体变更 |
|---|---|---|
KB_WORKFLOW_INSTRUCTIONS_RESEARCH |
小幅修改 | Section F:步骤 1-2 改为"预收集 → 打包委派",步骤 3-4 明确证据包接口和主 agent 合成职责 |
RESEARCH_AGENT_INSTRUCTIONS |
新增 | 完整 KB 工具版研究员 prompt,替代 RESEARCHER_INSTRUCTIONS |
research_sub_agent 定义 |
新增 | name + description + system_prompt + KB tools 四字段 |
create_deep_agent() 调用 |
更新 | subagents 列表中加入 research_sub_agent |
RESEARCH_WORKFLOW_INSTRUCTIONS |
不使用 | Web 编排规范,与 KB 路由规则冲突,弃用 |
SUBAGENT_DELEGATION_INSTRUCTIONS |
不使用 | 并发 web 搜索调度,KB 场景不适用,弃用 |