跳转到主要内容
子代理(又称 Subagent)是 Qoder CLI 中专门用于处理特定任务的 AI Agent,每个 Subagent 拥有独立的上下文窗口、系统提示词和工具权限,通过合理使用可以显著改善复杂任务的处理能力。

快速开始

3 步创建你的第一个 Subagent
  1. 打开配置面板:在 TUI 中执行 /agents
  2. 创建 Subagent:按 Tab 切换到 User 或 Project 标签页,选择 “Create new agent…”
  3. 开始使用:输入描述后,Qoder CLI 自动生成配置
帮我使用 general-purpose subagent 分析项目功能

核心价值

价值说明
上下文隔离每个 Subagent 在独立上下文中运行,防止污染主对话,使其专注于高层目标
专业化能力可针对特定领域配置详细指令,提升任务成功率
可复用性可跨项目使用,也可与团队共享统一工作流程
灵活权限每个 Subagent 可配置不同的工具访问级别

内置 Subagent

在 TUI 中执行 /agents,按 Tab 切换到 BuiltIn 标签页。
提示: User 标签页展示用户级别 Subagent 配置,Project 标签页展示项目级别 Subagent 配置。
内置 Subagent 的名称及功能说明如下:
名称功能描述
code-reviewer执行本地代码 Code Review 任务
design-agent根据用户需求进行软件设计并生成设计文档
general-purpose执行各类通用任务,适用于各种场景
task-executor根据设计文档开展软件开发任务

使用 Subagent

无论是在 TUI 还是 Headless 模式下,都可以通过自然语言唤起 Subagent 执行任务。

显式唤起

使用自然语言直接指定 Subagent 进行对话:
# TUI 模式
帮我使用 code-reviewer subagent 进行代码审查

# Headless 模式
qodercli -p "帮我使用 code-reviewer subagent 进行代码审查"

隐式唤起

使用自然语言直接输入任务内容,让 CLI 帮助你选择合适的 Subagent 处理任务:
# TUI 模式
帮我进行代码审查

# Headless 模式
qodercli -p "帮我进行代码审查"

串联唤起

使用自然语言描述 Subagent 的先后执行顺序,按照编排的流程顺序进行任务处理:
# TUI 模式
先使用 general-purpose subagent 完成系统开发和设计,最后使用 code-reviewer subagent 对生成代码进行审查

# Headless 模式
qodercli -p "先使用 general-purpose subagent 完成系统开发和设计,最后使用 code-reviewer subagent 对生成代码进行审查" --max-turns 10
提示: Headless 模式下可使用 --max-turns 参数限制 Subagent 的执行轮次。

创建 Subagent

Qoder CLI 提供两种方式创建 Subagent:AI 辅助生成(推荐)和手动编写配置文件。

方式一:AI 辅助生成(推荐)

这是创建 Subagent 最简单的方式,只需用自然语言描述你的需求,Qoder CLI 会自动生成完整的配置。 操作步骤:
  1. 在 TUI 中执行 /agents 进入配置面板
  2. Tab 切换到 User 或 Project 标签页
  3. 选择 “Create new agent…” 并按 Enter
  4. 输入 Subagent 描述,按 Enter 确认
> /agents
------------------------------------------------------------------------------------------
Agents:  User  [Project]  BuiltIn

-> Create new agent...

Agent list:
No project agents found.

Press Enter to select - Esc to exit - Tab to cycle tabs - Up/Down to navigate
输入描述后,Qoder CLI 自动生成配置:
> /agents
------------------------------------------------------------------------------------------
Agents:  User  [Project]  BuiltIn

Describe the agent:

> Help me review RESTful api design

Press Enter to select - Esc to exit - Tab to cycle tabs - Up/Down to navigate
生成完成后,可以在对应目录找到配置文件进行微调:
# 项目级别(Project 标签页)
.qoder/agents/

# 用户级别(User 标签页)
~/.qoder/agents/
提示: 建议先使用 AI 生成初始 Subagent,然后迭代优化使其符合你的特定需求。这种方法能给你最好的结果——一个可以定制的基础配置。

方式二:手动编写配置(进阶)

如果你需要完全控制 Subagent 配置,可以手动创建 Markdown 配置文件。
---
name: api-reviewer
description: Review API designs for RESTful compliance and best practices, including endpoint structures, HTTP methods, status codes, and resource naming. Evaluates REST principles and suggests improvements.
tools: Read,Grep,Glob
---

You are an expert API design reviewer specializing in RESTful architecture principles and best practices. Your role is to evaluate API designs for compliance with REST conventions, scalability, maintainability, and developer experience.

When reviewing APIs, you will focus on:

1. Resource Naming
- Use nouns instead of verbs for resources
- Use plural forms for collections (e.g., /users not /user)
- Use kebab-case or snake_case consistently (prefer kebab-case)
- Avoid CRUD verbs in URLs

2. HTTP Methods Compliance
- GET: Retrieve resources (safe, idempotent)
- POST: Create resources or actions
- PUT: Update entire resources (idempotent)
- PATCH: Partial updates (idempotent)
- DELETE: Remove resources (idempotent)

3. Status Codes
- 200: Successful GET, PUT, PATCH
- 201: Successful POST with resource creation
- 204: Successful DELETE or update with no response body
- 400: Client errors (validation, malformed requests)
- 401/403: Authentication/authorization issues
- 404: Resource not found
- 409: Conflicts (e.g., duplicate resources)
- 500: Server errors

4. URL Structure
- Use hierarchical URLs for relationships (/users/123/orders)
- Keep URLs short but meaningful
- Use query parameters for filtering, sorting, pagination
- Version APIs in URL path (/api/v1/) or headers

5. Response Format
- Consistent JSON structure
- Proper error message formats
- Include HATEOAS links where appropriate
- Standardized timestamp formats

When providing feedback:
1. First identify any RESTful violations or anti-patterns
2. Explain why the current design is problematic
3. Provide specific recommendations for improvement
4. Reference relevant REST constraints or best practices
5. Consider scalability and future extensibility

Be thorough but constructive in your reviews. Focus on technical correctness while considering real-world implementation concerns.
存放位置: 将配置文件保存到对应目录:
# 项目级别(仅当前项目生效,可提交版本控制)
.qoder/agents/<agentName>.md

# 用户级别(所有项目生效)
~/.qoder/agents/<agentName>.md

存储位置与优先级

类型路径优先级适用场景
项目级别.qoder/agents/<agentName>.md只对当前项目生效,提示词针对当前项目专门设计和优化,通常与代码一并提交到远端代码仓库中
用户级别~/.qoder/agents/<agentName>.md对当前系统中的所有项目生效,提示词相对通用,具备应用到多个项目中使用的特质
注意: 当 Subagent 名称冲突时,项目级别的 Subagent 优先于用户级别的 Subagent。

配置字段参考

字段必需说明示例
nameSubagent 的唯一标识符,使用小写字母和连字符api-reviewer
descriptionSubagent 用途的自然语言描述,模型根据该内容选择具体的 Subagent 执行任务Review API designs for RESTful compliance
tools逗号分隔的工具名称列表。省略时继承主 Agent 的所有工具。默认为 * 代表可以使用主 Agent 配置的所有工具Read,Grep,Glob

工具配置

Subagent 支持配置可以使用的工具清单,只需要在 tools 字段中配置具体的工具名称。工具包括任意内置工具和 MCP 工具。
tools: Read,Grep,Glob
工具配置规则:
  • 名称之间以逗号分隔
  • 默认为 * 代表可以使用主 Agent 配置的所有工具
  • 支持 MCP 工具,MCP 工具会自动继承到 Subagent 中
提示: 建议只授予 Subagent 必要的工具,这有助于提高安全性和帮助 Subagent 专注于相关操作。

测试任务执行效果

输入如下提示词进行效果验证:
@app.route('/api/do_login', methods=['POST'])
def do_login():
    # ...

@app.route('/api/do_logout', methods=['GET'])
def do_logout():
    # ...

使用 api-reviewer 帮我审查一下上述 Flask 应用的 API 接口设计
在提问之后,CLI 会唤起 api-reviewer subagent 并分派 API 审查任务。

最佳实践

  • 从 AI 生成的 Agent 开始:先使用 AI 生成初始 Subagent,然后迭代优化使其符合你的特定需求
  • 设计聚焦的 Subagent:创建具有单一、明确职责的 Subagent,而不是试图让一个 Subagent 做所有事情。这能提高性能并使 Subagent 更可预测
  • 编写详细的提示词:在系统提示词中包含具体的指令、示例和约束。提供的指导越多,Subagent 的表现就越好
  • 限制工具访问:只授予 Subagent 必要的工具。这有助于提高安全性和帮助 Subagent 专注于相关操作
  • 版本控制:将项目级别的 Subagent 提交到版本控制中,以便团队可以协作受益和改进它们

常见问题

Subagent 和主 Agent 有什么区别?

Subagent 拥有独立的上下文窗口,不会污染主对话。每个 Subagent 可以配置不同的工具权限和系统提示词,使其专注于特定类型的任务。

如何查看所有可用的 Subagent?

在 TUI 中执行 /agents 命令,可以查看所有可用的 Subagent(包括内置、用户级别和项目级别的)。

可以同时使用多个 Subagent 吗?

可以。使用串联唤起的方式,可以按顺序调用多个 Subagent 完成复杂工作流。