Spawn Subagent Skill
Minimum Model
Any model. Task delegation doesn't require complex reasoning.
When to Spawn vs. Stay in Main Session
Spawn a subagent when:
- - Task takes >30 seconds.
- Task has many sequential steps (research → draft → send → log).
- Task could fail and block the main session.
- Multiple independent tasks can run in parallel.
- Owner wants results "when ready," not now.
Stay in main session when:
- - Task takes <10 seconds.
- Task needs back-and-forth with the owner.
- Task needs the current conversation context.
How to Spawn
Basic Spawn
CODEBLOCK0
With Custom Model (for expensive reasoning tasks)
CODEBLOCK1
Writing Good Task Descriptions
A good task description has 4 parts:
- 1. What to do — specific actions
- Where inputs are — file paths, env vars, API endpoints
- What to output — exact format and save location
- What "done" looks like — clear completion signal
Good Example
CODEBLOCK2
Bad Example
CODEBLOCK3
Too vague — subagent won't know where files are or what to do with results.
Common Patterns
Batch Processing
CODEBLOCK4
Research + Draft
CODEBLOCK5
Parallel Independent Tasks
CODEBLOCK6
PA Daily Briefing (Non-Blocking)
CODEBLOCK7
Handling Completion
Do not poll. Wait for the push-based completion event.
When the completion event arrives:
- 1. Read the output file the subagent created.
- Use the results in the main session.
- Reply with
NO_REPLY if the owner doesn't need a response.
Failure Handling
If a subagent times out or fails:
- 1. Log the failure: append to
.learnings/ERRORS.md. - Notify owner if the task was time-sensitive.
- Retry with a simpler, more explicit task description.
- If still failing → run in the main session as a fallback.
Anti-Patterns
| ❌ Don't | ✅ Do Instead |
|---|
| Poll with sessions_list in a loop | Wait for push-based completion events |
| Spawn for a 5-second task |
Run quick tasks in main session |
| Use vague task descriptions | Be explicit about inputs, outputs, file paths |
| Spawn without a timeout | Always set
runTimeoutSeconds |
| Ignore subagent failures | Check for error events and handle them |
| Spawn a subagent from inside a subagent | Keep delegation to one level |
Cost Tips
- - Cheaper: Use small models for batch/shell operations — spawn with
model="provider/small-model". - Larger models only for: Complex reasoning, code generation, analysis.
- Avoid: Do not spawn many subagents simultaneously — they compete for resources.
- Batch: One subagent processing 100 items is cheaper than 100 subagents with 1 item each.
生成子代理技能
最低模型要求
任意模型均可。任务委派不需要复杂推理。
何时生成子代理 vs. 留在主会话中
生成子代理的情况:
- - 任务耗时超过30秒
- 任务包含多个连续步骤(研究→草稿→发送→记录)
- 任务可能失败并阻塞主会话
- 多个独立任务可以并行运行
- 任务负责人希望准备好时获取结果,而非立即获取
留在主会话中的情况:
- - 任务耗时少于10秒
- 任务需要与负责人来回沟通
- 任务需要当前会话上下文
如何生成子代理
基础生成
python
sessions_spawn(
task=[此处填写详细任务描述],
mode=run, # run = 一次性任务
runtime=subagent,
runTimeoutSeconds=300 # 5分钟后仍在运行则终止
)
使用自定义模型(适用于昂贵的推理任务)
python
sessions_spawn(
task=[复杂分析任务],
mode=run,
runtime=subagent,
runTimeoutSeconds=300,
# 仅在任务需要时使用高性能模型
model=your-provider/your-capable-model
# 示例: anthropic/claude-opus-4-6, openai/gpt-4o, google/gemini-1.5-pro
)
编写良好的任务描述
一个良好的任务描述包含4个部分:
- 1. 做什么 — 具体操作
- 输入在哪里 — 文件路径、环境变量、API端点
- 输出什么 — 精确格式和保存位置
- 完成的标志 — 明确的完成信号
良好示例
读取 /tmp/reports/ 目录下所有 .md 文件
用2-3句话总结每个文件
将所有摘要保存到 /tmp/reports/summary.md — 每个文件一个段落
完成后打印 完成:已总结X个文件
不要修改原始文件
糟糕示例
总结这些报告
过于模糊 — 子代理不知道文件在哪里或如何处理结果。
常见模式
批量处理
python
生成一个子代理处理所有项目 — 而不是每个项目一个子代理
sessions_spawn(
task=
处理 /tmp/items.json 中的每个项目:
1. 读取文件
2. 对每个项目:[描述操作]
3. 将结果保存到 /tmp/results/,每个项目一个文件 (item_ID.json)
4. 打印 完成:已处理X个项目
,
mode=run,
runtime=subagent,
runTimeoutSeconds=300
)
研究+草稿
python
sessions_spawn(
task=
1. 搜索网络:[主题]
2. 用要点总结前5个结果
3. 用通俗语言起草3段简报
4. 将草稿保存到 /tmp/briefing.md
5. 完成后打印 完成
,
mode=run,
runtime=subagent,
runTimeoutSeconds=180
)
并行独立任务
python
同时生成两个 — 比顺序执行更快
sessions_spawn(
task=获取最新邮件。保存到 /tmp/emails.json。打印完成。,
mode=run, runtime=subagent, runTimeoutSeconds=60
)
sessions_spawn(
task=获取今日日历事件。保存到 /tmp/calendar.json。打印完成。,
mode=run, runtime=subagent, runTimeoutSeconds=60
)
等待两个完成事件,然后读取两个文件
个人助理每日简报(非阻塞)
python
sessions_spawn(
task=
生成每日晨间简报:
1. 获取日历:GOG_ACCOUNT=owner@company.com gog calendar events primary --from TODAY --to TOMORROW
2. 获取邮件:GOGACCOUNT=owner@company.com gog gmail search is:unread newerthan:1d --max 5
3. 格式化为纯文本(使用大写字母作为章节标题,不使用markdown标题)
4. 保存到 /tmp/morning-briefing.txt
5. 打印完成
,
mode=run,
runtime=subagent,
runTimeoutSeconds=120
)
主会话保持空闲。完成后读取 /tmp/morning-briefing.txt,然后发送。
处理完成
不要轮询。 等待基于推送的完成事件。
当完成事件到达时:
- 1. 读取子代理创建的输出文件
- 在主会话中使用结果
- 如果负责人不需要回复,则回复 NO_REPLY
失败处理
如果子代理超时或失败:
- 1. 记录失败:追加到 .learnings/ERRORS.md
- 如果任务对时间敏感,通知负责人
- 使用更简单、更明确的任务描述重试
- 如果仍然失败 → 在主会话中运行作为后备方案
反模式
| ❌ 不要做 | ✅ 应该做 |
|---|
| 在循环中使用 sessions_list 轮询 | 等待基于推送的完成事件 |
| 为5秒的任务生成子代理 |
在主会话中运行快速任务 |
| 使用模糊的任务描述 | 明确输入、输出、文件路径 |
| 生成子代理时不设置超时 | 始终设置 runTimeoutSeconds |
| 忽略子代理失败 | 检查错误事件并处理 |
| 在子代理内部生成子代理 | 将委派限制在一层 |
成本提示
- - 更便宜: 对批量/Shell操作使用小型模型 — 使用 model=provider/small-model 生成
- 仅对以下情况使用大型模型: 复杂推理、代码生成、分析
- 避免: 不要同时生成多个子代理 — 它们会竞争资源
- 批量处理: 一个子代理处理100个项目比100个子代理各处理1个项目更便宜