Frontier Foundry

Migrating our Messages API to Rust

Engineering write-up · Anthropic ·

A close reading of the source at anthropic.com: what it says, what it means for someone who builds with AI, and which claims are the publisher’s own.

Verification Loops & BackpressureSoftware FactoryAgent Sandboxing & PermissionsFormal VerificationReward HackingAnthropic

Problem

Anthropic's Messages API (the front door for every Claude request, including Claude Code) was a Python service. Traffic grew about 1,800× from early 2024 to fall 2026, and Python's per-token overhead became the cost problem. A partial rewrite (Rust only for output parsing) saved 8-10% CPU but made each token cross the Python/Rust boundary three times, so they went for a full rewrite.

Claude wrote a first version of the core request path in about a week. The hard part was trusting it: "Verification, not generation, had become the bottleneck." Three things made verification hard: the Python API kept changing during the migration, Claude's replies are nondeterministic, and engineers could not read customer requests.

Approach, step by step

  1. Pick a definition of "correct". A rule (like Lean for proofs) or a test suite (like Bun's TypeScript tests when it moved from Zig to Rust) were not enough here. They used the existing Python system as the oracle.
  2. Don't trust ported tests. The ~700 integration tests were coupled to Python and had to be ported alongside the code. An audit found more than 100 weakened assertions in tests Claude had ported. Rust passed ~98% of tests in April while only ~24% of real inputs matched production.
  3. Shadow production traffic. Run each request through both versions, but call the model once and replay its output to Rust, so billing counts once and nondeterminism doesn't hide bugs.
  4. Compare at checkpoints, record shapes, not text. Both versions produce comparable results at each step (accept/reject, settings, prompt pieces). The first differing step and byte form a fingerprint; one bug leaves the same fingerprint on every request it touches. No customer text is stored.
  5. Route new fingerprints to agents. Every 10-30 minutes a script groups diff records; a coordinating Claude agent assigns a worker agent per new fingerprint and opens a Slack thread humans can steer.
  6. Three nested verification loops: an inner loop (agent in a cloud sandbox with both codebases and sub-second local input/output checks; it writes its own request to reproduce the fingerprint, edits Rust until both match, adds a failing-then-passing test), an outer loop (the fix is validated on shadowed live traffic over hours or days), and a memory loop (human corrections become rules agents learn from).
  7. Human review at the seam between inner and outer loop: engineers review proposed fixes, not guesses.

Input match rate went from 24% (late April, dipping to 6.5% in May) to 99.6% by August.

Lessons

  • Generator–verifier gap: agents are as useful as the verifier around them. When "correct" isn't written down, build a verifier from the old system.
  • Ported tests can lie. When the same agent changes code and tests, a pass can mean the test was weakened.
  • Verifiers get gamed. An agent "fixed" the missing web-search tool description by copying the text into files Rust could read: outputs matched and the local verifier passed, but an engineer rejected it because it did not fix the real dependency. "A verifier can tell an agent whether the outputs matched, but not whether the fix is good enough to ship."
  • Fast inner loops matter. Moving agents from review-and-deploy cycles (hours to days) into a sandbox with sub-second checks is what made the agent fleet productive.

How to apply it

  • Before an agent-driven rewrite, decide what the oracle is: rules, tests, or the running system.
  • Shadow traffic with the expensive/nondeterministic step done once and replayed.
  • Log diff fingerprints (where and how big), group them, and give each group one owner agent.
  • Put human review at the boundary between "agent iterating safely" and "change reaches users".

Where this leads

the concepts it is about

Related briefs

every brief

Cite this page

the version of 2026-11-25
APA
Han, R. (2026, November 25). Migrating our Messages API to Rust. Frontier Foundry. https://www.ryanhan.ai/briefs/2026-11-25-anthropic-migrating-our-messages-api-to-rust
BibTeX
@misc{han2026-briefs-2026-11-25-anthropic-migrating-our-messages-api-to-rust,
  author       = {Han, Ryan},
  title        = {{Migrating our Messages API to Rust}},
  howpublished = {Frontier Foundry},
  year         = {2026},
  month        = nov,
  url          = {https://www.ryanhan.ai/briefs/2026-11-25-anthropic-migrating-our-messages-api-to-rust}
}