from deepagents import create_deep_agent, CompiledSubAgentfrom langchain.agents import create_agent# Create a custom agent graphcustom_graph = create_agent( model=your_model, tools=specialized_tools, prompt="You are a specialized agent for data analysis...")# Use it as a custom subagentcustom_subagent = CompiledSubAgent( name="data-analyzer", description="Specialized agent for complex data analysis tasks", runnable=custom_graph)subagents = [custom_subagent]agent = create_deep_agent( model="claude-sonnet-4-6", tools=[internet_search], system_prompt=research_instructions, subagents=subagents)
from deepagents import create_deep_agent# Main agent uses Claude; general-purpose subagent uses GPTagent = create_deep_agent( model="claude-sonnet-4-6", tools=[internet_search], subagents=[ { "name": "general-purpose", "description": "General-purpose agent for research and multi-step tasks", "system_prompt": "You are a general-purpose assistant.", "tools": [internet_search], "model": "openai:gpt-4o", # Different model for delegated tasks }, ],)
from deepagents import create_deep_agent# Research subagent with its own skillsresearch_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="claude-sonnet-4-6", skills=["/skills/main/"], # Main agent and GP subagent get these subagents=[research_subagent], # Gets only /skills/research/ and /skills/web-search/)
research_subagent = { "name": "research-agent", "description": "Conducts in-depth research using web search and synthesizes findings", "system_prompt": """You are a thorough researcher. Your job is to: 1. Break down the research question into searchable queries 2. Use internet_search to find relevant information 3. Synthesize findings into a comprehensive but concise summary 4. Cite sources when making claims Output format: - Summary (2-3 paragraphs) - Key findings (bullet points) - Sources (with URLs) Keep your response under 500 words to maintain clean context.""", "tools": [internet_search],}
data_analyst = { "system_prompt": """Analyze the data and return: 1. Key insights (3-5 bullet points) 2. Overall confidence score 3. Recommended next actions Do NOT include: - Raw data - Intermediate calculations - Detailed tool outputs Keep response under 300 words."""}
from deepagents import create_deep_agentsubagents = [ { "name": "data-collector", "description": "Gathers raw data from various sources", "system_prompt": "Collect comprehensive data on the topic", "tools": [web_search, api_call, database_query], }, { "name": "data-analyzer", "description": "Analyzes collected data for insights", "system_prompt": "Analyze data and extract key insights", "tools": [statistical_analysis], }, { "name": "report-writer", "description": "Writes polished reports from analysis", "system_prompt": "Create professional reports from insights", "tools": [format_document], },]agent = create_deep_agent( model="claude-sonnet-4-6", system_prompt="You coordinate data analysis and reporting. Use subagents for specialized tasks.", subagents=subagents)
from deepagents import create_deep_agentfrom langchain.agents import toolfrom pydantic import BaseModel@tooldef get_user_data(query: str, config) -> str: """Fetch data for the current user.""" user_id = config.get("context", {}).get("user_id") return f"Data for user {user_id}: {query}"research_subagent = { "name": "researcher", "description": "Conducts research for the current user", "system_prompt": "You are a research assistant.", "tools": [get_user_data],}agent = create_deep_agent( model="claude-sonnet-4-6", subagents=[research_subagent], context_schema={"user_id": str, "session_id": str},)# Context flows to the researcher subagent and its tools automaticallyresult = await agent.invoke( {"messages": [HumanMessage("Look up my recent activity")]}, {"context": {"user_id": "user-123", "session_id": "abc"}},)
result = await agent.invoke( {"messages": [HumanMessage("Research this and verify the claims")]}, { "context": { "user_id": "user-123", # shared by all agents "researcher:max_depth": 3, # only for researcher "fact-checker:strict_mode": True, # only for fact-checker } },)
# ✅ Good{"name": "research-specialist", "description": "Conducts in-depth research on specific topics using web search. Use when you need detailed information that requires multiple searches."}# ❌ Bad{"name": "helper", "description": "helps with stuff"}
指示主智能体委托:
Copy
agent = create_deep_agent( system_prompt="""...your instructions... IMPORTANT: For complex tasks, delegate to your subagents using the task() tool. This keeps your context clean and improves results.""", subagents=[...])
system_prompt="""...IMPORTANT: Return only the essential summary.Do NOT include raw data, intermediate search results, or detailed tool outputs.Your response should be under 500 words."""
使用文件系统处理大量数据:
Copy
system_prompt="""When you gather large amounts of data:1. Save raw data to /data/raw_results.txt2. Process and analyze the data3. Return only the analysis summaryThis keeps context clean."""
subagents = [ { "name": "quick-researcher", "description": "For simple, quick research questions that need 1-2 searches. Use when you need basic facts or definitions.", }, { "name": "deep-researcher", "description": "For complex, in-depth research requiring multiple searches, synthesis, and analysis. Use for comprehensive reports.", }]