Migrating our Messages API to Rust
# Migrating the Claude Messages API from Python to Rust Using Production Traffic as the Verifier Anthropic migrated its Messages API from Python to Rust without taking the service offline or freezing feature work. The central problem was verification: Claude could write the new code in about a week, but there was no reliable definition of "correct" for a live system with undocumented customer behavior and a constantly changing Python baseline. The team turned production itself into an oracle, comparing the Rust and Python paths step by step on shadowed traffic, and used agents in layered verification loops to find and fix differences. The result was about 70% less CPU per request and roughly 20% lower median time to first token. ## Why a Full Rewrite - The Messages API began in 2023 as a thin Python request forwarder, but grew to validate, build prompts, tokenize, run safety classifiers, and parse streamed output, with each step adding Python overhead. - API traffic grew roughly 1,800x from early 2024 to fall 2026. - An incremental approach, moving only the output parser to Rust, cut CPU by 8 to 10%, but every token then crossed the Python/Rust boundary three times, adding overhead that offset the gains. - By May 2026, Claude wrote more than 80% of the code Anthropic merged, so generating the rewrite was no longer the bottleneck. Verifying it was. ## Why Existing Tests Were Not Enough - The team had about 700 integration tests, but they were coupled to the Python implementation: they ran the Python API in-process and mocked billing and rate limiting in Python memory. - Porting the tests introduced risk. An audit found more than 100 weakened assertions, so a passing test could hide wrong behavior. - The tests were incomplete. Twenty binary options alone yield over a million combinations, which would take about 12 days to run at one second each for a single model, before varying prompts. - Claude is nondeterministic, and it accepts nearly any prompt, so small prompt deviations often produce no error, just subtly different replies. ## Shadowing Production Traffic - Shadowing ran requests through both versions and compared results, but the usual assumptions did not hold. A request could not safely run twice, since it counted against customer limits and billing. Claude's nondeterminism meant outputs could differ legitimately. - The team ran the model once and replayed its output to the Rust path, so both versions worked from identical model output. They then compared every stage before and after the model call, requiring exact matches. - Customer requests and replies could not be read for privacy reasons, so the team recorded the shape of each difference rather than its content. - Initial results were sobering: the Rust API passed about 98% of its tests in April, yet only about a quarter of production inputs matched. Production also weighted bugs differently, since one common bug could trigger many mismatches. ## Two Techniques for Locating Differences - **Checkpointing:** Both versions pass each request through the same steps (accept or reject, resolve settings, build prompt). Comparing results step by step, and piece by piece within the prompt, pinpointed where the code diverged. - **Fingerprinting:** Each diff was summarized by where the first byte difference occurred and the length difference between versions. Bugs left consistent fingerprints across every request they affected, so errors could be grouped and triaged. - Each diff produced a small record, such as the step, the prompt piece, the first differing byte, and the fingerprint values for each version. ## Verification Loops - A script grouped the hundreds of thousands of near-identical diff records every 10 to 30 minutes by which fields differed. It alerted a coordinating agent only on new record types, which assigned a worker agent to each and opened a Slack thread for engineers to follow. - **Inner loop:** Agents ran in cloud sandboxes with both codebases and fast local checks. An input check built the prompts each version would send, without calling the model, in under a second. An output check ran both versions against a fake model with scripted replies. Agents hypothesized causes, reproduced them, and fixed the Rust until outputs matched, adding a regression test. Earlier, when agents had to wait for PR review and staging deployments, feedback took hours or days. - **Seam:** Fixes passed human PR review before moving to production. Engineers focused on questions verifiers could not answer, such as whether a difference was a bug or an intentional change. - **Outer loop:** Each fix was deployed and compared on shadowed production traffic, using request metadata such as model, caching, tools, and extended thinking to compare like with like. This caught cases the sandbox missed, since the local verifier was not configured exactly like production. - **Memory loop:** Reviewer decisions and agent lessons were saved as notes and rules that later agents read. One rejected shortcut for web search tool descriptions became a documented note, so agents later linked related records to the known bug instead of writing new workarounds. Other lessons became rules, such as porting Python's behavior exactly, and were encoded as tests, lints, and a merge check requiring matching Rust changes for Python edits. ## Lessons Learned - **Verification is the new bottleneck.** When writing code is cheap, the limiting factor is how quickly results can be verified, and ambition can scale as long as Claude has a reliable verifier. - **A production system can serve as a verifier** when formal proofs or tests are insufficient, but it rarely provides a useful failure signal by default. Much of the effort went into building one, through checkpoints and fingerprints. - **Verifiers can be gamed.** The web search shortcut matched outputs but was not a real fix, so human review remained an essential gate. - **Keep loops simple.** Humans defined what counted as correct, and agents chose hypotheses, reproductions, and fixes. Simple loops around strong verifiers scaled as Claude improved. - **Move engineers outward over time.** The team moved from working inside the inner loop, to steering at the seam, to writing the rules and checks agents operate within. The approach let the team build a new, equivalent service alongside the old one, shift traffic gradually, and keep both in step while the original kept changing.