跳到主内容
@wquguru
精选88Cloudflare 博客(RSS)技巧与观点

Cloudflare 通过优化推理流程将 AI 代码审查成本降低 68%

Cutting our inference bill by 68% by being obsessive about optimization

原文
发到 X
推荐理由

Agent 工程落地的硬核复盘,详细拆解了如何利用前缀缓存和 Prompt 结构调整来大幅降低 Token 消耗,对做 Agent 的同学极具参考价值。

When we previously blogged about our AI code reviewer in April, an average review cost a palatable $1.19. By the end of August, that figure had shot up to about $2.46. We hadn't made any significant architectural changes: the coordinator was still handing work to the same set of specialist reviewers, and the same risk tiers decided how intensive of a review we gave.

今年四月我们首次发布关于 AI 代码审查员的博文时,平均每次审查成本仅为令人愉悦的 1.19 美元。到了八月底,这一数字飙升至约 2.46 美元。我们并未进行任何重大的架构变更:协调器仍在向同一组专家审查员分配工作,相同的风险等级也决定了我们审查的强度。

Around mid-September we tasked our agents with working out where those tokens were going and cutting them — aggressively. With code quality at high effort, the average review now costs just $0.77, down 68% from the $2.43 it averaged over our two most expensive weeks before we started. Our main goal was to cut costs, but as part of this work we also managed to optimize the reviewer, so that the median review now takes just 1m 32s instead of the 4m 09s it took before.

九月中旬左右,我们要求我们的智能体找出这些 token 的去向并大力削减它们。在保持代码质量高投入的情况下,现在平均每次审查的成本仅为 0.77 美元,相比开始优化前最昂贵的两周平均 2.43 美元下降了 68%。我们的主要目标是降低成本,但作为这项工作的一部分,我们还成功优化了审查员,使得中位数审查时间从之前的 4 分 09 秒缩短至现在的 1 分 32 秒。

About 40% of the savings came from changes and optimizations to the reviewer's code, and the rest from experimenting with which models we use and how much effort they put in.

约 40% 的节省来自对审查员代码的更改和优化,其余部分则来自对我们使用的模型及其投入努力程度的实验。

How a review doubled in cost

审查成本翻倍的原因

The chart below shows the cost of an average review on each weekday since March. It holds the mix of review tiers fixed, so a day with more large reviews doesn't skew the data.

下图显示了自三月以来每个工作日的平均审查成本。该图表固定了审查等级的混合比例,因此某天出现更多大型审查不会扭曲数据。

The cost per review steadily rose from $1.22 (March) → $1.44 (June) → $1.74 (July) and finally, to $2.32 (August). A small part of that is that more reviews landed in the most expensive tier (59% of reviews in March and then 65% in August). We believe there’s a simple answer for why: models are getting much better, and much more capable. Naturally, engineers are producing bigger, more complex changes which means more of them fall into our “full review” tier. We found that most of the cost increase actually came from model changes over those months, though.

每次审查的成本稳步上升:从 3 月的 1.22 美元 → 6 月的 1.44 美元 → 7 月的 1.74 美元,最终达到 8 月的 2.32 美元。其中一小部分原因是更多审查落入了最昂贵的等级(3 月有 59% 的审查属于该等级,而 8 月为 65%)。我们认为原因很简单:模型变得更好、能力更强。自然,工程师们正在生成更大、更复杂的变更,这意味着更多的变更落入我们的“完整审查”等级。不过,我们发现大部分成本增加实际上来自于这几个月间模型的变更。

At the start of August we moved every reviewer onto Anthropic models, which raised the cost per job from $1.38 to $2.03. We made this choice because we found that during our evals, Anthropic models performed better on this specific workload than other models.

八月伊始,我们将所有审查员迁移到了 Anthropic 模型上,这使得每项工作的成本从 1.38 美元上升至 2.03 美元。我们做出这一选择是因为在评估中发现,Anthropic 模型在这一特定任务上的表现优于其他模型。

Our internal standards reviewer, Codex, ended up on Claude Opus 4.7 due to a mix of performance and reliability. That took it from about $0.25 per review to $0.88 in August, which made it more expensive than the coordinator agent itself.

我们的内部标准审查员 Codex 由于性能和可靠性的综合考量,最终采用了 Claude Opus 4.7。这使得其每次审查的成本从约 0.25 美元上升至 8 月的 0.88 美元,甚至比协调器智能体本身还要昂贵。

The cache hit rate also increased, going from 85.7% in April to 97% by September, while the number of tokens per review more than tripled, from 0.91 million to 2.99 million. A high hit rate on three times as many tokens still costs a lot more, even with the low cache read cost.

缓存命中率也提升了,从4月的85.7%上升到9月的97%,而每次审查处理的token数量翻了三倍多,从91万增加到299万。即使缓存读取成本很低,处理三倍数量的token且保持高命中率仍然需要更高的成本。

What else has changed since our last post?

自我们上一篇文章以来,还有什么变化?

One of the biggest changes is that the reviewer now runs on OpenCode v2, which is a ground-up rewrite from v1. Before switching all reviews over from v1 to v2, we ran every review in every canary repo on both v1 and v2 in a comparative shadow mode. The two agreed on the approval decision in 98% of the merge requests we tested, so we moved everyone over in one go in early October and removed the v1 code. Comparing two days with the same models, full-tier reviews on v2 finish about 13% faster. We’re also currently experimenting with other ways to run our internal AI reviewer, including solely on Durable Objects using pi-durable. We’re hopefully going to have more on this coming soon, so keep an eye out!

最大的变化之一是审查程序现在运行在 OpenCode v2 上,这是从 v1 彻底重写而来的版本。在将所有审查从 v1 切换到 v2 之前,我们在每个金丝雀仓库(canary repo)中同时以对比影子模式运行 v1 和 v2 的审查。在我们测试的合并请求中,两者在98%的情况下对审批决定达成一致,因此我们在10月初一次性将所有人迁移过来并移除了 v1 代码。在相同模型的两个日期之间进行比较,v2 的全层级审查速度提高了约13%。我们目前还在尝试其他运行内部 AI 审查程序的方法,包括仅使用 pi-durable 在 Durable Objects 上运行。希望很快会有更多相关信息,敬请期待!

So, where were the tokens going?

那么,这些 token 都去了哪里?

Right before we started focusing on cost optimization, our AI review spend was split by: output tokens making up about 23%, cache writes cost 43%, and cache reads cost 34%. This means that most of the money was going on context the model had already seen or was about to see again.

就在我们开始专注于成本优化之前,我们的 AI 审查支出分布如下:输出 token 约占23%,缓存写入成本占43%,缓存读取成本占34%。这意味着大部分资金都花在了模型已经见过或即将再次看到的上下文上。

That makes complete sense when you look into a single session. The code quality reviewer made 16.6 large language model (LLM) calls per review on average and over those calls it wrote 72k tokens to the cache and read back 1.08m, because each call sends the whole conversation as context. A session's cost grows with both the number of calls and the size of the context, and over the last few months we had slowly increased both.

当你深入查看单个会话时,这完全说得通。代码质量审查员平均每次审查调用了16.6次大型语言模型(LLM),在这些调用中,它向缓存写入了72k个token,并从缓存中读取了108万个token,因为每次调用都会将整个对话作为上下文发送。一个会话的成本随着调用次数和上下文大小的增加而增长,而在过去的几个月里,我们逐渐增加了这两者。

It’s worth taking a few minutes to read about how Anthropic's prompt cache works. It works by caching parts of the prompt that have already been computed by the large language model (LLM). These are known as prefixes, because they only work on the messages at the start of the conversation, not mid way through or the end. This means that a future request can reuse an earlier prefix, up to the part of the conversation that changes from what’s been cached. In our case, the harness tools, system prompt and messages at the start of our AI reviewer sessions rarely change, so they’re a great candidate for prefix caching. A prefix is only stored where a cache breakpoint ends it. Writing to the cache costs 1.25 times the normal input price and reading costs 0.1 times, and an entry expires after five minutes unless something reads it.

花几分钟阅读一下 Anthropic 的提示词缓存(prompt cache)是如何工作的是值得的。它的工作原理是缓存大语言模型(LLM)已经计算过的提示词部分。这些部分被称为前缀(prefixes),因为它们仅适用于对话开头的消息,而不适用于中途或结尾的消息。这意味着未来的请求可以重用较早的前缀,直到对话中发生变化的那一部分为止。在我们的案例中,harness 工具、系统提示词以及 AI 审查员会话开头的消息很少发生变化,因此它们是前缀缓存的理想候选对象。只有当缓存断点(cache breakpoint)结束时,前缀才会被存储。写入缓存的成本是正常输入价格的 1.25 倍,读取成本是 0.1 倍,并且条目在五分钟后过期,除非在此期间有人读取了它。

That means we had two things to work on: make the first request of each session read from the cache instead of writing to it, and make each session use fewer calls.

这意味着我们有两个需要解决的问题:让每个会话的第一个请求从缓存中读取而不是写入缓存,以及让每个会话使用更少的调用次数。

Sharing a cached prefix between every repository

在每个仓库之间共享缓存的前缀

Beginning September 14, the reviewer recorded the token usage of each reviewer's first LLM call, plus a hash of the first system block exactly as it was sent to the provider.

从 9 月 14 日开始,审查员记录了每位审查员第一次 LLM 调用的 token 使用情况,以及发送给提供商的第一个系统块的确切哈希值。

The data showed that the first request of a session found a cached prefix only 14% to 26% of the time. Every session starts with the same tool definitions and the same agent prompt, between 11k and 21k tokens that are identical in every repository we review, so almost all of them should have been cache hits.

数据显示,会话的第一个请求仅在 14% 到 26% 的时间内找到了缓存的前缀。每个会话都以相同的工具定义和相同的代理提示词开始,这部分包含 11k 到 21k 个 token,在我们审查的所有仓库中都是完全相同的,因此几乎都应该命中缓存。

The problem ended up being in how OpenCode v1 constructed the system prompt: It built one block containing our agent prompt, followed by an environment section with the working directory and the date, and placed the only cache breakpoint at the end of that block. The shared part was never stored on its own, so a review in one repository could never reuse what a reviewer ran in another repository had written.

问题最终出在 OpenCode v1 构建系统提示词的方式上:它构建了一个包含我们代理提示词的块,后面跟着一个包含工作目录和日期的环境部分,并将唯一的缓存断点放置在该块的末尾。共享部分从未单独存储,因此在一个仓库中的审查无法重用另一个仓库中审查员写入的内容。

On September 16, we added a marker to the end of every agent prompt and a plugin hook that split the block in two at that marker. The first half now ended on bytes that every job shares, and got its own breakpoint. To keep it that way we had to remove anything repository-specific from in front of it, so the list of available skills is disabled and tool descriptions can't change between jobs.

9 月 16 日,我们在每个代理提示词的末尾添加了一个标记,并添加了一个插件钩子,在该标记处将块拆分为两部分。前半部分现在以每个作业共享的字节结束,并获得了自己的断点。为了保持这种状态,我们必须移除其前面的任何特定于仓库的内容,因此可用技能列表被禁用,且工具描述不能在作业之间发生变化。

The same day we also moved our internal standards (Codex) reviewer's reference index (a table of internal standards, a skill file and a list of reference files) into its prompt. Until then every session had spent its first three turns reading those files with tool calls. Its LLM calls per run decreased from 15.9 to 9.6, and its cost per run dropped from $1.12 to $0.72.

同一天,我们还将内部标准(Codex)审查员的参考索引(包含内部标准表、技能文件和参考文件列表)移入了其提示词中。在此之前,每次会话的前三轮都会通过工具调用读取这些文件。每次运行的 LLM 调用次数从 15.9 次降至 9.6 次,每次运行的成本也从 1.12 美元降至 0.72 美元。

Together these took the cost per review from $2.47 down to $2.02, and the drop was noticeable as soon as we kicked off the release.

综合来看,这使得每次审查的成本从 2.47 美元降至 2.02 美元,且在我们启动发布后,这种下降立竿见影。

Fewer calls, fewer dollars

调用更少,花费更省

The next set of changes went out on September 21 and 23. None of them were particularly impressive by themselves, but together they took about $0.20 off every review.

下一组变更于 9 月 21 日和 23 日推出。它们单独看都不算特别显著,但综合起来使每次审查的成本降低了约 0.20 美元。

更进一步:量化金融体系

看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力

进入量化体系 →

相似阅读

关联信息,但可能不是同一事件