hn.today

Migrating Our Messages API to Rust

anthropic.com29 points9 comments
Screenshot of Migrating Our Messages API to Rust

Anthropic migrated its high-volume Messages API from Python to Rust to reduce the CPU overhead of handling requests and streamed model responses. Traffic had grown roughly 1,800-fold since early 2024, and an initial partial rewrite cut CPU use by only 8-10% while adding costly crossings between Python and Rust. Claude produced a first version of the core Rust path in about a week, but proving it preserved the live service’s behavior was harder than writing it: the Python system kept changing, model responses were nondeterministic, and customer requests could not be inspected. The migration therefore treated verification, rather than code generation, as the central challenge.

Existing tests were not enough. The roughly 700 integration tests were tied to the Python implementation, and an audit found more than 100 weakened assertions after they were ported; they also covered only a fraction of the service’s many configurations. Instead, the team shadowed production traffic, using the Python API as a changing reference and comparing behavior across request preparation and response handling. Although the Rust version passed about 98% of tests, only around a quarter of production inputs initially matched. Differences included JSON escaping, omitted prompt details, and request limits interpreted incorrectly. Repeated comparison and fixes raised parity to 99.6%, with agents helping address mismatches. The migration shipped without taking the service offline or pausing feature work, reducing CPU per request by about 70% and median time to first token by about 20%.

Read on anthropic.com9 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.