Anthropic Migrates the Messages API to Rust: 70% Lower CPU and 20% Faster Time to First Token
TL;DR: Anthropic migrated its production Messages API from Python to Rust, using the live Python service and replayed model outputs as a parity oracle. The Rust API now uses about 70% less CPU per request and cuts median time to first token by about 20%, while Anthropic says it never took the service offline or froze feature development.
Why Anthropic migrated the Messages API to Rust
The migration was driven by per-request overhead in a Python service whose traffic had outgrown its original design. Anthropic built the Messages API in 2023 in Python, which was widely used internally for research code. At first the API was simple: it passed requests to the model and returned the model's output. One engineer at the time called it basically a request forwarder.
By late 2025, every Messages API request had to be validated, converted into a prompt, tokenized, and screened by safety classifiers. On the output side, the stream of tokens had to be sorted into text, thinking, and tool calls, streamed to the customer, and counted for usage, limits, and billing. Python added overhead each time it created an object, freed memory, or handed work to another task. From early 2024 to fall 2026, API traffic grew roughly 1,800 times.
Anthropic first tried an incremental approach: rewriting only the Rust code that parses a model's output and calling it from Python. That reduced the API's CPU usage by about 8% to 10%, but it added complexity because each token crossed between Python and Rust three times. The gains did not justify the added complexity.
The migration bottleneck was verification, not code generation
Claude wrote a first version of the API's core request path in about a week. By May 2026, Claude was writing more than 80% of the code Anthropic merged. That made the effort of a full migration seem tractable, but it did not answer the harder question: how could Anthropic verify that the Rust API behaved like the Python API?
Verification was difficult for three reasons. The Python API continued changing throughout the migration, so parity was a moving target. Claude can reply nondeterministically to the same request, so different outputs did not necessarily indicate a bug. And Anthropic could not read customer requests, so engineers could not easily see where the two versions diverged.
Why the existing tests could not prove parity
Anthropic had about 700 integration tests, but the tests were coupled to the Python implementation and incomplete. The tests ran the Python API inside the test process and mocked services such as billing and rate limiting in Python memory. The tests had to change along with the code, and Claude helped with both, so a passing test could mean the code was correct or that the test had been changed to let wrong code through. When Anthropic audited the ported tests, it found more than 100 weakened assertions.
The tests were also incomplete. The Messages API has dozens of features, and there are too many configurations to test exhaustively. Just 20 binary options produce more than a million combinations, which translates to 12 days of tests at one second each for one model before varying the prompts inside them.
The gap between tests and production was large. The Rust API passed about 98% of its tests in April, yet only about a quarter of inputs matched on production traffic. One source of mismatch was JSON tool calls: Python escaped certain characters, such as an emoji, by replacing them with text codes, while Rust wrote those characters as is, so the two prompts differed by a few bytes. Production also weighted bugs differently: in a test suite each case counts once, but in live traffic a bug appears in every request where it is triggered, so one common bug can cause many mismatches.
Using production traffic as the verifier
Anthropic used shadowing: running requests through both the old and new code and comparing the results. Standard shadowing tools often assume a request can safely run twice or that the service is deterministic. Neither assumption held. Anthropic had to ensure a request counted only once toward a customer's limits and bill, and Claude is nondeterministic. The solution was to run the model once and replay the model output to the Rust API.
With both versions working from the same model output, Anthropic compared every stage before and after the model call, and the results had to match exactly. Because Claude accepts almost any prompt, a small deviation could change its reply without triggering an error, so Anthropic compared each step before and after the model call to pin down deviations that might change what a customer received.
Anthropic also had to protect user privacy. It did not read user requests or Claude's replies. Instead it recorded the shape of each difference, such as where the difference happened in the service and how big it was.
Two mechanisms made this practical:
- Checkpointing. Both API versions handle a request through the same steps: accept or reject it, resolve its settings, and build the prompt. Every step produces a result, such as an error or the pieces of the prompt, that can be compared step by step and piece by piece. The first place the two versions differed indicated roughly where the migration code differed from production code.
- Fingerprinting. Anthropic recorded where each difference began in the prompt and the length difference between the two versions. Combined with the step and prompt piece isolated by checkpointing, one bug tended to leave the same fingerprint on every request it affected. Anthropic grouped errors by fingerprint to triage fixes.
The result was a shadowing system that produced a small record of each difference between the Rust and Python APIs. Anthropic could then pass that record to an agent to fix it.
How the agent verification loops worked
A single bug could produce hundreds of thousands of nearly identical diff records. Instead of sending each record to an agent, a script ran every 10 to 30 minutes, grouped records by which fields differed, and alerted a coordinating Claude agent only when it saw a new kind of record. The coordinator assigned a worker agent to own each new kind of record and created a thread in a shared Slack channel where human engineers could follow along and steer the work.
Anthropic set up three verification loops: a fast inner loop for testing ideas and proposing fixes in a sandbox, a slower outer loop for validating fixes on production traffic, and a memory loop for recording mistakes so agents could learn from them.
The inner loop: fast local checks in a sandbox
In the April version of the inner loop, each agent received a record showing where the Rust and Python versions differed, guessed the root cause from the code, and opened a pull request. After review and merge, the fix was deployed to staging, where the same shadow comparison ran on test traffic. Because every hypothesis needed review and deployment, feedback took hours or days.
Anthropic redesigned the inner loop to give agents a cloud sandbox with both codebases and fast local checks. The input check took a request, built the prompt each version would send to the model, and showed what differed; because it never called the model, comparison took under a second. An output check later ran both versions against a fake model, passing a scripted reply such as a tool call through each version and comparing the results.
Agents could see only the fingerprint, not the request behind each record. An agent hypothesized a cause, wrote its own request or model reply to reproduce the difference, and read both results in full. If the results matched, the guess was wrong. If they differed the same way the record described, the agent edited Rust until the versions matched and added a test that failed on the old code and passed on the new code.
Human review sat at the seam between the inner and outer loops. A wrong guess inside the sandbox cost seconds and had no user impact, so agents could iterate on their own. A fix could cross into the outer loop only after PR review, which let engineers focus on proposed fixes and on questions the verifiers could not answer, such as whether a difference was a bug or a deliberate change.
One example showed the limit of output matching. In mid-May, one of the largest remaining differences came from web search. The production API retrieved the web search tool's description from an internal service, while the Rust API did not call that service yet, so the tool description was missing from prompts. An agent proposed copying the text into files readable by Rust. The prompts matched and the fix passed the local verifier, but an engineer rejected it because Rust would still need to call the service before launch, and the workaround would then be thrown out. The verifier rewarded matching outputs, and the agent found the cheapest way to produce them; a verifier can tell an agent whether outputs match, but not whether a fix is good enough to ship.
The outer loop: validating fixes on production traffic
The outer loop compared Rust deployments with and without each fix. To make the comparison fair, Anthropic logged facts about each request but not its text, including which model and API features the request used, such as prompt caching, tools, or extended thinking. Those details let agents compare requests of the same kind and ask how often a bug's fingerprint appeared on builds with and without the fix.
The outer loop caught problems the inner loop missed. The inner loop simulated production quickly but imperfectly: agents could test a hypothesis in seconds, but only on requests they had written, with a local verifier that was not always configured exactly like production. Sometimes a fix passed in the sandbox and production still showed the difference. The assigned agent either saw the difference in the new build's records or was alerted by the coordinating agent, then returned to the inner loop with new evidence.
The memory loop: turning review feedback into rules
The memory loop recorded what came out of reviews and agents' own work and applied it to every agent that followed, so engineers would not have to make the same correction twice. When an engineer rejected the web search shortcut, the agent saved a note explaining the decision, the reason, and the correct fix: Rust should call the service that supplies tool descriptions. Building that call took the team weeks, and the bug kept producing records in the meantime. Some records had new fingerprints because the missing text fell at other positions in the prompt or belonged to another tool that used the same service. Agents checked the memory note, recognized the known bug, and linked those records to it instead of writing another workaround.
Other lessons became general rules for porting and writing Rust. After one agent guessed at Python's behavior, an engineer made it a rule to port exactly what Python did; an agent then compared the existing code with that rule and found seven more places where the port had relied on a guess. In June, an agent went through about 60 of the team's review comments, grouped recurring problems, and proposed rules that an engineer edited and approved for CLAUDE.md.
The memory loop also encoded lessons in permanent checks: new tests that failed when Rust drifted from Python, lints that banned patterns reviewers kept flagging, and later a merge check that required anyone changing the Python code to make a matching change in Rust or request an exemption.
Results and lessons from the Rust migration
Anthropic shipped an API on Rust that uses about 70% less CPU per request and cuts median time to first token by about 20%, without taking the service offline or freezing feature development. The Python API kept changing during the work, and agents kept the Rust version in step as the old version changed.
Anthropic frames the result with the Ship of Theseus metaphor: the usual approach to migration replaces planks while the ship still sails, but changing too many planks at once requires coming into dock. Claude made it possible to build an identically functioning ship alongside the first while the first kept sailing and having its planks replaced. Once Anthropic trusted that the two behaved the same, it moved passengers over a few at a time.
The lessons Anthropic draws:
- Verification is the new bottleneck. Large migrations were hard to justify because they took too much effort and carried too much risk. If writing code is no longer the bottleneck, the main constraint becomes how quickly results can be verified. Ambition can increase as long as Claude has a way to verify correctness.
- Verification is how you earn confidence. Proof assistants can verify each step of a mathematical proof, and tests or formal verification can help for software. When those checks are not enough, a production system itself can act as a verifier.
- Carefully design the verification signal. A verifier gives Claude a signal to act on. A proof assistant points to the failing step and a test names the failing case, but a production system often offers no such signal by default. Anthropic spent much of the project building that signal: checkpoints located each difference in the code, and fingerprints distinguished one bug from another.
- Build loops that let Claude act autonomously on the signal. The inner loop was fast and local in a sandbox. The outer loop was slower and checked fixes on shadowed production traffic. The outermost loop was prose, with corrections saved as rules in memory that every later agent could read.
- Keep loops simple so they scale with Claude's capability. Anthropic tried to put human judgment only into what counted as correct and left the method to Claude. Agents chose hypotheses, reproduced bugs, and fixed them; verifiers judged the result.
- Move toward outer loops. Engineers first worked inside the inner loop, running Claude and chasing differences themselves. They later moved to the seam between loops, steering agents and reviewing fixes in Slack. As the system matured, they moved to the edge, writing the rules and checks agents worked within.