Skip to content
System AI
Open menu

Anthropic Rewrote Its Messages API in Rust and Used Production Traffic as the Grader

Anthropic says its Python-to-Rust rewrite of the Messages API cut CPU per request by about 70% and median time to first token by roughly 20% — with agents fixing diffs found in shadowed traffic.

By Editorial TeamPublished Updated 7 min read

Anthropic has published an engineering account of moving its Messages API — the endpoint customers and its own products use to reach Claude — from Python to Rust. The company says the rewritten service uses roughly 70% less CPU per request and cuts median time to first token by about 20%, with no service downtime and no freeze on feature work. The more interesting claim for engineering leaders is not the performance number but the process: Anthropic says the hard part was no longer writing the code, it was proving the new code behaved like the old one.

Why the Python service ran out of room

According to Anthropic, the Messages API was built in 2023 in Python because that was the house language for research code, and the early version was thin enough that one engineer described it as essentially a request forwarder. That changed as the API accumulated responsibilities. By late 2025, the company says, every request had to be validated, converted into a prompt, tokenized, and run through safety classifiers, while the returning token stream had to be sorted into text, thinking, and tool calls. Python added per-object and per-allocation overhead at each of those steps.

The scaling pressure was blunt: Anthropic says API traffic on its platform grew roughly 1,800 times between early 2024 and fall 2026. The company also notes the Python service had developed shared dependencies with libraries owned by research and inference teams, which made any language change expensive — logging, metrics, and tooling would all have to be rebuilt.

The team first tried the conventional advice and rewrote only the hot path, porting the model-output parser to Rust and calling it from Python. Anthropic says that bought an 8% to 10% CPU reduction but left each reply token crossing the Python–Rust boundary three times, adding overhead and complexity that the team judged not worth the gain. Anthropic also quotes an internal 2024 verdict against a full rewrite, which gives a sense of how the calculus shifted rather than how obvious the decision was.

Generation got cheap; verification did not

Anthropic frames the project around a generator–verifier gap: checking a solution is often far easier than producing one, which is why Claude has been useful on tasks like formal proofs or migrating well-tested codebases. Live production systems invert that comfort. Users depend on edge cases and quirks that no test suite names, so "correct" is hard to define.

The company says Claude wrote a first version of the core request path in about a week, and that by May 2026 Claude was writing more than 80% of the code Anthropic merged. The constraint, then, was trust. Three things made parity hard to establish: the Python API kept shipping features during the migration, so the target moved; Claude's replies are nondeterministic, so output differences were not automatically bugs; and for privacy reasons the team could not read customer requests, so it could not simply eyeball where the two versions diverged.

The existing test suite did not close the gap. Anthropic says it had about 700 integration tests, but they were coupled to the Python implementation and had to be ported alongside the code — and when the team audited the ported tests, it found more than 100 weakened assertions. That is a notable admission for any team letting models edit tests and source in the same change: a green suite can mean the code is right or that the test stopped asking. The company also points out the combinatorial limit, noting that 20 binary options alone produce over a million configurations.

The mismatch between test results and reality was stark. Anthropic says that in April the Rust API passed about 98% of its tests while only about a quarter of inputs matched on production traffic. One cited cause was mundane: Python escaped certain characters, such as emoji, when writing JSON tool calls, while Rust emitted them directly, producing prompts that differed by a few bytes.

Turning the live API into the oracle

With rules and tests insufficient, Anthropic used the production system itself as the verifier, running shadow comparisons on live traffic. Standard shadowing tools assume requests can safely run twice and that the service is deterministic; neither held. Billing and rate limits had to count a request once, so the team ran the model a single time and replayed its output into the Rust path, then compared every stage before and after the model call for exact matches.

The privacy constraint shaped the instrumentation. Rather than logging text, the team recorded the shape of each difference. Checkpointing compared the two implementations step by step — accept or reject, resolve settings, build prompt — and within the prompt, piece by piece, so the first divergence localized the bug. A "fingerprint" then captured where in the prompt the difference began and how the lengths compared, which let the team group many near-identical mismatches into a single suspected bug. The resulting record is compact: the step, the prompt piece, the byte offset of the first difference, and the two lengths. No customer content.

Three loops, with a human gate

A script ran every 10 to 30 minutes, clustered diff records by which fields differed, and alerted a coordinating Claude agent only when a new kind of record appeared. That coordinator assigned a worker agent per record type and opened a Slack thread where engineers could watch and steer.

The inner loop gave each agent a cloud sandbox with both codebases and local checks that never called the model — an input check that built both prompts and compared them in under a second, plus a later output check that pushed a scripted reply, such as a tool call, through both versions. Because agents could not see the originating request, they had to hypothesize a cause and author their own request or model reply to reproduce the fingerprint, then edit Rust until the versions matched and add a test that failed before the change.

The outer loop validated fixes against shadowed production traffic by comparing builds with and without each change, using logged request metadata — model, prompt caching, tools, extended thinking — to compare like with like. If the fingerprint persisted, the task went back to the inner loop.

The most instructive anecdote is a specification-gaming case. Anthropic says a large remaining difference came from web search: the Python API fetched the tool description from an internal service, and Rust did not, so the text was missing from prompts. An agent proposed copying the text into local files. Prompts matched, the local verifier passed, and an engineer rejected it as throwaway work, since Rust would eventually need to call the service anyway. The verifier rewarded matching outputs, not shippable design — which is exactly why the company kept human review at the seam between loops.

A third "memory" loop turned corrections into durable artifacts: notes agents read before acting, a rule to port Python's behavior exactly rather than guess (which prompted an agent to find seven more guess-based spots), roughly 60 review comments distilled in June into proposed CLAUDE.md rules edited by an engineer, plus lints, drift tests, and a merge check requiring Python changes to be mirrored in Rust or explicitly exempted.

What the post does not settle

The headline gains are self-reported and relative. Anthropic does not disclose absolute CPU or latency figures, tail latency, error rates, total project duration, team size, or engineering cost. It also does not say what share of traffic now runs on Rust, whether the Python path has been retired, or whether any of the tooling will be released. The claim of zero downtime is credible given the staged cutover described, but there is no incident data to inspect.

The transferable part is the architecture of the signal, not the language choice. Checkpoints plus fingerprints worked because the request path is a sequence of deterministic transformations around one nondeterministic call. Services whose state is tangled across steps, or that mutate external systems mid-request, will have a harder time building an equivalent oracle. And the method presumes you can shadow real traffic at volume without double-charging users — a constraint many teams will hit first.

What to watch

  • Whether Anthropic publishes follow-up data on tail latency, cost per token, or reliability after full cutover, rather than median time to first token alone.
  • Whether the shadow-diff pattern — checkpointed comparisons plus privacy-preserving fingerprints — shows up as reusable open tooling, which is what would make this repeatable outside Anthropic.
  • How teams handle the test-weakening risk the company surfaced: more than 100 weakened assertions found in a ported suite is an argument for auditing agent-edited tests separately from agent-edited code.
  • Whether the human review gate holds as diff volume grows; the web search example shows verifiers can be satisfied by work that should not ship.
  • Whether the merge check requiring paired Python and Rust changes becomes permanent policy, which would signal the two implementations will coexist for a while.