将动态工作流的 Claude 代码代币消耗量降低 80%

2 分•作者: gojkoa•20 天前
几天前偶然发现了一个非常有趣的东西,我想对于那些使用 Claude Code 运行动态工作流的人来说,这可能也很有趣。工作流脚本本身是 JavaScript。Claude Code 了解这种格式,并且可以创建一个工具来组合工作流,而不是每次都通过 LLM。 我实现了这一点,并且通过一些额外的调整,将工作流执行和监控的 token 消耗降低到了之前的约 20%,显著加快了速度(我们的大多数工作流以前需要 5-6 小时,现在只需要 20-40 分钟)。 我们让 Claude 分析了过去几周的工作流(工作流执行在磁盘上,位于 `~/.claude/projects`),并识别出它们之间的共同点。结果发现,实际上只有三种类型的工作流步骤,但以无数种临时的方式实现。 * **代码变更代理/实现者/工作者** - 负责执行计划中的任务;这些代理可以并行运行。 * **检查门控代理** - 负责评估实现者代理所做的工作,并处理不应并行执行的评估。 * **最终检查** - 负责部署、运行 API 测试等。 我还发现,Claude 可以从标准的“代理”(位于 `./claude/agents`)组合工作流,而无需每次都由 LLM 编写工作流代理的提示;因此,我要求它找出共同点/可变性,它编写了 3 个代理脚本,后来我们将其精简为两个: * **workflow-worker** - 接收计划的一部分,并拥有非常严格的权限,只能为其计划/文件边界的自身部分编写和运行测试,不能修复其他代理可能正在做的事情,更不能运行整个测试套件。 * **workflow-gate** - 接收一组并行工作流代理的结果,根据 git 中的更改运行串行验证(例如,如果任何 www 文件发生更改,则运行 Web 测试;如果任何后端文件发生更改,则运行 API 测试等)。以前这需要每次都由 LLM 自定义编写,但我们将其移至一个带有两个目标的 Makefile,因此 `make workflow-serial-gate` 会检查 git 树并针对更改运行测试,`make workflow-final-gate` 会在所有内容上运行干净的测试,部署到暂存环境,并运行部署后测试。 由于可变性已移至 Makefile,这两个代理的定义都变成了标准的 100 行 Markdown,LLM 无需每次都编写。 接下来,由于代理定义已标准化,我们让 Claude 编写了一个工具,该工具可以读取计划文件中的步骤和依赖关系,并直接编写工作流 JavaScript。以前,对于复杂的计划,这可能需要 10 分钟才能返回结果,现在只需要一秒钟。 由于工作流代理的提示是静态的,并且只编写一次,因此在编写它们时不会消耗 token。由于哪些测试何时运行的整个编排都在 Makefile 中,并且基于 git 更改,因此在这方面也不会消耗 token。由于工作者代理接收计划和步骤编号,因此在告诉它们做什么时不会消耗 token(除了它们读取特定步骤,但这不可避免)。Token 几乎只消耗在创建计划、狭窄的实现任务和上下文修复问题上。 当一个命名代理作为工作流的一部分运行时,传递给预工具使用钩子的上下文包括代理的名称(例如,workflow-worker、workflow-gate),因此我们能够显著地限制它们。例如,workflow-worker 不允许运行 make 或整个测试套件,并会收到一条消息,指示将其委托给 gate 代理。 最终结果令人惊叹,工作流运行速度大大加快,消耗的 token 也比以前少得多。Claude 几乎独立完成了所有这些工作,因此我强烈建议您在运行动态工作流时尝试一下。如果有人感兴趣,我很乐意提供更多信息。
查看原文
Discovered something very interesting by chance a few days ago, and thought it might be interesting for other people running dynamic workflows with claude code. The workflow scripts themselves are javascript. Claude code knows the format and it can make a tool that composes workflows instead of going through LLM each time.<p>I got it to do that, and with a few more tweaks it got the token spend for workflow execution and monitoring down to about 20% of what it was before, significantly speeding it up (most of our workflows used to take 5-6 hours, they now take 20-40 minutes).<p>We got Claude to analyze the workflows from the past few weeks (the workflow execution is on disk, in ~&#x2F;.claude&#x2F;projects) and identify commonalities between them. It turned out to be effectively 3 kinds of workflow steps, done in a million ad-hoc ways.<p>- a code change agent&#x2F;implementer&#x2F;worker - this takes a task from the plan and does it; such agents can run in parallel<p>- a check gate agent - this tries to figure out what the implementer agents did and evaluates things that should not be done in parallel<p>- the final check, which deploys, runs api tests etc.<p>It also turns out you that Claude can compose workflows out of standard &quot;agents&quot; (from .&#x2F;claude&#x2F;agents), not writing the prompts for workflow agents by LLM each time; so I asked it to figure out commonalities&#x2F;variabilities, and it wrote 3 agent scripts, which we later reduced to two:<p>- workflow-worker - gets a piece of the plan and very tight permissions to only write and run tests for it&#x27;s own part of the plan&#x2F;file boundary, and not fix anything that others might be doing, and definitely not run the whole test suite<p>- workflow-gate - gets the results from parallel workflow agents in a group, runs serial verifications based on what changed in git (e.g. if any www files changed run web tests; if any backend files changed, run api tests etc. This used to be custom-written by LLM each time but we moved it to a makefile with two targets, so make workflow-serial-gate checks the git tree and runs tests for changes, make workflow-final-gate runs clean tests on everything, deploys to a staging environment, runs post-deployment tests<p>as the variability moved into the makefile, both of these agent definitions became standard 100-line markdown, that the LLM did not need to write each time.<p>next, since the agent definitions are standardized, we got claude to actually write a tool that would read out the plan file with steps and dependencies, and write the workflow javascript directly. previously it used to take 10 minutes for something like this to come back for complex plans, now it takes a second.<p>Since the workflow agent prompts are static and written once, no tokens get spent writing them. since the whole choreography of which tests to run when is in the makefile, based on git changes, no tokens get spent on that. since worker agents are getting the plan and a step number, no tokens are spent telling them what to do (besides them reading the specific step, but this is unavoidable). Tokens get spent pretty much only on creating the plan, narrow implementation tasks and contextually fixing issues.<p>When a named agent runs as part of the workflow, the context passed to pre-tool use hooks includes the name of the agent (e.g. workflow-worker, workflow-gate) so we were able to constrain them significantly. For example, workflow-worker is not allowed to run make, or run the entire test suite, with a message to delegate that to the gate agent.<p>The end result is amazing, workflows run much much faster and spend far fewer tokens than before. Claude pretty much wrote the whole thing itself, so I definitely recommend this as an experiment if you run dynamic workflows. Happy to provide any additional info if people are interested.