
As agents take on more ambitious tasks, they have a hard time:随着代理承担的任务越来越宏大,它们面临着以下困难:
- Reliably completing work at scale难以大规模可靠地完成工作
- Managing their own context难以管理自身的上下文
We’ve been experimenting with how to handle these challenges in the form of what we’re calling dynamic subagents: instead of issuing subagent tasks through generic tool calling, the agent writes a short script that drives subagent execution. This means models can rely on code patterns it’s good at writing (like looping, branching, or concurrency) to write orchestration logic fit to the task.我们一直在尝试通过所谓的“动态子代理”来解决这些挑战:代理不再通过通用的工具调用来发布子代理任务,而是编写一段简短的脚本来驱动子代理的执行。这意味着模型可以利用其擅长的代码模式(如循环、分支或并发)来编写适合该任务的编排逻辑。
Why dynamic subagents?为什么需要动态子代理?
Deep Agents already supports subagents. They isolate context, let the main agent delegate discrete units of work, and keep intermediate results out of the main context window. So why do we need dynamic subagents?Deep Agents 已经支持子代理。它们可以隔离上下文,让主代理委派离散的工作单元,并将中间结果排除在主上下文窗口之外。那么,为什么我们还需要动态子代理呢?
With normal subagents, they are called one at a time, by the main model invoking them directly. That works at small scale. It breaks down when you need to spawn hundreds of subagents, or when the orchestration logic is conditional or multi-phase.普通的子代理是由主模型直接调用,一次只能调用一个。这在小规模任务中有效,但当需要生成数百个子代理,或者编排逻辑涉及条件判断或多阶段处理时,这种方式就会失效。
Dynamic subagents solve this with programmatic orchestration. Instead of making tool calls turn-by-turn, the agent writes a short script that orchestrates and calls subagents, and runs it in a lightweight interpreter.动态子代理通过程序化编排解决了这个问题。代理不再逐个进行工具调用,而是编写一段简短的脚本来编排和调用子代理,并在轻量级解释器中运行该脚本。
The canonical example: one subagent per page of a 300-page document. Rather than calling the subagent tool 300 times, the agent writes a loop:典型示例:针对 300 页文档中的每一页分配一个子代理。代理无需调用 300 次子代理工具,而是编写一个循环:
const results = await Promise.all(pages.map(page =>
task({ description: `Summarize page ${page.number}`, subagentType: "summarizer" })
));
This unlocks two things that tool-call-based orchestration can't reliably deliver:这解锁了基于工具调用的编排无法可靠实现的两个功能:
Deterministic coverage at scale. Without structure, agents make judgment calls about scope, screening 75 of 500 items and calling it done. A dispatch loop doesn't. Coverage becomes a structural guarantee, not a prompt engineering problem.大规模下的确定性覆盖。如果没有结构,代理会自行判断范围,例如在 500 个项目中筛选 75 个就认为完成了。而调度循环则不会。覆盖率成为了一种结构性保证,而不再是一个提示工程(prompt engineering)问题。
Reliable complex orchestration. Writing orchestration as code is more reliable than having the model reproduce it as a sequence of tool calls, especially for fan-out + synthesis, multi-phase pipelines, or conditional branching.可靠的复杂编排。将编排逻辑编写为代码比让模型将其重现为一系列工具调用更可靠,特别是在涉及扇出(fan-out)+ 合成、多阶段流水线或条件分支时。
This is the same idea behind workflows in Claude Code and Recursive Language Models (RLMs): a model writes code, and that code dispatches more agents.这与 Claude Code 和递归语言模型 (RLMs) 中的工作流背后的理念相同:模型编写代码,而该代码负责调度更多的代理。
Quickstart快速入门
Dynamic subagents require two things: subagents to dispatch work to, and a code interpreter: a secure, lightweight runtime where the model writes and executes orchestration code. Deep Agents includes an optional code interpreter based on QuickJS. To use it, install the QuickJS middleware package, then pass CodeInterpreterMiddleware via the middleware argument on create_deep_agent.动态子代理需要两样东西:用于分派工作的子代理,以及代码解释器:一个安全的、轻量级的运行时环境,模型可以在其中编写和执行编排代码。Deep Agents 包含一个基于 QuickJS 的可选代码解释器。要使用它,请安装 QuickJS 中间件包,然后在 create_deep_agent 的 middleware 参数中传入 CodeInterpreterMiddleware。
pip install -U "deepagents[quickjs]"
from deepagents import create_deep_agent
from langchain_quickjs import CodeInterpreterMiddleware
agent = create_deep_agent(
model="openai:gpt-5.5",
middleware=[CodeInterpreterMiddleware()],
)
Deep Agents ships with a general-purpose subagent built in, so there’s already one general subagent profile that can be used in workflows. For specialized workflows, configure custom subagents with their own names, descriptions, and system prompts: the names and descriptions are how the agent knows which role to reach for.Deep Agents 内置了一个通用子代理,因此工作流中已经有一个可用的通用子代理配置。对于专业工作流,您可以配置具有自定义名称、描述和系统提示词的子代理:代理正是通过名称和描述来识别该调用哪个角色的。
To trigger dynamic subagents, prompt your agent with the word "workflow", like this:要触发动态子代理,请在提示词中使用“workflow”一词,如下所示:
result = await agent.ainvoke({
"messages": [{"role": "user", "content": "Run a workflow that reviews every file in src/routes/ and summarizes the top risks."}]
})
Use with a coding agent与编码代理配合使用
The fastest way to try dynamic subagents is with dcode, our terminal coding agent built using a Deep Agent. It ships with the code interpreter enabled, so there's nothing to wire up — dynamic subagents works out of the box.尝试动态子代理最快的方法是使用 dcode,这是我们使用 Deep Agent 构建的终端编码代理。它默认启用了代码解释器,因此无需额外配置——动态子代理开箱即用。
Install安装
curl -LsSf https://langch.in/dcode | bash
Run运行
dcode
To trigger dynamic subagents, just ask for a “workflow”. Instead of grinding through the work itself, or trying to manage subagent fan outs with its native task tool, the agent writes an orchestration script that calls the built-in task() global and executes it in the code interpreter. For example: “run a workflow to review every file in src/ for SQL injection.”要触发动态子代理,只需请求一个“workflow”。代理不再亲自处理繁重的工作,也不再试图用其原生任务工具来管理子代理的扇出,而是编写一个调用内置 task() 全局函数的编排脚本,并在代码解释器中执行它。例如:“运行一个工作流来检查 src/ 中的每个文件是否存在 SQL 注入。”
As subagents spawn, dcode shows them live in the dynamic subagents panel grouped into phases by dispatch.当子代理生成时,dcode 会在动态子代理面板中实时显示它们,并按调度阶段进行分组。
.png)
You can try this fastest with dcode but you can also use it in your tool of choice via ACP (such as Zed)您可以通过 dcode 最快地尝试此功能,也可以通过 ACP(例如 Zed)在您选择的工具中使用它。
How it works工作原理
The agent is given an eval tool. It writes JavaScript that executes securely inside the interpreter. When subagents are configured, the interpreter exposes a built-in task() global that dispatches them from code. Based on the task at hand, the model writes different code — a loop, a branch, a Promise.all — and the interpreter runs it deterministically.代理被赋予一个 eval 工具。它编写在解释器内安全执行的 JavaScript 代码。当配置了子代理时,解释器会暴露一个内置的 task() 全局函数,用于从代码中分派子代理。根据手头的任务,模型会编写不同的代码——循环、分支、Promise.all 等——解释器则会确定性地运行它。

task() takes a description, a subagentType, and an optional responseSchema — when provided, the result is already a typed object, ready to filter or pass to the next step.task() 接收描述、subagentType 和可选的 responseSchema——如果提供了这些参数,结果将是一个已类型化的对象,可以直接进行过滤或传递给下一步。
const result = await task({
description: "Review src/auth/login.ts for security issues.",
subagentType: "reviewer",
responseSchema: {
type: "object",
properties: {
severity: { type: "string", enum: ["high", "medium", "low"] },
issues: { type: "array", items: { type: "string" } },
},
},
});
const critical = result.severity === "high" ? result.issues : [];
critical; // model sees the last line
For more, see Programmatic subagents and Interpreters in the docs.更多信息,请参阅文档中的“程序化子代理”和“解释器”部分。
Common Orchestration Patterns常见编排模式
Anthropic's dynamic workflows popularized a set of orchestration patterns for parallel agent work. They aren’t features you turn on. They’re shapes that naturally fall out of the work, and the agent settles into a different one as the task changes. The table below maps each shape to the kind of work it fits.Anthropic 的动态工作流推广了一套用于并行代理工作流的编排模式。它们不是需要开启的功能,而是工作中自然产生的形态,随着任务的变化,代理会进入不同的模式。下表将每种形态映射到其适用的工作类型。
Below we’ll dive into how each one works in Deep Agents, with live traces. We also put together a video explaining these six patterns, which you can check out here.下面我们将深入探讨每种模式在 Deep Agents 中是如何工作的,并提供实时追踪记录。我们还制作了一个解释这六种模式的视频,您可以点击此处查看。
Classify and act分类与执行
Items are classified first, then each item is handled by a specialized subagent based on its classification. This lets you process mixed inputs where different items need different expertise.首先对项目进行分类,然后根据分类由专门的子代理处理每个项目。这使您可以处理混合输入,其中不同的项目需要不同的专业知识。

Use cases: Triaging support tickets, error logs, user feedback, or any batch of items that need different handling depending on their type.用例:分流支持工单、错误日志、用户反馈,或任何需要根据类型进行不同处理的批处理项目。
Example: triaging a support-ticket backlog. The agent reads the tickets and classifies each as a bug, feature request, or question. Bugs to a bug-investigator, feature requests to a feature-analyst, and questions to a support-responder. The result is a summary grouped by category.示例:分流支持工单积压。代理阅读工单并将每个工单分类为错误、功能请求或问题。错误分配给错误调查员,功能请求分配给功能分析师,问题分配给支持响应者。结果是按类别分组的摘要。
View the trace here.在此查看追踪记录。
Fanout and synthesize扇出与合成
The agent dispatches the same kind of work across many items in parallel, then combines the results.代理在多个项目上并行分派相同类型的工作,然后合并结果。

Use cases: Code review across a directory, analyzing a batch of documents, processing log files, running the same check across many services.用例:跨目录的代码审查、分析一批文档、处理日志文件、跨多个服务运行相同的检查。
Example: a per-file security review across a source tree. The agent discovers every TypeScript file under src/ and dispatches one security-reviewer per file in parallel. It then merges the results into a single prioritized report with severity ratings and the lines that need to change.示例:跨源代码树的逐文件安全审查。代理发现 src/ 下的每个 TypeScript 文件,并并行分派一名安全审查员审查每个文件。然后,它将结果合并为一份优先级的报告,其中包含严重性评级和需要更改的代码行。
View the trace here.
Adversarial verification对抗性验证
A two-pass pattern. The first pass produces findings. The second pass sends each finding to independent verifiers, and only findings that survive agreement are kept. This reduces false positives when confidence matters more than speed.一种两轮模式。第一轮产生发现结果。第二轮将每个发现结果发送给独立的验证者,只有达成一致的发现结果才会被保留。当置信度比速度更重要时,这可以减少误报。
.png)
Use cases: Security audits where false positives are costly, compliance checks, any review where you need high confidence in findings.用例:误报代价高昂的安全审计、合规性检查,以及任何需要对发现结果具有高置信度的审查。
Example: a security audit where false positives are unacceptable. An auditor casts a wide net for potential vulnerabilities, then each finding is handed to an independent verifier that reads the code fresh and returns a CONFIRMED or REFUTED verdict. Only confirmed findings survive into the final report.示例:误报不可接受的安全审计。审计员广泛搜寻潜在漏洞,然后将每个发现结果交给独立的验证者,验证者重新阅读代码并返回“已确认”或“已反驳”的结论。只有确认的发现结果才会进入最终报告。
View the trace here.
Generate and filter生成与过滤
Multiple subagents generate independent solutions to the same problem. The agent compares, scores, and filters the results in code, keeping only the best.多个子代理针对同一个问题生成独立的解决方案。代理在代码中比较、评分并过滤结果,只保留最好的。

Use cases: Architecture proposals, refactoring strategies, content variations, any task where exploring multiple options before committing produces a better outcome.用例:架构提案、重构策略、内容变体,以及任何在提交前探索多种选项能产生更好结果的任务。
Example: competing rate-limiter redesigns, ranked. The agent has an architect to produce several independent redesigns of rate-limiter.ts, each written to its own file so they don’t overwrite each other. It then scores them on correctness under burst, multi-instance support, and complexity. The strongest one wins, with a rationale for why.示例:竞争性的速率限制器重新设计排名。代理让架构师生成 rate-limiter.ts 的多个独立重新设计方案,每个方案写入各自的文件,以免相互覆盖。然后,它根据突发流量下的正确性、多实例支持和复杂性进行评分。最强的方案胜出,并附带理由。
View the trace here.
Tournament锦标赛
Variations are compared head-to-head by a judge subagent, with winners advancing through elimination rounds.由裁判子代理对变体进行一对一比较,获胜者通过淘汰赛晋级。

Use cases: Optimization under subjective criteria, style selection, choosing between competing implementations.用例:主观标准下的优化、风格选择、在竞争性实现之间进行选择。
Example: a pairwise bracket over rewrites of a messy createOrder handler. Several writers each produce a candidate rewrites with different priorities, then a judge compares them head-to-head, advancing winners round-by-round until one champion stands out. It comes back with the judge’s reasoning.示例:对混乱的 createOrder 处理程序进行重写的两两对比。几位编写者各自生成具有不同优先级的候选重写方案,然后由裁判进行一对一比较,逐轮晋级获胜者,直到产生最终冠军。它会返回裁判的推理过程。
View the trace here.
Loop until done循环直到完成
The agent runs a discovery loop, deduplicating against what it has already found, until no new results appear. Useful when the scope of the work is not known upfront.代理运行发现循环,与已发现的内容进行去重,直到没有新结果出现。当工作范围事先未知时非常有用。

Use cases: Exhaustive search, dead code detection, dependency audits, any sweep where you want completeness rather than a fixed number of results.用例:穷举搜索、死代码检测、依赖审计,以及任何你想要完整性而非固定数量结果的扫描。
Example: a pass-based security sweep. The agent runs a scan pass, inspects what it found in code, and only starts another pass if the previous one surfaced new issues. It stops when a pass turns up nothing new. It reports the consolidated findings and how many passes it took.示例:基于轮次的扫描。代理运行扫描轮次,检查代码中发现的内容,仅在前一轮次发现了新问题时才开始下一轮。当某轮次没有发现新内容时停止。它报告汇总的发现结果以及所花费的轮次。
View the trace here.
Conclusion结论
Dynamic subagents are how you give agents more autonomy and increased reliability. The code handles coverage and intermediate context, and the model still does the judgement-heavy work. The above patterns above are a starting point. In practice, agents compose and mix them based on what the task demands.动态子代理是赋予代理更多自主权和更高可靠性的方式。代码处理覆盖范围和中间上下文,而模型仍然负责需要判断的工作。上述模式是一个起点。在实践中,代理会根据任务需求组合和混合使用它们。
This is the Recursive Language Model idea in its simplest form. An agent that writes code, and that code dispatches more agents. It’s an agent calling itself recursively and it isn’t capped by a context window or boxed into a fixed workflow. An agent can break the problem down as far as it goes and reassemble the pieces in whatever shape fits. The orchestration patterns highlighted above are early glimpses of what is possible but the ceiling will only continue to rise as models get better at writing code.这是递归语言模型理念的最简单形式。一个编写代码的代理,而该代码又调度更多的代理。这是一个递归调用自身的代理,它不受上下文窗口的限制,也不会被框定在固定的工作流中。代理可以尽可能地分解问题,并以任何适合的形态重新组装碎片。上述强调的编排模式只是可能性的初步一瞥,但随着模型编写代码能力的提升,上限将不断提高。
Dynamic subagents are how Deep Agents puts this in your hands today. Get started by adding a code interpreter to your agent, or reach for dcode where dynamic subagents works out of the box.动态子代理是 Deep Agents 今天为您提供此功能的方式。通过为您的代理添加代码解释器开始使用,或者使用 dcode,动态子代理在其中开箱即用。







