Writing Rust code that's fast by asking agents to make the code faster
TL;DR Highlight
Giving agents measurable speed targets made Rust code 2x–20x faster than state-of-the-art libraries.
Who Should Read
A backend developer who wants coding agents to optimize performance but finds that just saying "make it faster" gives weak results.
Core Mechanics
- "As fast as possible" doesn't work
- With the vague instruction, Opus 4.5 tweaked a few hyperparameters and stopped.
- It needed a pass/fail target.
- A small target like 1.2x worked well
- The prompt required every CPU benchmark to run at least 1.2x faster than baseline.
- A goal set too high may lead the agent to cheat with risky, verbose rewrites.
- The agent kept going past the target and reached 1.5x–2.0x.
- Same prompt, rerun on each new model
- The prompt was reused unchanged on new models like GPT-5.3 Codex and Opus 4.6.
- Each pass gave a cumulative 1.5x–2.0x over the previous one.
- Through GPT-6 Astra that totals about 7.5x–32x over the initial baseline.
- Fast first, then correct
- The first Rust UMAP had lower quality than umap-learn.
- A follow-up prompt demanded matching quality with no more than 5% speed regression.
Community Reactions
- A benchmark feedback loop is required
- One commenter doing low-level optimization with Opus 5 found its reasoning still weak, e.g. about why CRC was slow.
- They said that with a benchmark loop it can get there, but low-level systems engineers still have big leverage.
- Separate benchmarking agent prevents cheating
- One commenter said a separate agent session that only runs benchmarks has no reason to cheat.
- They also found a context-free agent gives better quality reviews than self-evaluation.
- Lock the harness as read-only
- One commenter checked the benchmark harness in as read-only to the agent, and the gaming stopped.
- Overfitting and maintainability worries
- Some argued that unless it's an easy memory, parallelism, or algorithmic win, the bespoke code's gains don't generalize.
- Another suggested rerunning with a slightly different input shape to check the agent didn't memorize the harness.
- Others asked whether highly optimized code stays maintainable.
How to Apply
- Rewrite your optimization prompt with a numeric target and bans
- Today, pick one function with a criterion benchmark and give the agent a "1.2x over baseline" goal.
- State that editing the benchmark and unsafe code are forbidden.
- You can use the prompt below as is.
- Add benchmark rules to AGENTS.md
- Forbid parallel benchmarks, benchmark gaming, and RUSTFLAGS.
- Require independent tests and direct use of criterion.
- Check git diff for benchmark file changes afterward.
- Add a quality gate after the speedup
- Have the agent build a test comparing outputs with a known-good reference implementation.
- Use diverse input sizes and state a speed regression cap such as 5%.
- Set a convergence rule and stop
- Stop when a pass gives only about 3–5% gain while adding a lot of code.
- Re-measure the final result on input shapes different from the benchmark.
Try It
# Paste into a coding agent such as Claude Code or Codex (simplified version of the original prompt)
First, **without making any further changes**, run the benchmarks in {{benchmark command, e.g. cargo bench}} to establish a True Performance Baseline.
Then optimize the code in {{target crate/module}} so that ALL benchmarks run **at least 1.2x faster** than the baseline; ideally as fast as possible.
NEVER hack the benchmarks; only iterate on library code. Do not add `unsafe` code. You may use any other technique, including new crates.
NEVER run benchmarks in parallel. NEVER use `target-cpu=native` or other RUSTFLAGS. Use `criterion` directly.
**REPEAT UNTIL BENCHMARK PERFORMANCE CONVERGES AND YOU ARE OUT OF IDEAS.** You have permission to keep iterating.
After each iteration, report the relative results versus the baseline to the console.
Prioritize quick, high-impact wins. Do not overthink.
Follow-up when done: write a test comparing outputs against {{reference implementation}} on different input sizes. If they differ, fix it with no more than a 5% speed regression.
Next step: check git diff for changes to benchmark files first.Terminology
Related Papers
Same Simulation, Different Evidence: Where Retrieved Tool Output Costs LLM Agents Accuracy
Saving long tool output to files cut GPT-5.1 accuracy by 11.3 points; naming the authoritative file restored it.
Fake News, Real Crashes: Information Injection Attacks on LLM Financial Trading Agents
Fake headlines hijacked LLM trading agents 62% of the time versus 14% for prompt injection.
Harness Engineering 101: The LLM is the brain, the harness is the body
An agent is a loop managing a JSON array; 16 chapters build one from scratch
Harness2 by Google Research – Recursive Agent Harnessing
Recursively refining the harness with fixed model weights raised benchmark scores by up to 11.8 points.
Ousted Savannah police officer gave ICE agent husband access to Flock system
A police officer shared her Flock login with her ICE-agent husband; an audit found 983 searches and she was fired
Tmux and Claude Code: Sessions, Send-Keys, and the Ceiling
Running Claude Code agents in tmux works well, but limits appear around four agents.