ProgrammingLevel: Intermediate

How Anthropic moved its Messages API from Python to Rust and verified it with shadowing

On November 25, 2026, Anthropic described migrating its Messages API to Rust and the verification system that compared old and new versions on the same input. Try the idea yourself with a small Python exercise.

A programming lesson with a mentor at the academy
Illustrative photo: a practical lesson at the academy.

Context

Anthropic built the Messages API in Python in 2023. Its post says API traffic grew roughly 1,800 times from early 2024 to fall 2026, and validating each request, building the prompt, tokenizing it and processing the reply stream all added Python overhead.

The team first rewrote only the model-output parser in Rust, which cut CPU usage by about 8–10%. But each token then crossed between Python and Rust three times, adding complexity, so the team chose to migrate the whole request path. Claude wrote a first version of the core request path in about a week; verification became the hard part.

The existing set of about 700 integration tests was coupled to the Python implementation, and an audit of the ported tests found more than 100 weakened assertions. So the team used shadowing: the model ran once, its output was replayed to the Rust version, and every stage before and after the model call was compared exactly. One example from the post: when writing JSON tool calls, Python escaped characters such as emoji into text codes, while Rust wrote them as is.

Exercise: compare two functions on the same input

This small example imitates the emoji/JSON difference from the post. old_render writes non-ASCII characters as \uXXXX, while new_render keeps them as they are. The fingerprint function never prints the data itself; it only returns the position of the first differing byte and the lengths. All inputs are synthetic test data.

Pythonshadow.py
import json


def old_render(tool_input: dict) -> bytes:
    # "old" version: non-ASCII characters are escaped as \uXXXX
    return json.dumps(tool_input, ensure_ascii=True).encode("utf-8")


def new_render(tool_input: dict) -> bytes:
    # "new" version: characters are written as-is
    return json.dumps(tool_input, ensure_ascii=False).encode("utf-8")


def fingerprint(a: bytes, b: bytes):
    """Describe the shape of a difference without printing the data itself."""
    if a == b:
        return None
    first = next((i for i, (x, y) in enumerate(zip(a, b)) if x != y),
                 min(len(a), len(b)))
    return {"first_diff_byte": first, "old_len": len(a), "new_len": len(b)}


# synthetic test inputs only
cases = [
    {"city": "Baku", "n": 1},
    {"note": "salam \U0001F44B"},
    {"name": "\u015e\u0259ki"},
    {"list": [1, 2, 3]},
]

matched = 0
for i, case in enumerate(cases):
    fp = fingerprint(old_render(case), new_render(case))
    if fp is None:
        matched += 1
    print(i, "match" if fp is None else fp)
print(f"{matched}/{len(cases)} matched")

Expected result

Running python3 shadow.py was tested with Python 3.13.5 and produced the output below. ASCII inputs match, while inputs with emoji and Azerbaijani letters produce a longer byte sequence in the old version.

Textoutput.txt
0 match
1 {'first_diff_byte': 16, 'old_len': 30, 'new_len': 22}
2 {'first_diff_byte': 10, 'old_len': 26, 'new_len': 18}
3 match
2/4 matched

Limitations

This exercise does not reproduce Anthropic's system; it only shows the principle. Real shadowing also has to make sure a request is not counted twice for limits and billing, handle nondeterministic replies, and protect user privacy. Finding a difference does not prove it is a bug either: as Anthropic's post stresses, some differences are deliberate changes, and a human makes that call.

Recommended reading

Networking

Why getting a DNS reply is not enough

Cloudflare checks DNS changes against what users receive. Learn to distinguish getting a reply from getting the expected updated value.

IT Innovations Editorial1 min read