Migrating the API to Rust: Claude Wrote the Code in a Week, Then Spent Months Proving It Didn't Get It Wrong
Claude wrote the core request path for the Rust version of the Messages API in a week, but the real bottleneck in the production migration was verification. Anthropic used the old Python service as a grader and had Claude agents trace every discrepancy; in the end, CPU dropped by about 70% and median time to first token dropped by about 20%.
Anthropic moved the Messages API from Python to Rust. Claude wrote the first version of the core request path in about a week. The part that actually took time came later: proving that the new service behaved identically to the old service in production.

The Messages API is the main entry point through which customers and Anthropic's own applications, such as Claude Code and Cowork, access Claude. From early 2024 to fall 2026, API traffic grew about 1,800x. The Python service was originally simple—one engineer called it "basically a request forwarder." Later, every request had to be validated, converted into a prompt, tokenized, and passed through safety classifiers, and the response token stream had to be split into text, thinking, and tool calls. Python's overhead for creating objects, freeing memory, and switching tasks was amplified by traffic.

The team first did an incremental rewrite: calling Rust code that parsed model output from Python. CPU dropped only 8% to 10%. Every token crossed between Python and Rust three times, complexity went up, and the payoff wasn't enough. In 2024, one engineer proposed migrating to Go or Rust, and another engineer left a note: "We are not rewriting the API in go or rust!" By May 2026, Claude had written more than 80% of the code Anthropic merged, and the team recalculated the tradeoff.

Writing code was no longer the bottleneck; verification was. In production systems, edge behaviors and quirks that users depend on aren't necessarily covered by test suites. It's hard to write a "correct" that covers everything. Anthropic's choice was to use the existing production Messages API itself as the oracle and use it to test whether the new Rust version matched.

They started with tests. There were already about 700 integration tests, but there were two problems. The tests were coupled to the Python implementation and ran inside the test process, with services such as billing and rate limiting mocked in Python memory. After Claude helped migrate the tests, a "pass" might mean the code was correct, or it might mean the test had been loosened. Auditing the ported tests found more than 100 weakened assertions. The tests were also incomplete. The API has dozens of features; 20 binary options already exceed a million combinations. At one per second, one model would take 12 days to run, not counting prompt variations.

In April, the Rust API's test pass rate was about 98%, but only about a quarter of production traffic inputs matched Python. The gap came from production cases the tests didn't cover, and from how production weights bugs: in tests each case counts once, while in production every request that triggers a bug counts once, so a common bug can create a huge number of mismatches.

They did shadowing: ran the same batch of requests through the old and new versions and compared the results. But there were two constraints. Requests could only count once against customer rate limits and billing. Claude's output is nondeterministic for the same input. So the model was run only once, and the model output was replayed to Rust. Both sides started from the same model output and then compared every stage before and after the model call, requiring exact agreement.

Privacy requirements meant they couldn't read user requests or Claude responses. So instead of comparing text, they recorded the shape of the difference. A checkpoint located which step the difference occurred in and which piece of the prompt; a fingerprint recorded the starting byte of the difference, the length difference on each side, and so on, grouping the same kind of bug together. An example record looked like this:
step: build prompt
piece: 2
first diff: byte 17
python: 190 bytes
rust: 190 bytes


With these records, the verification loop started running. A script ran every 10 to 30 minutes, grouped differences by field, and notified the coordinating agent only when a new kind appeared. The coordinating agent assigned work to worker agents and opened a Slack thread where human engineers could follow up.

The inner loop ran in a cloud sandbox; the agent could see fingerprints but not the original requests. It first guessed the cause, wrote its own request or fake model response to reproduce the difference, then changed Rust to make both sides match, and finally added a test that failed on the old code and passed on the new code. The local input check did not call the model; it compared the prompts built by both versions and took less than a second. The output check used a fake model to replay scripted responses.

The outer loop deployed fixes to production shadow traffic and compared the frequency of the same fingerprint in builds with and without the fix. The inner loop was fast but imperfect; the outer loop was slow but closer to reality. Human review in between was the gate.

One example shows why the gate was necessary. In mid-May, one of the largest differences came from web search. The production API fetched the web search tool description from an internal service; the Rust version did not call that service yet, so the description was missing from the prompt. The agent proposed copying the text into a file Rust could read, matching the prompt, and passing local verification. The engineer rejected it: before launch, Rust would still need to call the service, and this workaround was throwaway code. The verifier could tell the agent whether the output matched; it could not tell it whether the fix was worth shipping.

The memory loop recorded corrections from reviews. After the engineer rejected the web search shortcut, the agent wrote the decision, the reason, and the correct fix into memory: Rust needed to call the service that provided the tool description. That fix took the team weeks, and during that time bugs kept generating records. Some fingerprints were different because the missing text landed elsewhere in the prompt or belonged to another tool from the same service. The agent consulted memory, recognized the known bug, associated the record with it, and stopped writing new workarounds. Some rules became permanent checks: "port exactly what Python did," and one agent used it to find seven places where the port had relied on guesses; review comments were turned into rules in CLAUDE.md; new tests, lints, and merge checks required anyone changing Python to update Rust in sync or request an exemption.

Results: the Rust version reduced CPU per request by about 70%, memory by more than 80%, and median time to first token by about 20%. The service did not go down, and feature development was not frozen. During the migration, merged changes every two weeks rose from fewer than 100 to more than 3,000.


Anthropic summarized in its blog post: verification is the new bottleneck. Large migrations used to be hard because writing code was labor-intensive and risky; if writing code is no longer the blocker, the constraint becomes how quickly results can be verified. Verification signals need to be designed in enough detail for Claude to know where it went wrong; the loop needs to be simple, so humans only make judgments about "what counts as correct" and leave the rest to agents.
In the comments, some asked why everyone is migrating to Rust, and why Rust instead of Go; someone said Python had been demoted to a grader; someone summed it up as the old system's bugs being promoted to acceptance criteria. Some also worried whether different bugs could produce the same fingerprint. In Anthropic's example, the fingerprint combines checkpoint and grouping to distinguish bugs, but the question itself is worth continuing to watch.
One commenter said that rewriting a production API and having the old version grade the new one was like running old and new water pumps side by side until the water came out the same—nobody wants surprises in production. That metaphor is more accurate than a lot of architecture diagrams.
发布时间: 2026-11-26 01:27