# A harness for every task: dynamic workflows in Claude Code
**作者**: Thariq
**日期**: 2026-06-02T20:26:32.000Z
**来源**: [https://x.com/trq212/status/2061907337154367865](https://x.com/trq212/status/2061907337154367865)
---

Last week, we released dynamic workflows in Claude Code. Claude can now write its own harness on the fly, custom-built for the task at hand.
While the default Claude Code harness is built for coding, it is also useful for many other types of tasks because, as it turns out, many tasks resemble coding tasks. But there are certain classes of tasks where we have had to build custom harnesses on top of Claude Code to achieve peak performance such as Research, security analysis, agent teams, or Code Review.
Workflows allow you to dynamically create harnesses that enable Claude to solve all of those problems and more natively inside of Claude Code. You can also share and re-use these workflows with others.
In this article, I’ll cover my initial workflows experiences and learnings so you can take full advantage.
That said, best practices are still developing! Dynamic workflows often use more tokens, so think carefully about when and how to use them.
Note: this post is also available on the Claude Blog
## Example prompts
Before diving into the technical details, I’d like to start with some example prompts to get you thinking about the possibilities with workflows:
- "This test fails maybe 1 in 50 runs. Set up a workflow to reproduce it, form theories and adversarially test them in worktrees /goal don't stop until one theory works."
- "Using a workflow, go through my last 50 sessions and mine them for corrections I keep making and turn the recurring ones into CLAUDE.md rules"
- “Use a workflow to dig through #incidents in Slack for the past six months and find recurring root causes where nobody has filed a ticket."
- "Take my business plan and run a workflow where different agents tear it apart from an investor's, a customer's, and a competitor's perspective."
- "Here's a folder of 80 resumes, use a workflow to rank them for the backend role and double-check the top ten. Interview me using the AskUserQuestion tool for a rubric."
- "I need a name for this CLI tool. Use a workflow to brainstorm a bunch of options and run a tournament to pick the top 3."
- "Use a workflow to rename our User model to Account everywhere."
- “Go through my blog post draft and using a workflow verify every technical claim against the codebase, I don't want to ship anything wrong."
## How dynamic workflows work
Dynamic workflows execute a javascript file with a few special functions that help spawn and coordinate subagents:

Dynamic workflows also include standard JavaScript functions like JSON, Math, and Array, to help process data.
It’s particularly useful to know that dynamic workflows can decide which models an agent uses and whether subagents are run in their own worktree, allowing Claude to choose the intelligence level and isolation needed.
If a workflow is interrupted, for example by user action or quitting the terminal, resuming the session will allow the workflow to pick up where it left off.
## Why dynamic workflows
When you ask the default Claude Code harness to do a task, it needs to both plan and execute in the same context window. For many coding tasks, this is highly effective, but it can sometimes break down over long-running, massively parallel and/or highly structured adversarial tasks.
This is because the longer Claude works on a complex task in a single context window, the more it becomes susceptible to a few specific failure modes:
- Agentic laziness refers to when Claude stops before finishing a particularly complex, multi-part task and declares the job done after partial progress, for example addressing 20 of the 50 items in a security review.
- Self-preferential bias refers to Claude’s tendency to prefer its own results or findings, especially when asked to verify or judge them against a rubric.
- Goal drift refers to the gradual loss of fidelity to the original objective across many turns, especially after compaction. Each summarization step is lossy, and details like edge-case requirements or "don't do X" constraints can get lost.
Creating a workflow helps combat these by orchestrating separate Claudes with their own context windows and focused, isolated goals.
## Dynamic vs static workflows
You may have previously created a static workflow using the Claude Agent SDK or claude -p to coordinate multiple instances of Claude Code together.
But because static workflows need to work for all edge cases, they are usually more generic. With Claude Opus 4.8 and dynamic workflows, Claude is now intelligent enough to write a custom harness tailor-made for your use case.

# Helpful patterns when using dynamic workflows
You can start using dynamic workflows just by asking Claude to make one, or by using the trigger word “ultracode” to ensure that Claude Code creates a workflow.
But building a mental model for how dynamic workflows work will help you understand when to use them and how you might nudge Claude via prompts.
There are a few common patterns that Claude might use and compose together when building workflows:

Classify-and-act
Use a classifier agent to decide on the type of task, and then route to different agents or behavior based on the task. Or, use a classifier at the end to determine output.
Fan-out-and-synthesize
Split up a task into many smaller steps, run an agent on each step and then synthesize those results. This is particularly useful for when there are a large number of smaller steps, or when each step benefits from its own clean context window so they don't interfere or cross-contaminate. The synthesize step is a barrier—it waits for all the fan-out agents, then merges their structured outputs into one result.
Adversarial verification
For each spawned agent, run a separate spawned agent to adversarially verify its output against a rubric or criteria.
Generate-and-filter
Generate a number of ideas on a topic and then filter them by a rubric or by verification, dedupe duplicates and return only the highest quality, tested ideas.
Tournament
Instead of dividing the work, have agents compete on it. Spawn N agents that each attempt the same task using different approaches. Prompts or models then judge the results in a pairwise fashion using a judging agent until you have a winner.
Loop until done
For tasks with an unknown amount of work, loop spawning agents until a stop condition is met (no new findings, or no more errors in the logs) instead of a fixed number of passes.
# Use cases
Think creatively of when and how to ask Claude Code to make dynamic workflows. I’ve found that workflows are sometimes even more useful for non-technical work.

## Migrations and refactors
Bun was rewritten from Zig to Rust using workflows. You can read more about how that was done in Jarred’s X thread.
The key is to break down the task into a series of steps that need to be operated on for example callsites, failing tests, modules, etc. Spin off a subagent for every fix in a worktree to make the fix, then have another agent adversarially review, and merge them. Consider telling the agent not to use resource intensive commands so that you can maximally parallelize without running out of resources on your machine.
## Deep research
We published a deep research skill (/deep-research) inside Claude Code that uses dynamic workflows. Specifically, it fans-out web searches, fetches sources, adversarially verifies their claims, and synthesizes a cited report.
But you may do this sort of research for more than just web searches. For example, asking Claude to compile a status report from context in Slack or to research how a feature works by exploring a codebase in-depth.
## Deep verification

On the other hand, if you have a report where you want to check and source every factual claim that it references you may want to generate a workflow which has one agent identify all of the factual claims and then spin off a subagent to check each one in-detail. You could also have a verification agent check the source subagent to make sure its source is high quality.
## Sorting

You may have a list of items that you want to sort by some qualitative measurement that you believe that Claude Code is good at evaluating, for example: support tickets sorted by severity of the bug. But if you try to sort 1000+ rows in one prompt, quality degrades and it won't fit in context. Instead run a tournament, a pipeline of pairwise-comparison agents (comparative judgment is more reliable than absolute scoring), or bucket-rank in parallel then merge. Each comparison is its own agent, so the deterministic loop holds the bracket and only the running order stays in context.
## Memory and rule adherence

If you have a particular set of rules that you find Claude misses or struggles with, even when put into the CLAUDE.mds, create a workflow with a list of rules that must be checked by verifier agents—one verifier per rule. Creating a skeptic persona subagent to review the rules to make sure they are in line will help avoid too many false positives.
The reverse direction works too: mine your recent sessions and code review comments for corrections you keep making, cluster them with parallel agents, adversarially verify each candidate (would this rule have prevented a real mistake?), and then distill the survivors back into a CLAUDE.md.
## Root-cause investigation
Debugging works best when you come up with several independent hypotheses and test them, but if you’re only using one context window, Claude can run into self-preferential bias.
A workflow can structurally prevent this by spinning up agents to generate hypotheses from disjoint evidence. For example, separate agents for logs, files, and data. Each hypothesis can then face a panel of verifiers and refuters.
This isn't just for code. Workflows can be used for sales (why did sales drop in March?), data engineering (why did this pipeline fail?), or any post-mortem exercise.
## Triaging at scale

Every team has a support queue, bug reports, or some other backlog that cannot be fully processed by humans.
A triage workflow classifies each item, dedupes against what's already tracked, and takes action. This could mean attempting the fix or escalating to a human user.
A useful pattern for triage workflows is quarantine. This involves barring the agents that read untrusted public content from taking high-privilege actions, which are instead done by the agents in charge of acting on the information.
Pair triage workflows with /loop to have Claude do this continuously.
## Exploration and taste
Workflows can be useful when exploring different approaches to a solution, especially when it is taste based, like design or naming, and would benefit from a rubric.
Try asking Claude to explore a bunch of solutions, and give a review agent a rubric for what a good solution looks like. The task is complete when the review agent feels like it has met the criteria. Solutions can also be ordered or selected via a tournament based on the rubric.
## Evals
You can run lightweight evals for particular tasks by spinning off separate agents in a worktree and then spinning off comparison agents to compare and grade the specific outputs against a rubric. For example, evaluating and then refining a skill you’ve created against a particular criteria.
## Model and intelligence routing
Create a classifier agent tuned to your tasks that decides which model to use. This can be helpful when your task will involve many tool calls and conducting research prior to execution can identify the best model for the job.
For example, the best model for the task “explain how the auth module works” depends on how many files in the auth module there are and the shape of the codebase. A classifier agent can do this research and then route to Sonnet or Opus based on the expected complexity of the task.
## When not to use dynamic workflows
Workflows are new. While there are many use cases where it will create outsized results, they are not needed for every task and may end up using significantly more tokens.
It’s best to use workflows creatively to push Claude Code in ways that you haven’t previously. For regular coding tasks, try and ask yourself does it really need more compute? For example, most traditional coding tasks do not need a panel of 5 reviewers.
# Tips for building dynamic workflows
Prompting
Detailed prompting, using the specific techniques we described above, for dynamic workflows creates the best results.
Workflows are not just for large tasks. You can prompt the model to use a “quick workflow.” For example, you can create a quick adversarial review of an assumption.
Combine with /goal and /loop
When using workflows that can be repeated, for example triage, research, or verification, pair them with /loop to be run at regular intervals, and /goal to set a hard completion requirement.
Token usage budgets
You can set explicit token usage budgets for dynamic workflows to limit how many tokens a task uses. You can prompt it with a budget like: “use 10k tokens,” which will set the cap.
Saving and sharing dynamic workflows
You can save workflows by pressing “s” in the workflow menu. You can check these into ~/.claude/workflows or distribute them via a skill.

To share them via a skill, put your JavaScript workflow files in the skill and folder and reference them in the SKILL.MD. To allow for more flexibility, you may want to prompt Claude to think of the workflows in the skill as a template instead of a script that needs to be run verbatim.

## A whole new world
Workflows are a helpful new way to extend Claude Code. I encourage you to think of this as a starting point, there's still much to discover in how to use them best. Let us know what you find.
Thariq Shihipar and Sid Bidasaria (@sidbid) are members of technical staff at Anthropic, working on Claude Code.
## 相关链接
- [Thariq](https://x.com/trq212)
- [@trq212](https://x.com/trq212)
- [451K](https://x.com/trq212/status/2061907337154367865/analytics)
- [dynamic workflows](https://code.claude.com/docs/en/workflows)
- [harness](https://code.claude.com/docs/en/glossary#agentic-harness)
- [Research](https://support.claude.com/en/articles/11088861-using-research-on-claude)
- [security analysis](https://support.claude.com/en/articles/11932705-automated-security-reviews-in-claude-code)
- [agent teams](https://code.claude.com/docs/en/agent-teams)
- [Code Review](https://code.claude.com/docs/en/code-review)
- [available on the Claude Blog](https://claude.com/blog/a-harness-for-every-task-dynamic-workflows-in-claude-code)
- [#incidents](https://x.com/search?q=%23incidents&src=hashtag_click)
- [subagents](https://code.claude.com/docs/en/sub-agents)
- [Claude Opus 4.8](https://www.anthropic.com/news/claude-opus-4-8)
- [Bun](https://bun.com/)
- [Jarred’s X thread](https://x.com/jarredsumner/status/2060050578026189172)
- [CLAUDE.md](http://claude.md/)
- [SKILL.MD](http://skill.md/)
- [@sidbid](https://x.com/@sidbid)
- [Upgrade to Premium](https://x.com/i/premium_sign_up)
- [4:26 AM · Jun 3, 2026](https://x.com/trq212/status/2061907337154367865)
- [451.3K Views](https://x.com/trq212/status/2061907337154367865/analytics)
- [View quotes](https://x.com/trq212/status/2061907337154367865/quotes)
---
*导出时间: 2026/6/3 09:07:15*
---
## 中文翻译
# 每项任务的利器:Claude Code 中的动态工作流
**作者**:Thariq
**日期**:2026-06-02T20:26:32.000Z
**来源**:[https://x.com/trq212/status/2061907337154367865](https://x.com/trq212/status/2061907337154367865)
---

上周,我们在 Claude Code 中发布了动态工作流功能。Claude 现在可以即时编写自己的工具,专门针对当前的任务进行定制构建。
虽然默认的 Claude Code 工具是为编码而构建的,但它对许多其他类型的任务也很有用,因为事实证明,许多任务类似于编码任务。但是,对于某些类别的任务,为了实现最佳性能,我们必须在 Claude Code 之上构建自定义工具,例如研究、安全分析、代理团队或代码审查。
工作流允许您动态创建工具,使 Claude 能够在 Claude Code 内部原生地解决所有这些问题,甚至更多。您还可以与他人共享和重用这些工作流。
在本文中,我将介绍我使用工作流的初步体验和心得,以便您能够充分利用它们。
也就是说,最佳实践仍在发展中!动态工作流通常会消耗更多的 token,因此请仔细考虑何时以及如何使用它们。
注意:这篇文章也发布在 Claude 博客上
## 示例提示
在深入探讨技术细节之前,我想先通过一些示例提示来启发您思考工作流的可能性:
- “这个测试大约每运行 50 次就会失败 1 次。设置一个工作流来重现它,形成假设,并在工作树中以对抗方式测试这些假设 /goal 在某个假设成功之前不要停止。”
- “使用工作流,回顾我过去 50 次的会话,从中挖掘我一直在进行的修正,并将重复出现的问题转化为 CLAUDE.md 规则。”
- “使用工作流深入挖掘 Slack 过去六个月中的 #incidents(事故),找出尚未有人创建工单的反复出现的根本原因。”
- “拿我的商业计划书来,运行一个工作流,让不同的代理分别从投资者、客户和竞争对手的角度对其进行批判。”
- “这里有一个包含 80 份简历的文件夹,使用工作流对它们进行后端职位的排名,并对前十名进行复核。使用 AskUserQuestion 工具面试我,以根据评分标准进行评估。”
- “我需要给这个 CLI 工具起个名字。使用工作流头脑风暴一系列选项,并举办一场锦标赛来选出前 3 名。”
- “使用工作流将我们的 User 模型重命名为 Account。”
- “通读我的博客文章草稿,使用工作流对照代码库验证每一个技术主张,我不想发布任何错误的内容。”
## 动态工作流的工作原理
动态工作流会执行一个 JavaScript 文件,其中包含一些特殊的函数,用于帮助生成和协调子代理:

动态工作流还包括标准的 JavaScript 函数,如 JSON、Math 和 Array,以帮助处理数据。
特别有用的一点是,动态工作流可以决定代理使用哪些模型,以及子代理是否在自己的工作树中运行,从而允许 Claude 选择所需的智能级别和隔离程度。
如果工作流被中断(例如因用户操作或退出终端),恢复会话将使工作流从中断的地方继续执行。
## 为什么需要动态工作流
当您要求默认的 Claude Code 工具执行任务时,它需要在同一个上下文窗口中进行规划和执行。对于许多编码任务,这非常有效,但在长期的、大规模并行和/或高度结构化的对抗性任务中,它有时可能会失效。
这是因为 Claude 在单个上下文窗口中处理复杂任务的时间越长,就越容易受到几种特定故障模式的影响:
- **代理惰性**:指 Claude 在完成特别复杂的多部分任务之前停止工作,在取得部分进展后宣布任务完成,例如在安全审查中只解决了 50 个项目中的 20 个。
- **自我偏好偏差**:指 Claude 倾向于偏爱自己的结果或发现,尤其是在被要求根据标准对其进行验证或判断时。
- **目标漂移**:指在多轮对话中逐渐失去对原始目标的忠实度,尤其是在经过压缩之后。每个总结步骤都是有损的,边缘情况的要求或“不要做 X”等约束细节可能会丢失。
创建工作流可以通过协调具有各自上下文窗口和专注、独立目标的独立 Claude 实例来帮助对抗这些问题。
## 动态工作流与静态工作流
您之前可能使用 Claude Agent SDK 或 `claude -p` 创建过静态工作流,以协调多个 Claude Code 实例。
但由于静态工作流需要适用于所有边缘情况,它们通常比较通用。有了 Claude Opus 4.8 和动态工作流,Claude 现在已经足够智能,可以编写专为您的用例量身定制的自定义工具。

# 使用动态工作流时的实用模式
您只需要求 Claude 创建一个工作流,或者使用触发词“ultracode”来确保 Claude Code 创建工作流,即可开始使用动态工作流。
但是,建立一个关于动态工作流如何工作的心智模型,将有助于您理解何时使用它们,以及如何通过提示引导 Claude。
Claude 在构建工作流时可能会使用并组合几种常见模式:

**分类与行动**
使用分类代理来决定任务的类型,然后根据任务路由到不同的代理或行为。或者,在最后使用分类器来确定输出。
**分发与综合**
将任务拆分为许多较小的步骤,在每个步骤上运行一个代理,然后综合这些结果。这对于有大量较小步骤,或者每个步骤都受益于自己干净的上下文窗口以避免干扰或交叉污染的情况特别有用。综合步骤是一个屏障——它等待所有分发的代理完成,然后将其结构化输出合并为一个结果。
**对抗验证**
对于每个生成的代理,运行一个单独的生成代理,根据标准或条件对抗性地验证其输出。
**生成与过滤**
针对某个主题生成大量想法,然后根据标准或验证进行过滤,去重并仅返回质量最高、经过验证的想法。
**锦标赛**
不是分配工作,而是让代理竞争。生成 N 个代理,每个代理尝试使用不同的方法完成相同的任务。然后,提示或模型通过评判代理以成对的方式评判结果,直到选出获胜者。
**循环直到完成**
对于工作量未知的任务,循环生成代理,直到满足停止条件(没有新发现,或日志中不再有错误),而不是执行固定次数的遍历。
# 用例
创造性地思考何时以及如何要求 Claude Code 创建动态工作流。我发现工作流有时甚至对非技术工作也更有用。

## 迁移和重构
Bun 使用工作流从 Zig 重写为 Rust。您可以在 Jarred 的 X 帖子中阅读更多关于此的内容。
关键在于将任务分解为一系列需要操作的步骤,例如调用点、失败的测试、模块等。为工作树中的每个修复生成一个子代理来进行修复,然后让另一个代理进行对抗性审查,并合并它们。考虑告诉代理不要使用资源密集型命令,以便您可以最大限度地并行化,而不会耗尽机器上的资源。
## 深度研究
我们在 Claude Code 中发布了一个深度研究技能,它使用动态工作流。具体来说,它会分发网络搜索、获取来源、对抗性地验证其主张,并综合一份带引用的报告。
但是,您不仅可以将这种研究用于网络搜索。例如,要求 Claude 根据 Slack 中的上下文编写状态报告,或者通过深入探索代码库来研究某个功能是如何工作的。
## 深度验证

另一方面,如果您有一份报告,想要检查并引用其引用的每一个事实主张,您可能希望生成一个工作流,让一个代理识别所有事实主张,然后生成一个子代理来详细检查每一个主张。您还可以让一个验证代理检查来源子代理,以确保其来源是高质量的。
## 排序

您可能有一个项目列表,想要按某种您认为 Claude Code 擅长评估的定性测量进行排序,例如:按错误的严重程度排序的支持工单。但是,如果您尝试在一个提示中对 1000 多行进行排序,质量会下降,而且无法放入上下文中。相反,可以运行锦标赛,成对比较代理的流水线(比较判断比绝对评分更可靠),或者并行进行桶排序然后合并。每次比较都是它自己的代理,因此确定性循环会控制整个流程,只有执行顺序保留在上下文中。
## 记忆和规则遵守

如果您有一套特定的规则,发现 Claude 经常遗漏或难以遵守,即使将它们放入 CLAUDE.md 中也是如此,请创建一个包含规则列表的工作流,这些规则必须由验证代理检查——每个规则对应一个验证代理。创建一个怀疑者人设的子代理来审查规则,以确保它们一致,这将有助于避免过多的误报。
反向操作也有效:挖掘您最近的会话和代码审查评论,找出您一直在进行的修正,使用并行代理对其进行聚类,对抗性地验证每个候选者(这条规则本可以防止一个真实的错误吗?),然后将幸存者提炼回 CLAUDE.md。
## 根本原因调查
当您提出几个独立的假设并对其进行测试时,调试效果最好,但如果您只使用一个上下文窗口,Claude 可能会遇到自我偏好偏差。
工作流可以通过生成代理从不相交的证据中生成假设,从而在结构上防止这种情况。例如,分别为日志、文件和数据设置不同的代理。然后,每个假设都可以面对一组验证者和反驳者。
这不仅仅适用于代码。工作流可用于销售(为什么三月份销量下降了?)、数据工程(为什么这个管道失败了?)或任何事后分析工作。
## 大规模分类

每个团队都有支持队列、错误报告或其他一些人类无法完全处理的积压工作。
分类工作流对每个项目进行分类,针对已跟踪的内容进行去重,并采取行动。这可能意味着尝试修复或升级给人类用户。
分类工作流的一个有用模式是隔离。这包括禁止读取不受信任的公共内容的代理执行高权限操作,这些操作由负责根据信息采取行动的代理完成。
将分类工作流与 /loop 结合,让 Claude 连续执行此操作。
## 探索和品味
在探索解决方案的不同方法时,工作流会很有用,特别是当基于品味时,例如设计或命名,并且受益于评分标准。
尝试要求 Claude 探索一系列解决方案,并给审查代理一个关于好解决方案的标准。当审查代理认为满足标准时,任务即完成。也可以根据标准通过锦标赛对解决方案进行排序或选择。
## 评估
您可以通过在工作树中生成单独的代理,然后生成比较代理来根据标准比较和评估特定输出,从而为特定任务运行轻量级评估。例如,根据特定标准评估然后优化您创建的技能。
## 模型和智能路由
创建一个针对您的任务调整的分类代理,来决定使用哪个模型。当您的任务将涉及许多工具调用,并且在执行前进行研究可以确定最佳模型时,这会很有帮助。
例如,“解释身份验证模块是如何工作的”这一任务的最佳模型取决于身份验证模块中有多少文件以及代码库的形状。分类代理可以进行这项研究,然后根据任务的预期复杂性路由到 Sonnet 或 Opus。
## 何时不应使用动态工作流
工作流是新增功能。虽然有许多用例会产生显著的成效,但并非每个任务都需要它们,并且可能会消耗更多的 token。
最好创造性地使用工作流,以以前没有过的方式推动 Claude Code。对于常规编码任务,试着问问自己,真的需要更多的计算能力吗?例如,大多数传统的编码任务并不需要 5 位审查人员的评审小组。
# 构建动态工作流的技巧
**提示**
为动态工作流进行详细的提示,使用我们上面描述的特定技巧,可以产生最佳结果。
工作流不仅适用于大型任务。您可以提示模型使用“快速工作流”。例如,您可以对假设进行快速的对抗性审查。
**结合 /goal 和 /loop**
当使用可以重复的工作流(例如分类、研究或验证)时,将它们与 /loop 结合以定期运行,并与 /goal 结合以设置严格的完成要求。
**Token 使用预算**
您可以为动态工作流设置明确的 token 使用预算,以限制任务使用的 token 数量。您可以使用诸如“使用 10k token”的预算进行提示,这将设置上限。
**保存和共享动态工作流**
您可以通过在工作流菜单中按“s”来保存工作流。您可以将它们存入 ~/.claude/workflows 或通过技能进行分发。

要通过技能共享它们,请将您的 JavaScript 工作流文件放入技能和文件夹中,并在 SKILL.MD 中引用它们。为了允许更多的灵活性,您可能希望提示 Claude 将技能中的工作流视为模板,而不是需要逐字运行的脚本。

## 一个全新的世界
工作流是扩展 Claude Code 的一种有用的新方式。我鼓励您将此视为一个起点,关于如何最好地使用它们,仍有待发现。请让我们知道您的发现。
Thariq Shihipar 和 Sid Bidasaria (@sidbid) 是 Anthropic 的技术人员,从事 Claude Code 的工作。