Anthropic 用 Claude 把 Messages API 由 Python 遷到 RustAnthropic used Claude to migrate the Messages API from Python to Rust
Anthropic 11-25 公布工程遷移結果:Claude 約一週寫出 Rust 版核心請求路徑,9 月公開流量無感切換,期間零停機。每請求 CPU 減約 70%、記憶體減逾 80%、首 Token 中位延遲縮短約 20%,支撐 2024 年初以來約 1,800 倍流量增長。驗證靠影子流量與差異指紋,不讀取客戶原文。Anthropic announced the results of the engineering migration from November 25: Claude wrote the core request path in Rust in about a week, with a seamless traffic switch in September and zero downtime during the period. Each request's CPU usage decreased by about 70%, memory usage dropped by over 80%, and the median latency for the first token was reduced by about 20%, supporting approximately 1,800-fold traffic growth since early 2024. Validation relied on shadow traffic and differential fingerprints, without reading the original customer text.
Many problems have a generator–verifier gap: it’s often far easier to verify that a solution is correct than to come up with one. Sudoku is a classic example. It’s simple to check the solution (just count the numbers in each row, column, and box), but finding that solution can take a long time. Many ambitious Claude projects have relied on this gap: formalizing proofs in mathematics or migrating large, well-tested codebases are tasks Claude can take on as long as it has a reliable way to verify its work.
But as Claude has become more capable, it’s begun to take on projects in live systems, where generating the work is easier than verifying it. In these systems, users often rely on edge cases and quirks that fall outside the test suite, and it’s hard to write a definition of “correct” that covers everything. Without a reliable way to check Claude’s work, the migration of a live system is hard to trust—however quickly Claude can generate the code for it.
We ran into this problem when we migrated our Messages API from Python to Rust. This API is the primary way that customers—as well as our own apps, like Claude Code and Cowork—reach Claude. From early 2024 to fall 2026, API traffic grew roughly 1,800 times. Rust would allow us to use far less CPU per request. Claude wrote a first version of the API’s core request path in about a week. But building trust in the migration required us to find a way to verify that our Rust API was at parity with our production API. This proved hard for several reasons, including that the Python API continued changing throughout the migration, so parity was a moving target; Claude can reply nondeterministically to the same request, so a difference between two outputs didn’t necessarily mean a bug; and we couldn't read customer requests, so we weren't able to easily see where the two versions diverged.
In this post, we share how we turned the production Messages API itself into a verifier—and how that helped us test the new Rust version on production traffic, find where it differed, and automatically dispatch agents to fix those differences. This allowed us to ship an API on Rust that uses about 70% less CPU per request and cuts median time to first token by about 20% without ever taking the service offline or freezing feature development.
Why migrate?
We built the Messages API in 2023 and wrote it in Python, which was widely used internally for much of our research code. At first, it was simple. The Messages API passed requests to the model and sent back what the model wrote. (One engineer at the time called it “basically a request forwarder.”) But as the Messages API became more capable and deeply integrated with our codebases, it also developed shared dependencies with Python libraries that were owned by our research and inference teams, which made it hard to move. If we ever wanted to move to another language, it would mean building logging, metrics, and tools all over again.
This calculus gradually began to change. By late 2025, every Messages API request had to be validated, converted into the prompt, tokenized, and screened by our safety classifiers. As each reply arrived, the stream of tokens had to be sorted into text, thinking, and tool calls, and Python added overhead to every one of those steps each time it created an object, freed memory, or handed its work to the next task. Traffic eventually grew far beyond what the initial Python service was built for (Figure 1), and we quickly needed to find a way to make the service much more efficient.
Incremental or all at once?
Full rewrites of large production systems are often approached cautiously, for at least two reasons. First, there is no guarantee that test suites cover 100% of production code behavior, and rewriting it is risky. Second, the sheer effort required often makes such work intractable. (In 2024, after one of our engineers suggested migrating to Go or Rust, another temporarily settled the question: “We are not rewriting the API in go or rust!”)
The standard advice if you’re looking for performance gains is thus to rewrite only the parts that are slowing the system down, so that’s where we started. In Rust, we rewrote the code that parses a model’s output and called it from Python. This reduced the API’s CPU usage by about 8% to 10%. However, incremental solutions often incur added complexity, and it’s important to make sure that incremental improvements justify those costs. In our case, the gains didn’t justify the added complexity. Each token now crossed between Python and Rust three times (Figure 2), creating new sources of overhead.
As Claude became increasingly capable, we began to question whether the usual reasons to avoid a full migration still held. The effort required to undertake the project was dropping fast: by May 2026, Claude was writing more than 80% of the code Anthropic merged. We wondered if Claude could now help us write code that would empirically capture every behavior and fix in the production system. But the question became: how could we verify that the behavior of our migration was correct? Verification, not generation, had become the bottleneck.
How we verified the migration
General approaches
To verify the migration, we first needed to define what “correct” meant. You can do this in a few ways (Figure 3). If you’re lucky, you can simply write a rule. A theorem in mathematics is a precise statement, and a proof assistant (like Lean) can check each step of a proof against a fixed set of rules. Other times—and more frequently in software engineering—you define “correct” with a set of examples, like tests. For instance, when Bun moved from Zig to Rust, its test suite was written in TypeScript, so the same tests could judge both versions. In our case, neither approach was sufficient, so we used the existing system as an oracle to test parity during the migration. In this case, our existing system (the Messages API) kept changing. We were releasing new models and shipping new features regularly, which made the definition of “correct” a moving target.
Tests
Tests were the easiest place for us to start, since we already had an existing set of about 700 integration tests. However, two problems limited our ability to trust them to verify the correctness of a migration as high stakes as this (Figure 4).
The tests were coupled to the Python implementation. An integration test sends a request, then checks the reply and what was sent to other services. Our tests ran the Python API inside the test’s process and mocked services, like billing and rate limiting, in Python’s memory. The tests had to change along with the code, and Claude helped with both. Now, a “pass” could mean that the code was right, or that the test had been changed in a way that let wrong code through. We saw this happen: when we audited the ported tests, we found more than 100 weakened assertions.
The tests were incomplete. The Messages API has dozens of features, and there are far too many configurations to test them exhaustively. Just 20 binary options make over a million combinations—that translates to 12 days of tests at a second each, for one model, before we even vary the prompts inside them. In practice, engineers test a subset that tries to approximate production, but that process inevitably misses cases. Some projects, like Bun, have an independent suite that’s extensive enough to judge a rewrite. Many production services don’t; what counts as correct depends as much on how they behave as on what’s in their tests or documentation.
Detecting parity with production
……(原文過長,此處截斷)
Many problems have a generator–verifier gap: it is often much easier to verify that a solution is correct than to come up with one. Sudoku is a classic example. It is simple to check the solution (just count the numbers in each row, column, and box), but finding that solution can take a long time. Many ambitious Claude projects have relied on this gap: formalizing proofs in mathematics or migrating large, well-tested codebases are tasks Claude can take on as long as it has a reliable way to verify its work.
But as Claude has become more capable, it has started taking on projects in live systems, where producing the work is easier than verifying it. In these systems, users often rely on edge cases and quirks that are outside the test suite, and it is difficult to define “correct” in a way that covers everything. Without a reliable way to check Claude’s work, migrating a live system is hard to trust—no matter how quickly Claude can generate the code for it.
We ran into this problem when we migrated our Messages API from Python to Rust. This API is the primary way that customers—as well as our own apps, like Claude Code and Cowork—reach Claude. From early 2024 to fall 2026, API traffic grew roughly 1,800 times. Rust would allow us to use far less CPU per request. Claude wrote a first version of the API’s core request path in about a week. But building trust in the migration required us to find a way to verify that our Rust API was at parity with our production API. This proved hard for several reasons, including that the Python API continued changing throughout the migration, so parity was a moving target; Claude can reply nondeterministically to the same request, so a difference between two outputs didn’t necessarily mean a bug; and we couldn't read customer requests, so we weren't able to easily see where the two versions diverged.
In this post, we share how we turned the production Messages API itself into a verifier—and how that helped us test the new Rust version on production traffic, find where it differed, and automatically dispatch agents to fix those differences. This allowed us to ship an API on Rust that uses about 70% less CPU per request and cuts median time to first token by about 20% without ever taking the service offline or freezing feature development.
Why migrate?
We built the Messages API in 2023 and wrote it in Python, which was widely used internally for much of our research code. At first, it was simple. The Messages API passed requests to the model and sent back what the model wrote. (One engineer at the time called it “basically a request forwarder.”) But as the Messages API became more capable and deeply integrated with our codebases, it also developed shared dependencies with Python libraries that were owned by our research and inference teams, which made it hard to move. If we ever wanted to move to another language, it would mean building logging, metrics, and tools all over again.
This calculus gradually began to change. By late 2025, every Messages API request had to be validated, converted into the prompt, tokenized, and screened by our safety classifiers. As each reply arrived, the stream of tokens had to be sorted into text, thinking, and tool calls, and Python added overhead to every one of those steps each time it created an object, freed memory, or handed its work to the next task. Traffic eventually grew far beyond what the initial Python service was built for (Figure 1), and we quickly needed to find a way to make the service much more efficient.
Incremental or all at once?
大规模生产系统的全面重写通常会被谨慎对待,至少有两个原因。首先,没有保证测试套件能够覆盖生产代码行为的100%,而重写它具有风险。其次,所需的巨大工作量通常使这类工作变得难以处理。(在2024年,当我们的一位工程师建议迁移到Go或Rust时,另一位工程师暂时解决了这个问题:“我们不会用Go或Rust重写API!”)
因此,如果你想提高性能,标准建议是只重写那些拖慢系统的部分,所以这就是我们的出发点。在 Rust 中,我们重写了解析模型输出的代码,并从 Python 调用它。这将 API 的 CPU 使用率降低了大约 8% 到 10%。然而,增量的解决方案通常会增加复杂性,因此确保增量改进值得这些成本是很重要的。在我们的案例中,收益并不足以抵消增加的复杂性。每个 token 现在在 Python 和 Rust 之间交叉三次(图 2),产生了新的开销来源。
As Claude became increasingly capable, we began to question whether the usual reasons for avoiding a full migration still applied. The effort required for the project was decreasing rapidly: by May 2026, Claude was writing more than 80% of the code that Anthropic merged. We wondered if Claude could now help us write code that would empirically capture every behavior and fix in the production system. But the question became: how could we verify that the migration's behavior was correct? Verification, not generation, had become the bottleneck.
How we verified the migration
General approaches
为了验证迁移,我们首先需要定义“正确”的含义。你可以通过几种方式做到这一点(见图3)。如果你够幸运,可以简单地写一条规则。数学中的定理是一个精确的陈述,而证明助手(如 Lean)可以根据固定的一套规则检查证明的每一步。其他时候——更常见于软件工程中——你可以通过一组示例来定义“正确”,比如测试。例如,当 Bun 从 Zig 转向 Rust 时,其测试套件是用 TypeScript 编写的,因此相同的测试可以评估两个版本。在我们的情况中,这两种方法都不够用,所以我们使用现有系统作为权威,在迁移过程中测试一致性。在这种情况下,我们的现有系统(消息 API)不断变化。我们定期发布新模型并推出新功能,这使得“正确”的定义成为一个不断变动的目标。
Tests
Tests were the easiest place for us to start, since we already had an existing set of about 700 integration tests. However, two problems limited our ability to trust them to verify the correctness of a migration as high stakes as this (Figure 4).
这些测试与 Python 实现紧密结合。一个集成测试会发送请求,然后检查回复以及发送给其他服务的内容。我们的测试在测试进程内运行 Python API,并在 Python 内存中模拟服务,例如计费和速率限制。测试必须随着代码的更改而更改,而 Claude 在这两方面都提供了帮助。现在,“通过”可能意味着代码是正确的,也可能意味着测试以某种方式被修改,从而让错误代码通过。我们确实看到了这种情况:当我们审计迁移过来的测试时,发现有超过 100 个断言被削弱。
测试是不完整的。Messages API 有数十个功能,而且配置选项太多,无法进行全面测试。仅 20 个二进制选项就有超过一百万种组合——这意味着对于一个模型,每个组合花一秒钟测试的话,需要 12 天的时间,更不用说还要在这些组合中改变提示了。实际上,工程师会测试一个尝试接近生产环境的子集,但这个过程不可避免地会遗漏一些情况。一些项目,比如 Bun,有一个独立的测试套件,足够全面,可以评估重写情况。许多生产服务没有;判断是否正确不仅取决于测试或文档中的内容,还取决于它们的实际表现。
Detecting parity with production
……(The original text is too long, truncated here)
原文出處:Source: Anthropic 官方 ↗