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:-
Session Level (highest priority)
- Provided at runtime
- Read-only
- Use: Temporary, specialized agents for specific sessions
-
Project Level
- Location:
.qwen/agents/in project directory - Use: Project-specific agents
- Location:
-
User Level
- Location:
~/.qwen/agents/in home directory - Use: Personal agents across all projects
- Location:
-
Extension Level
- Provided by installed extensions
- Read-only
-
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:
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
tools is omitted, the subagent inherits all available tools.
Runtime Execution
Executing a Subagent
Subagents are invoked through thetask tool:
Execution Flow
-
Initialization:
- Load subagent configuration
- Create
SubAgentScopeinstance - Initialize tool registry with allowed tools
- Set up event emitter
-
Context Setup:
- Apply system prompt with variable substitution
- Set initial chat history
- Configure model parameters
-
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
-
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:Statistics and Telemetry
Subagents track execution statistics:Best Practices
Design Guidelines
- Single Responsibility: Each subagent should have a clear, focused purpose
- Minimal Tools: Only grant access to necessary tools
- Clear Instructions: Write detailed, unambiguous system prompts
- Appropriate Limits: Set reasonable time and turn limits
- Error Handling: Account for failures in parent agent logic
System Prompt Tips
Tool Selection
Read-only Tools (always safe):read_filegrep_searchgloblist_directory
write_fileeditrun_shell_command
task(enable nested delegation)
Built-in Subagents
Qwen Code includes built-in subagents:General Purpose Agent
Code Search Agent
packages/core/src/subagents/builtin-agents.ts for all built-in agents.
Validation
Subagent configurations are validated on:- Creation
- Loading
- Execution
- Name must be valid (alphanumeric, hyphens, underscores)
- Description must not be empty
- System prompt must not be empty
- Tools must exist in the tool registry
- Model config must be valid
- Run config must have reasonable limits
Error Handling
Testing Subagents
Test subagent configurations before deployment:Next Steps
- Configuration System - Understanding configuration
- Task Tool Reference - Using the task delegation tool
- Testing Guide - Testing subagents
