Features

Anthropic moved its Messages API from Python to Rust. Writing the code was the easy part

Claude produced the first version in about a week. It passed 98% of tests and matched production behaviour on a quarter of real inputs.

Anthropic moved its Messages API from Python to Rust. Writing the code was the easy part
Photo: TechCrunch / Wikimedia Commons (CC BY 2.0)

Anthropic has rewritten its Messages API, the service through which customers and its own products such as Claude Code and Cowork reach Claude, from Python to Rust. The company says CPU usage per request fell by about 70% and memory use by more than 80%. The migration finished in September and is described in an engineering blog post.

The more useful part of the account is what went wrong on the way.

Why move at all

API traffic grew roughly 1,800 times between early 2024 and autumn 2026, Anthropic says. The Python service dates from 2023, when it did little more than forward requests. Since then it has taken on input validation, prompt construction, tokenisation and safety classification on the way in, and parsing of text, thinking and tool calls, streaming, and usage tracking for billing and rate limits on the way out. Python added overhead at every step through object creation, memory management and hand-offs between tasks.

A first attempt rewrote only the output parser in Rust. CPU use fell by 8 to 10%, but tokens now crossed between the two languages repeatedly, and the system became harder to reason about. Anthropic then chose a full rewrite.

98% of tests, 25% of traffic

Claude generated the first version of the core request path in about a week. Proving that it behaved exactly like the service it replaced took months.

"Our Rust API passed about 98% of its tests in April, yet only about a quarter of inputs matched on production traffic," the company wrote. "The gap came partly from cases in production traffic that our tests didn't cover."

There were about 700 integration tests, and they were tightly bound to the Python implementation. When the team audited the ported tests, it found more than 100 assertions that had been weakened.

One mismatch came from JSON serialisation. Python escaped certain characters, emojis among them, into text codes when it produced JSON tool calls. Rust handled them differently. Both versions processed the request without error, but the prompts they built differed by a few bytes.

How the gap was closed

Anthropic built a shadow-testing system that sent requests through both implementations without exposing customer data. Because Claude can answer the same request in different ways, the model was run once and its output replayed through the Rust path, so both versions were compared on identical model responses.

Each mismatch was logged with its location, its size and the processing stage where it began. The records were grouped into fingerprints that Claude agents investigated and proposed fixes for. Engineers still reviewed every pull request and approved changes before they shipped.

"Verification, not generation, had become the bottleneck," the company said.

The takeaway for engineering teams

A passing test suite showed that the new code agreed with the tests. It did not show that it agreed with production. Teams planning to have a model port a service should budget most of their time for comparing behaviour on real traffic, and should check that ported tests still assert what the originals did.

Sources: Anthropic.

Arsh Goyal
Arsh Goyal

Founder and editor of The Machine Standard. Senior software engineer and open-source maintainer.

Follow on LinkedIn

Get the Standard Brief

One email a week with the stories that matter in AI and tech.

Anthropic moved its Messages API from Python to Rust. Writing the code was the easy part