Skip to main content

Subagents System

The subagents system enables task delegation to specialized agents, allowing Qwen Code to handle complex, multi-step tasks autonomously through parallel execution and specialized expertise.

Overview

Subagents are lightweight, single-use AI agents that:
  • Execute tasks autonomously with their own tool access
  • Run in parallel for improved performance
  • Have specialized system prompts and configurations
  • Communicate through events for real-time UI updates
  • Are stateless and terminate after completing their task

Architecture

Core Components

Configuration Storage

Subagents are configured as Markdown files with YAML frontmatter: File Format:

Storage Hierarchy

Subagents are stored at different levels with priority:
  1. Session Level (highest priority)
    • Provided at runtime
    • Read-only
    • Use: Temporary, specialized agents for specific sessions
  2. Project Level
    • Location: .qwen/agents/ in project directory
    • Use: Project-specific agents
  3. User Level
    • Location: ~/.qwen/agents/ in home directory
    • Use: Personal agents across all projects
  4. Extension Level
    • Provided by installed extensions
    • Read-only
  5. Built-in Level (lowest priority)
    • Embedded in codebase
    • Always available

Creating Subagents

Using the SubagentManager

Via Command Line

Users can create subagents using the /agents command:
This opens an interactive wizard to configure the subagent.

Manual File Creation

Create a Markdown file in the appropriate directory:

Configuration Options

Required Fields

Optional Fields

Tool Configuration

Available Tools:
  • File system: read_file, write_file, edit, glob, grep_search, list_directory
  • Shell: run_shell_command
  • Memory: save_memory
  • Task: task (for nested delegation)
  • Web: web_fetch, web_search
  • LSP: lsp_* tools
  • MCP: Dynamically loaded MCP tools
Example Tool Configuration:
If tools is omitted, the subagent inherits all available tools.

Runtime Execution

Executing a Subagent

Subagents are invoked through the task tool:

Execution Flow

  1. Initialization:
    • Load subagent configuration
    • Create SubAgentScope instance
    • Initialize tool registry with allowed tools
    • Set up event emitter
  2. Context Setup:
    • Apply system prompt with variable substitution
    • Set initial chat history
    • Configure model parameters
  3. Execution Loop:
    • Send prompt to model
    • Process tool calls
    • Handle confirmations (if needed)
    • Execute tools
    • Send results back to model
    • Repeat until completion or limits
  4. Termination:
    • Model returns final text response
    • Emit finish event
    • Clean up resources
    • Return result to parent agent

Event-Driven Updates

Subagents emit events for real-time UI updates:

Context State and Templating

Subagents support variable substitution in system prompts:

Parallel Execution

Multiple subagents can run simultaneously:
The Task tool supports parallel execution when multiple tool calls are made in a single model response.

Statistics and Telemetry

Subagents track execution statistics:
Statistics are logged for analytics (respecting privacy settings):

Best Practices

Design Guidelines

  1. Single Responsibility: Each subagent should have a clear, focused purpose
  2. Minimal Tools: Only grant access to necessary tools
  3. Clear Instructions: Write detailed, unambiguous system prompts
  4. Appropriate Limits: Set reasonable time and turn limits
  5. Error Handling: Account for failures in parent agent logic

System Prompt Tips

Tool Selection

Read-only Tools (always safe):
  • read_file
  • grep_search
  • glob
  • list_directory
Modifying Tools (require careful consideration):
  • write_file
  • edit
  • run_shell_command
Delegation Tools (for complex tasks):
  • task (enable nested delegation)

Built-in Subagents

Qwen Code includes built-in subagents:

General Purpose Agent

Code Search Agent

See packages/core/src/subagents/builtin-agents.ts for all built-in agents.

Validation

Subagent configurations are validated on:
  • Creation
  • Loading
  • Execution
Validation Rules:
  1. Name must be valid (alphanumeric, hyphens, underscores)
  2. Description must not be empty
  3. System prompt must not be empty
  4. Tools must exist in the tool registry
  5. Model config must be valid
  6. Run config must have reasonable limits

Error Handling

Testing Subagents

Test subagent configurations before deployment:

Next Steps