Tag: Rust

  • Anthropic Migrates the Claude Messages API to Rust (2026)

    Anthropic Migrates the Claude Messages API to Rust (2026)

    Takeaway: On November 25, 2026, Anthropic published Migrating our Messages API to Rust, an engineering account of how it moved the Messages API—the primary way customers and Anthropic’s own apps, including Claude Code and Cowork, reach Claude—from Python to Rust. The company says the Rust service uses about 70% less CPU per request and cuts median time to first token by about 20%, without taking the service offline or freezing feature development. Public traffic moved to Rust in September 2026.

    The details below come from that official research post, written by Lance Martin, Jake Eaton, Arvin Aminpour, Emma Webb, Bharat Mediratta, and Ravi Trivedi. Performance figures are Anthropic’s. The post’s CPU comparison is for one Region, memory is for all of production, and the time-to-first-token figure is from customers’ Claude Code requests over a shorter period.

    Why Anthropic left Python

    Anthropic built the Messages API in 2023 in Python, which was widely used internally for research code. At first, one engineer called it “basically a request forwarder.” That changed as the API gained features and shared Python dependencies with research and inference teams. From early 2024 to fall 2026, Anthropic says API traffic grew roughly 1,800 times. By late 2025, every request had to be validated, converted into a prompt, tokenized, and screened by safety classifiers, and each reply’s tokens had to be sorted into text, thinking, and tool calls. Python’s object and task overhead sat on every one of those steps.

    The team first tried an incremental rewrite: the code that parses a model’s output was moved to Rust and called from Python. That reduced CPU usage by about 8% to 10%, but each token then crossed between Python and Rust three times. Anthropic says the gains did not justify the added complexity, so it revisited a full migration. By May 2026, it says Claude was writing more than 80% of the code Anthropic merged. Claude wrote a first version of the API’s core request path in about a week. The harder problem was verifying that the Rust API matched production.

    Why tests were not enough

    The existing suite had about 700 integration tests, but two problems limited trust. The tests were coupled to the Python implementation—they ran the Python API in-process and mocked services such as billing and rate limiting in Python’s memory—so a “pass” could mean the code was right or that a test had been loosened. When the team audited the ported tests, it found more than 100 weakened assertions. The tests were also incomplete: Anthropic notes that just 20 binary options make over a million combinations, and that production users rely on edge cases the suite does not cover.

    That gap showed up clearly in April. The Rust API passed about 98% of its tests, yet only about a quarter of inputs matched on production traffic. One example: when Python writes JSON tool calls, it escapes certain characters, such as an emoji, as text codes, while Rust wrote those characters as is, so the prompts differed by a few bytes. A single common bug could also cause many mismatches, because live traffic weights frequent cases more heavily than a test suite that counts each case once.

    Using production as a verifier

    Anthropic turned the live Messages API into an oracle and shadowed production traffic, comparing the Python and Rust paths. Ordinary shadowing tools often assume a request can run twice and that the service is deterministic. Neither was true here. A request could be counted only once toward a customer’s limits and bill, and Claude’s replies can differ for the same input. The team ran the model once and replayed that model output to Rust, then required an exact match at every stage before and after the model call.

    The work had to protect user privacy: engineers could not read customer requests or Claude’s replies. Instead of inspecting text, they recorded the shape of each difference—where it happened in the service and how large it was. Checkpointing compared each step (accept or reject a request, resolve settings, build the prompt) and each piece of the prompt, stopping at the first mismatch. A fingerprint combined that location with the byte offset and the length of each version, so the same bug tended to leave the same signature and could be grouped for triage. Those records, not the underlying text, were what agents used to fix differences.

    Inner, outer, and memory loops

    A script ran every 10 to 30 minutes, grouped diff records, and alerted a coordinating Claude agent only when it saw a new kind of record. That coordinator assigned a worker agent and opened a Slack thread where human engineers could follow along. Three loops then handled the work.

    In the inner loop, each agent ran in a cloud sandbox with both codebases and fast local checks. An input check compared the prompts each version would send to the model without calling the model, in under a second. An output check later ran both versions against a scripted fake-model reply. Because agents could see only fingerprints, they hypothesized a cause, wrote their own request or model reply to reproduce it, and edited the Rust until the versions matched, then added a test. Early on, every hypothesis needed a review and a staging deploy, which took hours or days; the sandbox brought that cycle down to seconds, with no impact on users. A fix could enter the outer loop only after pull-request review. In mid-May, an agent proposed copying a missing web-search tool description into files so prompts would match. The local verifier accepted the shortcut, but an engineer rejected it because Rust still needed to call the internal service that supplies tool descriptions.

    The outer loop compared Rust deployments with and without each approved fix on shadowed live traffic. Anthropic logged facts about each compared request, but not its text, including the model and features such as prompt caching, tools, or extended thinking, so agents could watch whether a bug’s fingerprint disappeared. A fix that passed in the sandbox sometimes still failed in production; the assigned agent then returned to the inner loop with that evidence.

    A memory loop recorded review decisions so later agents would not repeat the same mistake. After the web-search shortcut was rejected, a note told later agents to link related fingerprints to the known missing-service bug instead of inventing another workaround. An engineer also made it a rule to port exactly what Python did; an agent then found seven more places where the port had relied on a guess. In June, an agent grouped about 60 review comments into proposed rules, which an engineer edited and approved for CLAUDE.md. The team also added tests that failed when Rust drifted from Python, lints for patterns reviewers kept flagging, and later a merge check that required a matching Rust change—or an exemption—whenever Python code changed.

    For other recent Anthropic changes that affect how developers talk to Claude and ship code with it, see Anthropic Message Threads: Faster Claude Agents (2026) and Anthropic Launches Claude Ship to Review, Fix, and Approve PRs (2026).

    Frequently asked questions

    When did Anthropic publish the Messages API Rust migration?

    On November 25, 2026, in the official research post “Migrating our Messages API to Rust.” Public traffic had already moved to the Rust API in September 2026.

    What performance results does Anthropic report?

    About 70% less CPU per request and about a 20% reduction in median time to first token. Anthropic notes that the CPU comparison is for one Region, memory is for all of production, and the time-to-first-token figure comes from customers’ Claude Code requests over a shorter period.

    Did the migration take the API offline?

    No. Anthropic says it did not take the service offline or freeze feature development. It describes moving traffic over a few at a time once the two implementations matched on shadowed production requests.

    Why did Anthropic migrate from Python to Rust?

    The Messages API, first written in Python in 2023, faced roughly 1,800 times more traffic from early 2024 to fall 2026, plus more per-request work such as validation, tokenization, safety classification, and streaming tool calls. An incremental Rust parser cut CPU use by only about 8% to 10% and added language-boundary complexity, so the team chose a full migration.

    Why wasn’t the existing test suite enough?

    The suite had about 700 integration tests, but they were coupled to Python and did not cover production edge cases. An audit found more than 100 weakened assertions in the ported tests. In April, the Rust API passed about 98% of tests while only about a quarter of production inputs matched.

    How did Anthropic compare the two APIs without reading user data?

    It ran the model once, replayed that output to Rust, and compared every stage before and after the model call. Differences were recorded as checkpoints and fingerprints—location, offset, and length—without the request or reply text.

    What were the verification loops?

    An inner sandbox loop let agents reproduce and fix fingerprint differences in seconds. An outer loop validated approved fixes on shadowed production traffic. A memory loop turned review decisions into rules, searches, and tests so later agents did not repeat known mistakes.

    When did public traffic move to the Rust API?

    In September 2026, according to the official post’s account of changes merged to the Rust code from April until public traffic moved.