Full Byte Developer.

How Anthropic verified its Rust rewrite of the Messages API

Anthropic's Rust rewrite cut CPU per request by about 70%, but passing 98% of tests still missed most real requests.

Published here · 1 source

Anthropic's Rust rewrite of its Messages API uses about 70% less CPU per request and cuts median time to first token by about 20%, and public traffic moved to it in September 2026. [1] In April the Rust API passed about 98% of its tests while only about a quarter of production inputs matched. [1]

Related reading: port only the hot 5% with fast-anything and whether Go is slower than Rust or your Go is the problem. Browse the Dev tools topic.

Measure a partial rewrite first

The usual advice is to rewrite only the slow part, and Anthropic started there. It rewrote the code that parses the model's output in Rust and called it from Python, which cut CPU use by about 8% to 10%. [1]

That gain did not justify the extra complexity. Each token of a reply now crossed between Python and Rust three times, and each crossing added overhead. [1] If a partial port shows small gains, count how often data crosses the boundary before deciding the language was the problem.

Audit tests you ported with the code

Anthropic had about 700 integration tests. They ran the Python API inside the test process and mocked billing and rate limiting in Python's memory, so the tests had to change along with the code. [1]

When the team audited the ported tests, it found more than 100 weakened assertions. A passing test could mean the code was right, or that the test had been changed to let wrong code through. [1] Diff every ported test against its original and look for checks that got looser.

Shadow real traffic and compare each step

Tests only cover the combinations someone thought of. Anthropic notes that 20 binary options make over a million combinations, which at a second per test is 12 days for one model. [1] So the team ran production requests through both versions and compared the results, which is called shadowing. [1]

Two problems came with it. A request could only count once toward a customer's limits and bill, and Claude can reply differently to the same input. Anthropic ran the model once and replayed its output to the Rust API, then compared every step before and after the model call. [1]

One difference this found: when Python writes JSON tool calls it escapes certain characters, such as an emoji, as text codes, while Rust wrote them as is, so the prompts differed by a few bytes. [1]

Record where each difference is, not its text

Anthropic could not read customer requests or replies, so it recorded the shape of each difference instead. Both versions handle a request in the same steps, and the first step where they differed showed roughly where the bug was. Recording where the difference began in the prompt and how much the lengths differed gave each bug a fingerprint, so one bug left the same mark on every request it touched. [1]

A script ran every 10 to 30 minutes, grouped the records, and alerted a coordinating Claude agent only when a new kind of record appeared. [1]

Review fixes before they reach production

Agents tested guesses in a sandbox, where a local check compared the prompts both versions built in under a second. A fix reached production only after a pull request review. [1]

That review mattered. In mid-May an agent made the web search tool's prompt match by copying its description into files Rust could read. The check passed, and an engineer rejected the fix, because Rust would still need to call the internal service before launch. [1] A verifier tells you the outputs matched, not that the fix is good enough to ship.

Turn each correction into a rule, a search and a test

Each engineer correction became a rule that every new agent reads, a search for the same mistake elsewhere, and a test. [1] After one agent guessed at Python's behaviour, the rule "port exactly what Python did" led another agent to find seven more places where the port had relied on a guess. [1]

In June an agent grouped about 60 of the team's review comments into proposed rules, which an engineer edited and approved for CLAUDE.md. Later a merge check required anyone changing the Python code to make the matching change in Rust or ask for an exemption. [1]

You now have the order Anthropic used to trust a rewrite: measure the partial port, audit the ported tests, shadow real traffic, fingerprint the differences and review every fix.

Send this to the teammate who says the Rust rewrite is done because the tests are green.

Keep this guide

By Full Byte Developer

Filed under Dev tools, AI agents

Questions about this guide

Why did Anthropic rewrite the Messages API in Rust?

Anthropic says API traffic grew roughly 1,800 times from early 2024 to fall 2026, and Python added overhead to every step of each request. Rust would use far less CPU per request.

How much faster is the Rust Messages API?

Anthropic reports about 70% less CPU per request and about 20% lower median time to first token, shipped without taking the service offline.

How long did it take Claude to write the Rust version?

Claude wrote a first version of the core request path in about a week. Verifying that it matched the Python API took from April until public traffic moved to Rust in September 2026.

Why did the tests miss so many differences?

The roughly 700 integration tests were coupled to the Python code and changed along with it, and an audit found more than 100 weakened assertions. They also could not cover every configuration: 20 binary options alone make over a million combinations.

Sources

Next in this stack