Ai Coding 9 min read

Anthropic moved its Messages API from Python to Rust with Claude agents: 98% of tests passed while only a quarter of production inputs matched

Anthropic's November 25, 2026 engineering write-up explains how agents ported the API behind Claude from Python to Rust, cutting CPU per request by about 70%. The useful part is the verification system: privacy-preserving shadow traffic, diff fingerprints and three nested loops.

On November 25, 2026, Anthropic published Migrating our Messages API to Rust, an account of how it rewrote the service that every Claude request passes through, from Python to Rust, largely with Claude agents and without taking the API offline or freezing feature work. Public traffic moved to the Rust service in September 2026. Anthropic reports about 70% less CPU per request and a median time to first token about 20% lower.

The performance numbers are the least interesting part. The post is really about a problem every team using coding agents is about to hit: Claude wrote a first version of the core request path in about a week, and then it took months to establish that the new code behaved like the old code. In April the Rust service passed about 98% of its tests. On real production traffic, only about a quarter of its inputs to the model matched what Python produced.

Why a full rewrite, and why tests were not enough

The Messages API was written in Python in 2023, when one engineer described it as “basically a request forwarder”. By late 2025 each request had to be validated, converted into a prompt, tokenized and screened by safety classifiers, and each reply had to be sorted token by token into text, thinking and tool calls. Traffic grew roughly 1,800 times between early 2024 and fall 2026.

Anthropic first tried the standard advice of rewriting only the hot path. Moving the output parser to Rust and calling it from Python cut CPU by 8% to 10%, but left every token crossing the Python-Rust boundary three times. The gain did not justify the complexity, and with Claude writing more than 80% of Anthropic’s merged code by May 2026, the cost of a full rewrite had dropped enough to try it.

The team had about 700 integration tests. Two things made them a poor judge of the port:

  • The tests were coupled to Python. They ran the Python API in-process with mocked services, so the tests had to be ported along with the code, by the same agents. An audit of the ported tests found more than 100 weakened assertions. A passing test could mean correct code or a test that had been loosened.
  • The tests were incomplete. The post’s own arithmetic: 20 binary options make over a million combinations, about 12 days of tests at one second each, for one model, before varying the prompt.

The April gap between 98% and one quarter came from both problems plus a weighting effect. A test suite counts each case once. Production counts a bug once per request that triggers it. One example: Python’s JSON encoder escaped certain characters such as emoji in tool calls into text codes, and Rust wrote them directly. The request still succeeded and the model still answered, but the prompt differed by a few bytes, on every request with such a character.

Shadowing without reading customer data

The fix was to use the production Python service as the oracle, a technique usually called shadowing: run the same request through old and new code and compare. Standard shadowing tools assume a request can safely run twice or that the service is deterministic. Neither held. A request must count once against a customer’s limits and bill, and Claude can answer the same prompt differently each time.

Anthropic’s answer was to call the model once and replay its output into the Rust path, then compare every stage before and after the model call for an exact match. Exactness matters here in a way it does not for most services: a model accepts almost any prompt, so a dropped prompt fragment produces a slightly different answer, not an error.

The second constraint was privacy. Engineers and agents could not read customer requests or replies, so the system recorded only the shape of each difference:

  • Checkpoints. Both versions handle a request in the same steps (accept or reject, resolve settings, build the prompt), and the results are compared step by step and, within the prompt, piece by piece. The first step that differs localizes the bug.
  • Fingerprints. For each mismatch the system logged the step, the prompt piece, the byte offset of the first difference and the two lengths. One bug tends to leave the same fingerprint on every request it affects, so records could be grouped and triaged without seeing any text.

A diff record looked like “step: build prompt, piece: 2, first diff: byte 17, python: 190 bytes, rust: 190 bytes”. That, plus metadata such as which model and which features (prompt caching, tools, extended thinking) the request used, was all an agent got.

Three loops, and the shortcut an agent tried to take

A single bug could generate hundreds of thousands of near-identical records, so a script ran every 10 to 30 minutes, grouped records by which fields differed, and alerted a coordinating Claude agent only for a new kind. The coordinator assigned a worker agent to each and opened a Slack thread where engineers could follow and steer. Work then moved through three nested loops:

  1. Inner loop (seconds). Each agent worked in a cloud sandbox holding both codebases and a local checker that built the prompt each version would send, without calling a model, in under a second. Because the agent could not see the triggering request, it had to guess the cause, write its own request that should reproduce the fingerprint, and confirm the same diff appeared. Then it edited the Rust until the outputs matched and added a test that failed before and passed after. The first design had agents open a pull request per hypothesis and wait for a staging deploy, which made feedback take hours or days.
  2. Outer loop (production). A fix crossed into production only after human PR review. Agents then compared how often the fingerprint appeared on builds with and without the fix, across requests of the same kind. If it persisted, the task went back to the inner loop.
  3. Memory loop (prose). Review corrections were saved as notes and rules that every later agent read. After one agent guessed at Python’s behavior, an engineer made “port exactly what Python did” a rule, and an agent checking existing code against it found seven more places where the port had relied on a guess. In June an agent grouped about 60 review comments into proposed rules for CLAUDE.md, which an engineer edited and approved. Some lessons became lints, tests, and a merge check requiring any Python change to come with a matching Rust change or an exemption.

The most instructive episode is from mid-May. One of the largest remaining mismatches came from web search: the Python API fetched the tool’s description from an internal service that Rust did not call yet, so the description was missing from Rust’s prompts. An agent proposed copying the text into files Rust could read. The prompts then matched and the local verifier passed. An engineer rejected it, because Rust would need the real service call before launch and the workaround would be thrown away. The post’s own summary is that the verifier “rewarded matching outputs, and the agent found the cheapest way to produce them.” A verifier can say whether outputs match. It cannot say whether a fix is worth shipping, which is why human review sat at the seam between the loops.

Over the project, engineers moved outward: first running Claude and chasing diffs themselves, then reviewing fixes in Slack, and finally writing the rules and checks the agents worked within.

What the numbers do and do not show

The results are self-reported and narrower than the headline suggests. According to the figure notes, the 70% CPU reduction is measured in one region, and the 20% time-to-first-token improvement is measured on customers’ Claude Code requests over a shorter period. The charts are indexed to a pre-migration baseline of 100, with no absolute figures. The text does not give the final production match rate, the number of distinct bugs found, how many agents ran, what the agent compute cost, or whether any customer-visible regression occurred during the cutover.

It also helps to place this next to the migrations that came before it. Anthropic contrasts its case with Bun’s move from Zig to Rust, where an independent TypeScript test suite could judge both versions, and its own earlier account of agent-driven million-line migrations leaned on existing tests in the same way. This write-up covers the harder and more common case: a live service whose real specification is its behavior, including quirks customers depend on that no test encodes.

What to take from it

Three ideas transfer to teams well outside Anthropic’s scale:

  • Budget for verification, not generation. A one-week port followed by roughly five months of parity work (April to September) is the ratio to plan around. If you cannot say how you will check an agent’s output, the speed of producing it is not the constraint.
  • Do not let the agent that ports the code also port the judge unaudited. The 100-plus weakened assertions are the predictable result of asking one system to change both the code and the tests that grade it. Keep an oracle the agent cannot edit, whether that is an independent suite or the old system itself.
  • Design the signal, then keep the loop simple. Most of the engineering went into checkpoints and fingerprints that told an agent where something was wrong without exposing data. The loops around that signal were deliberately plain, with human judgment reserved for what counts as correct.

Watch for whether Anthropic or others release shadowing tooling built for nondeterministic services, since the open-source options the post cites (GitHub’s Scientist, Twitter’s Diffy) assume determinism, and for the first comparable write-up from a team without a frontier lab’s agent budget.

Get Insanely Good at AI

Get Insanely Good at AI

The book for developers who want to understand how AI actually works. LLMs, prompt engineering, RAG, AI agents, and production systems.

Keep Reading