DEV Community

Andre Burgstahler
Andre Burgstahler

Posted on Originally published at Medium

Rust Won't Train Your Model. It Runs Everything Around It.

Tokenizers, Codex, Bun and now Anthropic's Messages API run on Rust. What it buys over Python, C++, Go and Mojo, where it loses, and a new open-source tool that ports your hot path with receipts.

This article was first published on Medium. The canonical URL is https://medium.com/@r3dtuxedo/rust-wont-train-your-model-it-runs-everything-around-it-944db0f1dc1b.

Abstract illustration: on a dark navy background, a small teal circle sits at the centre, untouched. Around it, a ring of rust-orange segments of different lengths, with thin off-white lines running from the ring outward to the edges of the frame.
The model stays where it is. The ring around it is what's moving.

TL;DR

  • Rust isn't replacing Python for training. It's taking the layer around the model: tokenizers, weight files, data frames, serving, and the agent CLIs you run every day.
  • The strongest evidence is fresh. Anthropic says its Messages API, rewritten from Python to Rust, uses about 70% less CPU per request and cut median time to first token by about 20%.
  • The honest catch: a Rust function called once per item through a language bridge can be slower than the original. Move the loop, not the leaf.
  • rust-anything by Erkam Yaman turns that into a workflow: profile, port one unit with your coding agent, and switch it on only when recorded behavior matches. It's 12 days old, so treat it as promising, not proven.

On 25 November, Anthropic published how it moved the Messages API from Python to Rust. Anthropic calls it the primary way customers, and its own apps like Claude Code, reach Claude. In August, Bun shipped 1.4, its first release written in Rust instead of Zig. And OpenAI's Codex CLI has been Rust since 2025.

None of these teams moved their research to Rust. They moved the part that runs on every request.

Python is where the model is born. Rust is increasingly where it gets served.

This piece looks at why, where the trade-offs bite, and one tool that makes a small, checked first step possible.

Where Rust already sits in the AI stack

Most people picture "AI code" as a notebook full of PyTorch. That layer is still Python, and the kernels under it are still mostly C++ and CUDA. Everything between the model and the user is a different story.

A layered diagram of the AI stack, top to bottom. Research and training: Python, with PyTorch and JAX. Kernels and runtimes: C++ and CUDA, for example llama.cpp, with Mojo as the newcomer. Data and tokens: Rust, with Hugging Face tokenizers, safetensors and Polars. Serving: Rust, with Anthropic's Messages API, Hugging Face text-embeddings-inference and candle. Tools and agents: Rust, with Codex CLI, Bun, uv and Ruff. The three lower layers are highlighted in rust orange.
Where each language does the work today. Sources listed at the end.

A few of those are easy to miss because you use them from Python:

  • Hugging Face tokenizers is written in Rust and called from Python. The "fast" tokenizers in transformers are built on it.
  • safetensors stores tensors "safely (as opposed to pickle)" with zero-copy loading. Loading a pickle can run code. Loading a safetensors file can't.
  • Polars is "an analytical query engine written in Rust", and uv and Ruff are Rust tools for Python itself: a package manager and a linter. uv's own benchmarks claim it is 10 to 100x faster than pip.

Then there are the agent tools. When OpenAI announced the Codex CLI rewrite in May 2025, the reasons were specific: no Node.js requirement to install, native sandboxing bindings, "no runtime garbage collection, resulting in lower memory consumption", and a wire protocol other languages can extend. Today GitHub counts the Codex repository as almost entirely Rust.

Four reasons it keeps winning that layer

1. Overhead per request adds up at scale. Anthropic's write-up is unusually concrete. Each request is validated, turned into a prompt, tokenized and screened, and each reply is sorted into text, thinking and tool calls as it streams. "Python added overhead to every one of those steps each time it created an object, freed memory, or handed its work to the next task." Traffic grew roughly 1,800 times from early 2024 to fall 2026. At that volume, overhead per request is the bill.

2. Memory safety without a garbage collector. C++ gives you control and no safety net. Go and Java give you a safety net and a collector. Rust's compiler rejects use-after-free and double-free at build time. Google reports a 1000x lower memory-safety vulnerability density in Android's Rust code than in its C and C++, and Rust changes there had a 4x lower rollback rate and spent 25% less time in review. Bun's author gave the same reason for leaving Zig: in safe Rust, those bugs "are compiler errors".

3. One binary, no runtime. Candle, Hugging Face's Rust ML framework, states its goal plainly: "make serverless inference possible". Full frameworks like PyTorch are large and slow to start on a cluster, and Candle ships lightweight binaries. The same property is why Codex installs without Node.

4. It runs in the browser too. Candle's demos run Whisper, LLaMA2, T5 and Segment Anything entirely in the browser through WebAssembly. For edge or on-device inference, the same code compiles to a native binary and to WASM.

The compiler is a reviewer that never gets tired. That matters more now that agents write a lot of the code.

How it compares

Rust isn't the right answer everywhere. Here's the short version, as of late November 2026.

Language Best at in AI Memory safety Runtime cost Main catch
Python Research, training, glue; PyTorch and JAX live here Yes (GC) Interpreter and object overhead on every call The GIL is optional only in the free-threaded build (official since 3.14, not the default)
C++ Kernels, inference engines (llama.cpp), PyTorch's core No Lowest Memory bugs are your job
Go Network services and local runners (Ollama) Yes (GC) Low, with a collector Thin ML ecosystem; cgo for native libraries
Mojo GPU and accelerator kernels with Python-like syntax Safer than C++ by design, less proven Low Young: 1.0 and an open-source compiler only since August 2026
Rust Serving, tokenizers, data, agent CLIs, edge and WASM Yes, no GC Low Learning curve, compile times, few training libraries

The fair summary: if your problem is training a model, stay in Python. If it's a GPU kernel, look at C++/CUDA or Mojo. If it's the service, the tool or the pipeline that runs millions of times around the model, Rust has the best mix of speed and safety on offer.

The catch: the bridge has a toll

Here's the part most "rewrite it in Rust" posts skip. Anthropic didn't start with a full rewrite. It first rewrote only the output parser in Rust and called it from Python. That cut CPU by about 8 to 10%, but "each token now crossed between Python and Rust three times", and the gain didn't justify the complexity. The full rewrite of the request path is what got to about 70%.

rust-anything's own benchmark logs show the same effect on a small scale, and its FAQ says it bluntly: "Is Rust always faster? No." Its first Python, Go, Ruby and Java bridges sent every argument through JSON, which cost 10 to 50x more than the work itself. Even after rebuilding them, a Go function ported on its own ran at 0.04x of Go's speed, because Go was already fast and every call now crossed a boundary.

A chart of Rust speed against the original, measured by rust-anything, on a log scale with a line at 1x. Group one, one function called per item across a bridge: Go 0.04x, PHP 0.09x, Java 0.18x, JavaScript (vercel/ms) 0.56x, C 0.87x, Python 1.35x, Ruby 4.4x. Group two, the loop moved into Rust: vercel/ms parse over 100,000 strings counting the handover 3.4x; Fuse.js search over 50,000 strings on one core 5.3x; vercel/ms batch on all cores against one JavaScript thread 18x; Fuse.js on all cores 32x.
Same tool, same kind of code. Where the boundary sits decides the result. Per-call figures are geometric means from rust-anything's bench; batch figures compare all cores against one thread unless marked. Source: rust-anything docs, real-world.md and real-world-languages.md, Apple M4, read 28 Nov 2026.

A tiny function that V8, the JVM or Go runs in nanoseconds gets slower when every call crosses a bridge. Move the loop around it, so the data crosses once, and the picture flips: 5.3x on a single core for Fuse.js search, more with all cores. The lesson transfers to any Python service: port the hot path whole, not one leaf at a time.

rust-anything: port one piece, prove it matches

rust-anything is an open-source CLI by Erkam Yaman (MIT or Apache-2.0). It doesn't call a model itself. It's a toolkit your coding agent drives, and it works with Claude Code, Codex, Cursor, Gemini CLI and OpenCode. Sources can be TypeScript/JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin or PowerShell.

The loop: analyze plans what to port, profile finds what's hot under a real workload, and go bridges the hottest function, records what the original returns, throws and prints, hands the port to your agent, then builds, diffs and gates it. gate means every recorded case matches, plus fmt, clippy, cargo test, an unsafe check and a check that the agent didn't paste recorded answers into the port. Only then does enable switch callers to Rust, and disable switches them back.

That's the same lesson as Anthropic's post, at the size of one function: generating the port is the easy part, and the recorded behavior of the original is the oracle. Anthropic's Rust API passed about 98% of its tests in April while only about a quarter of production inputs matched, and an audit of the ported tests found more than 100 weakened assertions.

Your tests tell you what you thought of. Recorded behavior tells you what the code actually does.

The report steps cost nothing to try. I installed 5.0.2 and pointed profile at the repo's own Python eval fixture with a small loop as the workload:

$ rust-anything profile . -- python3 work.py   # output trimmed
2359 ms in all; 62% of it in the repository's own code.

  Function       File                    Total  Self     Calls
  title_case     original/textkit.py:4   78%    1129 ms  200000
  format_number  original/textkit.py:16  13%    218 ms   200000

What to port
- title_case (78%): Hot and exported by original/: port it, then check rust-anything bench.
Enter fullscreen mode Exit fullscreen mode

analyze on the same folder flagged the risks a port has to get right: five string-offset spots (Rust slices UTF-8 bytes, Python counts code points), dynamic typing and exceptions. No model was called and no file changed.

I wrapped both reports into a small read-only script that prints a shortlist and flags functions that are too cheap per call to port alone, the bridge toll from above:

https://gist.github.com/br4vesirrobin/1ef37e332bded8bc851210b4b582d258

A decision flow titled "Should this part move to Rust?". Step 1: is it hot under a real workload? If no, keep it. Step 2: is it a small function called once per item across a language bridge? If yes, move the loop around it, not the function. Step 3: do you have an oracle, such as recorded behavior or shadowed traffic? If no, build that first. Step 4: port, gate, shadow, then switch on, with a way to switch back.
Four questions before you rewrite anything.

What to watch with a 12-day-old tool

The repository was created on 16 November 2026 and had 10,993 stars, 894 forks and 60 contributors on 28 November, re-checked at 13:22 Berlin. That's a lot of attention for 12 days, and version 5.0.2 already. The good signs: the docs publish where Rust lost (the 0.56x result is on the project's own page), the status table separates "Verified" on real code (ESLint, Prettier, ffmpeg's libavutil, CPython's colorsys) from "Tested" on fixtures, and the evals page says "No results are published yet" rather than inventing a number. The author also offers paid migration consulting, which is fine, but worth knowing.

Start with analyze and profile. They're read-only. Let the agent port one unit, read the diff, and keep disable one command away.

The short version

Rust won't train your next model, and it doesn't need to. The value is in the layer that runs on every request: the tokenizer, the weight loader, the API server, the agent on your laptop. That's where garbage collection, interpreter overhead and memory bugs cost the most, and where Anthropic and OpenAI have already moved.

If you try it, start where it's measurable: one hot path, one recorded oracle, one switch you can flip back.

What's the one part of your AI stack you'd move first?

Sources

Top comments (0)