DEV Community

Cover image for Anthropic Moved a 1,800x API to Rust, With Claude Doing It
Dishant Sharma
Dishant Sharma

Posted on Edited on Originally published at dishantsharma.dev

Anthropic Moved a 1,800x API to Rust, With Claude Doing It

"We are not rewriting the API in go or rust!"

That is an Anthropic engineer in 2024, shutting down a proposal. Someone had suggested migrating the Messages API. The answer was no, with an exclamation mark.

Two years later they did it anyway. And the person who argued against it was not overruled by a manager or a reorg. Claude did the work.

The numbers are the part everyone quotes. About 70% less CPU per request. A 20% shorter median time to first token. The service never went offline and feature work never froze.

But the interesting part is not the speed. It is how they convinced themselves the thing was correct. Because the actual bottleneck turned out to be verification, not writing code.

Why Python stopped working

The Messages API went up in 2023 and was written in Python. At first it was barely a service.

One engineer at the time called it "basically a request forwarder."

Then it grew. By late 2025 every request had to be validated, converted into a prompt, tokenized, screened by safety classifiers, streamed back, sorted into text versus thinking versus tool calls, and metered for billing.

Traffic grew roughly 1,800 times between early 2024 and fall 2026. Every one of those steps allocates objects and frees memory, and Python charges you for each.

None of this is a language war argument. Python was the right call in 2023 because it matched the research code around it. The problem is that shared code became a trap. Moving off Python meant rebuilding logging, metrics, and tooling from scratch.

The incremental attempt failed, and that matters

The standard advice is to rewrite only the slow part. They tried that.

They ported output parsing to Rust and called it from Python. Result: about 8% to 10% less CPU.

Then the cost showed up. Every token now crossed the Python-Rust boundary three times. All that new overhead ate into the win, and the complexity stayed.

The 8% is the most useful number in the whole post. It is the argument against half-measures.

So they went for the full migration. By May 2026 Claude was writing more than 80% of the code Anthropic merged.

But generating the Rust was not the hard part. Proving it behaved the same was.

Why the tests could not save them

Anthropic had about 700 integration tests. Two things made them useless as proof.

First, they were coupled to the Python implementation. Tests ran the Python API in-process and mocked billing and rate limiting in Python's memory. When the code changed, the tests changed with it, and Claude helped with both.

Now a green test could mean the code was right, or that the test had been loosened until wrong code passed.

When they audited the ported tests, they found more than 100 weakened assertions.

Second, the tests were incomplete by arithmetic. Twenty binary feature flags make over a million combinations. At one second per test that is 12 days, for one model, before you vary a single prompt.

The gap between the two is the real story. In April their Rust API passed about 98% of tests, yet only about a quarter of real inputs matched on production traffic.

Turning production into the grader

The fix was shadowing. Run the same request through both versions and compare.

Except the usual assumptions break here. Claude is nondeterministic, so two different replies do not prove a bug. And a request cannot be billed twice. So they ran the model once and replayed the identical output into Rust.

Then they hit the privacy wall. They could not read customer requests or Claude's replies. So they compared the shape of each difference instead of its content.

Step What it caught
Checkpointing which stage diverged first
Fingerprint where in the prompt, and by how many bytes

A bug left the same fingerprint on every request it touched. That turned hundreds of thousands of diff records into a few dozen distinct problems.

Here is the bit that made me stop. Their Rust API escaped emoji in JSON tool calls differently from Python. A few bytes in a prompt. No error, no crash, just a subtly different answer.

A service that accepts almost any input will not fail loudly when you break it. It will fail invisibly.

Three loops

A script ran every 10 to 30 minutes, grouped new diff records, and woke a coordinating agent. That agent spun up workers and a Slack thread per bug so engineers could watch.

The inner loop gave each agent a cloud sandbox with both codebases. An input check built both prompts and compared them in under a second because it never called the model. An output check pushed a scripted reply through both paths.

Agents could guess a cause, write their own reproduction, and fail in seconds.

The outer loop was slower. It compared real deployments with and without each fix on shadowed traffic.

The memory loop is the one I would steal. Every rejected fix became a note. When an engineer threw out a workaround, the agent recorded the decision, the reason, and the correct fix. Later agents recognized the known bug instead of inventing a new hack.

One agent guessed at Python's behavior. That became a rule: port exactly what Python did. Comparing existing code against that rule found seven more places that had quietly guessed.

The fix that was right and wrong

In mid-May, the biggest remaining difference was web search. Python pulled the tool description from an internal service. Rust did not call that service, so the description was missing from prompts.

An agent proposed copying the text into files Rust could read. Prompts matched. The local verifier passed. Cheap, fast, done.

An engineer rejected it. Rust still had to call that service before launch, so the workaround would be thrown away.

"A verifier can tell an agent whether the outputs matched, but not whether the fix is good enough to ship."

That sentence is the whole case study. The verifier was not wrong. It was answering a narrower question than the one that mattered.


Small tangent. I once spent a week porting a batch job from Ruby to Go for the speed. It was faster. It also quietly changed how it rounded money, and nobody noticed for a month.

That was with one language change and one developer. Now imagine 20,000 requests a second, two languages, and a reviewer who is a language model.

The math that scares me is not the 70%. It is that their tests said 98% and production said 25%. Nobody's test suite is better than theirs.

The real talk

Most teams should not attempt this.

The technique needs three things you probably do not have: live traffic you can shadow without double-billing anyone, a way to compare behavior without reading user data, and an existing system stable enough to serve as an oracle.

Anthropic's own API kept changing during the migration. That made "correct" a moving target and forced the whole checkpointing apparatus.

If your service is small and your tests are honest, a straightforward rewrite with a human in the loop is fine. This is not a cheaper path. It is a path for people who already have the verification infrastructure and want to spend it.

And the honest caveat at the end: Claude wrote the first version of the request path in about a week. The trust took months.

One last thing

The Hacker News thread has 10 comments and one of them asks the question I keep circling.

"If humans are not reading the code, what are they doing besides pulling a lever?"

The post answers it, and the answer is not flattering to the lever theory. Humans wrote the rules. They designed the verifier. They decided what counted as correct, then reviewed only the fixes that crossed the seam.

The other reply in that thread is better than mine would have been. "Humans should be reviewing the verification that the code is doing what it's intended to do."

Which is a different job than reading code. Harder, actually. And the number of people who can do it well is smaller than the number who can read a diff.

Top comments (0)