feat: improve research agent prompt design and delegation strategy

- Add date formatting to RESEARCHER_INSTRUCTIONS placeholder
- Reorder workflow steps (plan before saving request)
- Add synthesis step for consolidating sub-agent findings
- Separate orchestrator and sub-agent instructions
- Remove RESEARCHER_INSTRUCTIONS from main agent (orchestrator only delegates)
- Simplify citation format and remove duplication
- Bias delegation strategy toward single sub-agent by default
- Only parallelize for explicit comparisons or clearly separated aspects
- Enhance verification step to check citations and structure
This commit is contained in:
Lance Martin
2025-11-18 17:04:24 -08:00
parent 1558609c77
commit f1ca60e229
4 changed files with 1199 additions and 1117 deletions
+4 -1
View File
@@ -163,4 +163,7 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.langgraph_api/
# Other
.langgraph_api/
.vscode/
.DS_Store
+13 -11
View File
@@ -4,7 +4,10 @@ This module creates a deep research agent with custom tools and prompts
for conducting web research with strategic thinking and context management.
"""
from datetime import datetime
from langchain.chat_models import init_chat_model
from langchain_google_genai import ChatGoogleGenerativeAI
from deepagents import create_deep_agent
from research_agent.prompts import (
@@ -18,7 +21,10 @@ from research_agent.tools import tavily_search, think_tool
max_concurrent_research_units = 3
max_researcher_iterations = 3
# Combine instructions
# Get current date
current_date = datetime.now().strftime("%Y-%m-%d")
# Combine orchestrator instructions (RESEARCHER_INSTRUCTIONS only for sub-agents)
INSTRUCTIONS = (
RESEARCH_WORKFLOW_INSTRUCTIONS
+ "\n\n"
@@ -28,25 +34,21 @@ INSTRUCTIONS = (
max_concurrent_research_units=max_concurrent_research_units,
max_researcher_iterations=max_researcher_iterations,
)
+ "\n\n"
+ "=" * 80
+ "\n\n"
+ RESEARCHER_INSTRUCTIONS
)
# Create research sub-agent
research_sub_agent = {
"name": "research-agent",
"description": "Delegate research to the sub-agent researcher. Only give this researcher one topic at a time.",
"system_prompt": RESEARCHER_INSTRUCTIONS,
"system_prompt": RESEARCHER_INSTRUCTIONS.format(date=current_date),
"tools": [tavily_search, think_tool],
}
# Initialize model
model = init_chat_model(
model="anthropic:claude-sonnet-4-5-20250929",
temperature=0.0
)
# Model Claude 4.5
# model = init_chat_model(model="anthropic:claude-sonnet-4-5-20250929", temperature=0.0)
# Model Gemini 3
model = ChatGoogleGenerativeAI(model="gemini-3-pro-preview", temperature=0.0)
# Create the agent
agent = create_deep_agent(
File diff suppressed because it is too large Load Diff
+34 -23
View File
@@ -4,11 +4,12 @@ RESEARCH_WORKFLOW_INSTRUCTIONS = """# Research Workflow
Follow this workflow for all research requests:
1. **Save the request**: Use write_file() to save the user's research question to `/research_request.md`
2. **Plan**: Create a todo list with write_todos to break down the research into focused tasks
3. **Research**: Delegate research tasks to sub-agents using the task() tool
4. **Write Report**: Write a comprehensive final report to `/final_report.md` (see Report Writing Guidelines below)
5. **Verify**: Read `/research_request.md` to confirm you've addressed all aspects of the original question
1. **Plan**: Create a todo list with write_todos to break down the research into focused tasks
2. **Save the request**: Use write_file() to save the user's research question to `/research_request.md`
3. **Research**: Delegate research tasks to sub-agents using the task() tool - ALWAYS use sub-agents for research, never conduct research yourself
4. **Synthesize**: Review all sub-agent findings and consolidate citations (each unique URL gets one number across all findings)
5. **Write Report**: Write a comprehensive final report to `/final_report.md` (see Report Writing Guidelines below)
6. **Verify**: Read `/research_request.md` and confirm you've addressed all aspects with proper citations and structure
## Research Planning Guidelines
- Batch similar research tasks into a single TODO to minimize overhead
@@ -48,10 +49,9 @@ Simply list items with details - no introduction needed:
- Each section should be comprehensive and detailed
- Use bullet points only when listing is more appropriate than prose
<Citation Rules>
Use numbered citations throughout your report:
**Citation format:**
- Cite sources inline using [1], [2], [3] format
- Assign each unique URL a single citation number
- Assign each unique URL a single citation number across ALL sub-agent findings
- End report with ### Sources section listing each numbered source
- Number sources sequentially without gaps (1,2,3,4...)
- Format: [1] Source Title: URL (each on separate line for proper list rendering)
@@ -62,7 +62,6 @@ Use numbered citations throughout your report:
### Sources
[1] AI Research Paper: https://example.com/paper
[2] Industry Analysis: https://example.com/analysis
</Citation Rules>
"""
RESEARCHER_INSTRUCTIONS = """You are a research assistant conducting research on the user's input topic. For context, today's date is {date}.
@@ -111,20 +110,24 @@ After each search tool call, use think_tool to analyze the results:
</Show Your Thinking>
<Final Response Format>
When providing your final answer, use numbered citations and include a Sources section:
When providing your findings back to the orchestrator:
1. **Cite sources inline**: Use [1], [2], [3] format when referencing information
2. **Sources section**: End with a numbered list of all sources
1. **Structure your response**: Organize findings with clear headings and detailed explanations
2. **Cite sources inline**: Use [1], [2], [3] format when referencing information from your searches
3. **Include Sources section**: End with ### Sources listing each numbered source with title and URL
Example format:
Example:
```
## Key Findings
Context engineering is a critical technique for AI agents [1]. Studies show that proper context management can improve performance by 40% [2].
### Sources
[1] Context Engineering Guide: https://example.com/context-guide
[2] AI Performance Study: https://example.com/study
```
This allows readers to verify information and explore topics further.
The orchestrator will consolidate citations from all sub-agents into the final report.
</Final Response Format>
"""
@@ -138,20 +141,28 @@ Your role is to coordinate research by delegating tasks from your TODO list to s
## Delegation Strategy
**Simple queries** → 1 sub-agent:
- "List the top 10 coffee shops in San Francisco"
- "What is quantum computing?"
- "Summarize the history of the internet"
**DEFAULT: Start with 1 sub-agent** for most queries:
- "What is quantum computing?" → 1 sub-agent (general overview)
- "List the top 10 coffee shops in San Francisco" → 1 sub-agent
- "Summarize the history of the internet" → 1 sub-agent
- "Research context engineering for AI agents" → 1 sub-agent (covers all aspects)
**Comparisons** → 1 sub-agent per element:
**ONLY parallelize when the query EXPLICITLY requires comparison or has clearly independent aspects:**
**Explicit comparisons** → 1 sub-agent per element:
- "Compare OpenAI vs Anthropic vs DeepMind AI safety approaches" → 3 parallel sub-agents
- "Compare Python vs JavaScript for web development" → 2 parallel sub-agents
**Multi-faceted research** → 1 sub-agent per aspect:
- "Research renewable energy: costs, environmental impact, adoption rates" → 3 parallel sub-agents
- "Analyze the 2024 election: candidates, issues, polling" → 3 parallel sub-agents
**Clearly separated aspects** → 1 sub-agent per aspect (use sparingly):
- "Research renewable energy adoption in Europe, Asia, and North America" → 3 parallel sub-agents (geographic separation)
- Only use this pattern when aspects cannot be covered efficiently by a single comprehensive search
## Parallel Execution
## Key Principles
- **Bias towards single sub-agent**: One comprehensive research task is more token-efficient than multiple narrow ones
- **Avoid premature decomposition**: Don't break "research X" into "research X overview", "research X techniques", "research X applications" - just use 1 sub-agent for all of X
- **Parallelize only for clear comparisons**: Use multiple sub-agents when comparing distinct entities or geographically separated data
## Parallel Execution Limits
- Use at most {max_concurrent_research_units} parallel sub-agents per iteration
- Make multiple task() calls in a single response to enable parallel execution
- Each sub-agent returns findings independently