Skip to main content

Usage

DeepAgent can spawn subagents to handle complex, multi-step independent tasks with isolated context. The task tool is automatically available.

Tool Signature

task(description: str, subagent_type: str = "general-purpose") -> str

Available Subagent Types

  1. general-purpose: Automatically available, created with same model as parent
  2. Custom agents: Agents provided in the subagents parameter

When to Use Subagents

  • Complex independent tasks that can be fully delegated
  • Parallel processing of multiple related tasks
  • Specialized expertise requiring focused knowledge
  • Context isolation for heavy token usage

Basic Example

import asyncio
from upsonic.agent.deepagent import DeepAgent
from upsonic import Task

async def main():
    agent = DeepAgent(model="openai/gpt-4o")
    
    task = Task(description="""
    Research Python web frameworks and return a summary.
    
    Then create /reports/frameworks.txt with the summary.
    """)
    
    result = await agent.do_async(task)
    print(result)

asyncio.run(main())

Custom Subagents Example

import asyncio
from upsonic.agent.deepagent import DeepAgent
from upsonic import Agent, Task

async def main():
    # Create specialized subagents
    researcher = Agent(
        model="openai/gpt-4o-mini",
        name="researcher",
        role="Research Specialist",
        system_prompt="You are a research expert focused on gathering information"
    )
    
    writer = Agent(
        model="openai/gpt-4o-mini",
        name="writer",
        role="Technical Writer",
        system_prompt="You are a technical writing expert"
    )
    
    # Create DeepAgent with subagents
    agent = DeepAgent(
        model="openai/gpt-4o",
        subagents=[researcher, writer]
    )
    
    task = Task(description="""
    Research AI trends and write a technical report based on research
    
    Save the final report to /reports/ai_trends.txt
    """)
    
    result = await agent.do_async(task)
    print(result)

asyncio.run(main())

Parallel Subagents Example

import asyncio
from upsonic.agent.deepagent import DeepAgent
from upsonic import Agent, Task

async def main():
    # Create multiple specialized subagents
    python_expert = Agent(
        model="openai/gpt-4o-mini",
        name="python-expert",
        system_prompt="Python programming expert"
    )
    
    js_expert = Agent(
        model="openai/gpt-4o-mini",
        name="js-expert",
        system_prompt="JavaScript programming expert"
    )
    
    agent = DeepAgent(
        model="openai/gpt-4o",
        subagents=[python_expert, js_expert]
    )
    
    task = Task(description="""
    Research Python web frameworks and JavaScript web frameworks in parallel.
    
    Then synthesize findings into /reports/comparison.txt
    """)
    
    result = await agent.do_async(task)
    print(result)

asyncio.run(main())

Adding Subagents Dynamically

import asyncio
from upsonic.agent.deepagent import DeepAgent
from upsonic import Agent, Task

async def main():
    agent = DeepAgent(model="openai/gpt-4o")
    
    # Add subagent after creation
    analyst = Agent(
        model="openai/gpt-4o-mini",
        name="analyst",
        role="Data Analyst",
        system_prompt="Data analysis expert"
    )
    agent.add_subagent(analyst)
    
    task = Task(description="""
    Analyze data trends.
    Save results to /analysis/trends.txt
    """)
    
    result = await agent.do_async(task)
    print(result)

asyncio.run(main())

Key Points

  • Subagents execute autonomously and return a single result
  • Subagents have isolated context (no memory sharing)
  • Each subagent must have a name attribute
  • Use parallel subagents for independent tasks