Press "Enter" to skip to content

Anthropic将Messages API迁移至Rust:每请求CPU用量下降约70%

Anthropic于2026年11月25日发表详细文章,说明其如何把Messages API从Python迁移到Rust,完成后的服务每请求CPU用量比被替换的Python版本低约70%。Messages API是进入Claude的主要通道,客户通过它访问模型,Anthropic自家的Claude Code和Cowork等产品也通过它工作。Anthropic表示,该接口的流量从2024年初到2026年秋季增长了约1800倍。

这项服务始于2023年,当时只是一个简单的Python程序,一位当时的工程师形容它基本上只是一个请求转发器。到2025年底,每个请求都要经过验证、转换成提示、完成分词,并由安全分类器筛查,每个回复在流式返回时都要被分拣为文本、思考和工具调用。Python在其中每一步都增加了开销。

团队先尝试了谨慎路线。他们只用Rust重写了输出解析代码,再从Python中调用,CPU用量下降了约8%到10%。但每个词元随后要三次跨越两种语言之间的边界,Anthropic认为增加的复杂性不值得这点节省。完整迁移成为剩下的选项,而反对完整迁移的通常理由已经减弱。到2026年5月,Anthropic表示,Claude编写了公司合并代码的80%以上,Claude用约一周时间产出了接口核心请求路径的第一个Rust版本。

事实证明,写代码是容易的部分,难的部分是证明Rust版本在真实流量上的行为与Python版本一致。现有测试套件包含约700个集成测试,Anthropic发现依赖它有两个问题。测试与Python实现耦合,会随代码一起变化,对移植后测试的审计发现了100多处被削弱的断言。测试本身也不完整,仅20个二元选项就构成100多万种组合,Anthropic计算,单个模型以每项1秒计需要12天,还没有计算提示的变化。

其下还有一个更深的问题。迁移进行期间,Python接口一直在变化,因为新模型和新功能持续发布,正确的定义是一个移动目标。Claude的回复本身具有不确定性,两个不同输出并不必然意味着存在缺陷。团队也不能读取客户请求,因此不能直接查看两个版本在哪里出现分歧。本站处理敏感信息的报道原则与此相通,近期的莱蒂西亚王后接见轮椅手球世界冠军同样只依据已确认的公开事实写作。

Anthropic构建的答案是把生产接口本身用作检查器。团队让每个模型调用只运行一次,然后把模型的输出分别通过Python路径和Rust路径重放,比较调用前后每个阶段。系统记录的不是请求文本,而是每处差异的形状,包括差异出现的步骤、在提示中的起始位置和大小。Anthropic把这种组合称为指纹,并发现一个缺陷往往会在它影响的每个请求上留下相同的指纹,于是差异可以被分组和计数,而不是逐条阅读。

早期数字说明了这项工作的必要性。Rust接口在4月通过了约98%的测试,但在生产流量上只有约四分之一的输入能够匹配。修复差异随后变成了一个智能体工作流,脚本每10到30分钟运行一次,按类型分组新的差异记录,只有出现新类型时才提醒一个协调Claude智能体。协调者为每个新类型指派一个工作智能体,并在一个工程师可以跟进和引导的共享Slack频道中开设讨论串。

工作智能体在三个循环中运作。快速的内层循环为每个智能体提供同时包含两套代码的沙箱,本地检查在一秒内运行且不调用模型。较慢的外层循环针对生产环境验证拟议修复。记忆循环记录错误,让后续智能体可以从中学习。由于智能体只能看到指纹而看不到背后的请求,它必须自己编写测试请求,复现差异,再修改Rust代码,直到两个版本匹配。

迁移在没有让服务下线、也没有冻结Python一侧功能开发的情况下上线。除CPU节省外,Anthropic还报告内存用量下降约80%,首个词元的中位时间下降约20%。文章最后落在生产数字上,文中所述2026年9月的切换完成后,Rust接口已经是Claude流量的实际通道。

本站本周的其他报道还包括卢森堡大公接受五国大使国书,读者可对照阅读相关背景。

English

Anthropic published a detailed account on November 25, 2026 of how it moved the Messages API from Python to Rust, and the finished service uses about 70 percent less CPU per request than the Python version it replaced. The Messages API is the main route into Claude for customers and for Anthropic’s own products, including Claude Code and Cowork. Anthropic says traffic grew roughly 1,800 times from early 2024 to the fall of 2026.

The service began in 2023 as a simple Python program described at the time as basically a request forwarder. The team first rewrote only the output parsing code in Rust and called it from Python, which cut CPU use by about 8 to 10 percent, but each token then crossed the language boundary three times. By May 2026, Anthropic says, Claude was writing more than 80 percent of the code the company merged, and Claude produced a first version of the core request path in about a week.

Proving the Rust version behaved like the Python version was the hard part. The existing suite held about 700 integration tests, an audit found more than 100 weakened assertions, and twenty binary options alone make over a million combinations. The Python API also kept changing during the migration, model replies are nondeterministic, and the team could not read customer requests.

The answer was to use the production API itself as the checker. The team ran each model call once, then replayed the output through both paths, comparing every stage and recording the shape of each difference rather than request text. Anthropic calls that combination a fingerprint. The Rust API passed about 98 percent of its tests in April, yet only about a quarter of inputs matched on production traffic.

Fixing the differences became an agent workflow. A script ran every 10 to 30 minutes, grouped new difference records by kind, and alerted a coordinating Claude agent only when a new kind appeared. Workers used a fast inner loop with local checks under a second, a slower outer loop against production, and a memory loop that recorded mistakes.

The migration shipped without taking the service offline and without freezing feature development. Alongside the CPU saving, Anthropic reports about 80 percent less memory use and a cut of about 20 percent in median time to first token. The Rust API is already the live path for Claude traffic as of the September 2026 cutover the post describes.

本报道的其他版本也刊登于News Today World, Manhattan Mogul, American Pulse Journal, American Morning Herald, Revista Minutos, Le Miroir Francais。

Schaumburg-Lippe News Organization
More from 最新消息More posts in 最新消息 »
More from 科技More posts in 科技 »

Be First to Comment

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注