Coding

kimi-k3-subagent

Try it

Kimi K3子Agent架构 - 两阶段批量调度、生命周期管理、限流策略。借鉴MoonshotAI/kimi-code源码,提供Python实现

What it does

Kimi K3子Agent架构 - 两阶段批量调度、生命周期管理、限流策略。借鉴MoonshotAI/kimi-code源码,提供Python实现

The skill document

Kimi K3 Subagent 架构

Kimi K3 (MoonshotAI) 的子Agent调度架构,蒸馏为Python实现。

架构

SubagentHost (生命周期管理)
  ├── spawn()    — 创建新子Agent
  ├── resume()   — 恢复已有子Agent(保留上下文)
  ├── retry()    — 重试失败子Agent
  └── runQueued()— 批量执行

SubagentBatch (两阶段调度引擎)
  ├── 正常阶段: 5个立即启动, 之后每700ms启动1个
  └── 限流阶段: 指数退避(3s/6s/12s/24s) + 容量收缩/恢复(3分钟)

SwarmEventEmitter (生命周期事件)
  ├── spawned / started / completed / failed / suspended / batch_done
  └── on() / off() / emit() 监听机制

SummaryCheck (结果质量控制)
  └── 子Agent结果 ≥ 200字符, 不足自动补全

内置子Agent类型

类型用途工具
coder通用编码执行read/write/edit/bash/grep/glob
explore探索调研read/grep/glob/web_search/web_fetch
plan规划设计read/write/web_search
btw侧通道问答无工具(纯文本)

使用方法

1. 批量并行任务

from scripts.lib.agent import SubagentHost, SubagentBatch, QueuedSubagentTask

host = SubagentHost(session, "main_agent")

tasks = [
    QueuedSubagentTask(
        data={"code": "600519"},
        prompt="分析贵州茅台资金流向",
        profile_name="explore",
    ),
    QueuedSubagentTask(
        data={"code": "000858"},
        prompt="分析五粮液资金流向",
        profile_name="explore",
    ),
]
results = await host.run_queued(tasks)

2. 生命周期管理

# 创建子Agent
handle = await host.spawn("coder", "扫描板块资金流")

# 恢复已有子Agent
handle = await host.resume(agent_id, "继续之前的分析")

# 重试失败
handle = await host.retry(agent_id, "重试板块扫描")

3. 事件监听

from scripts.swarm import SwarmEventType

scheduler = SwarmScheduler(executor)

def on_completed(event_type, data):
    print(f"Task {data['task_id']} completed in {data['result_len']} chars")

scheduler.on(SwarmEventType.COMPLETED, on_completed)
results = await scheduler.run(tasks)

4. 结果摘要检查

子Agent结果自动确保≥200字符, 不足时自动补全:

from scripts.swarm import _ensure_min_summary

result = _ensure_min_summary(short_result)
# 不足200字符自动追加说明

限流策略

正常阶段 → 遇到RateLimit → 限流阶段
  ├── 容量收缩: 每次限流-1, 最小1
  ├── 重试间隔: 3s/6s/12s/24s... 指数退避
  └── 容量恢复: 3分钟无新限流则+1

文件清单

文件说明
scripts/lib/agent/subagent_types.py类型定义
scripts/lib/agent/subagent_host.py生命周期管理
scripts/lib/agent/subagent_batch.py批量调度引擎
scripts/swarm/init.py统一调度器(含事件+摘要检查)
learnings/kimi-k3-subagent-architecture.md完整学习笔记

Related skills

Call the Kimi API (kimi-k3, kimi-k2.7-code, kimi-k2.6, kimi-k2.5) through RunAPI using OpenAI-compatible Chat Completions. Use for Kimi text chat, streaming, or an existing compatibility client that needs the conditional reference.

2 installs

Moonshot AI thinking model with text, image, and video understanding. Suited to complex analysis, coding, and writing that needs long reasoning chains.

Kimi 桌面端管理 openclaw gateway 子进程生命周期。当用户提到"重启 / 启动 / 停止 openclaw / openclaw 卡住 / gateway 没响应"等需要操控 daemon 进程时,必读此 skill。 说明哪些 CLI 不能用、以及正确的恢复路径。

Matt Pocock 工程方法论 Kimi 适配版 — 一键安装 6 个生产级开发 skills: grilling(需求对齐)、grill-with-docs(代码库对齐+文档维护)、handoff(跨会话交接)、 tdd(测试驱动开发)、diagnosing-bugs(六阶段 Bug 诊断)、to-prd(...

3 installs

Produce a comprehensive prompt that hands off the current session's work to a fresh session for sub-agent-orchestrated execution. Use when the user wants to...

1 installs

基于文件的知识捕获与检索工具,支持URL、视频、文章、社交内容收藏与搜索。Use when 需要SEO优化、关键词分析、排名提升、搜索流量优化时使用。不适用于黑帽SEO手段。适用于独立开发者、企业团队和自动化工作流场景。支持中文交互,无需复杂配置即开即用。输出结果可直接使用,减少二次加工成本。提供结构化输出和错误处理机制。