TL;DR
Automated theorem provers are getting great at generating mathematically valid proofs, but their outputs are often bloated, slow to verify, and fragile across software updates. Researchers at Emergence AI built an agentic search pipeline that compresses formal proofs in Lean 4 by over 80% in both length and compute cost—all under a strict $3 API budget per problem. Their secret wasn't complex prompt engineering, but rather building an ultra-fast compiler feedback loop and maximizing the sheer number of verified guesses that $3 could buy.
What Problem Does This Paper Address?
Formal verification languages like Lean 4 act as mathematically infallible code compilers: if your proof compiles, it is 100% true. Increasingly, AI agents write these proofs. However, code written purely to pass a check is rarely economical. Bloated proofs slow down library build times and break easily when Lean releases new versions.
To make "proof economy" measurable, the Lean Refactor Arena challenges entrants to rewrite existing Lean 4 proofs to optimize three exact metrics:
- Length: Fewer syntax tokens (measured by Lean’s internal lexer, not standard LLM subword tokenizers).
- Compute ("Heartbeats"): Fewer deterministic steps required by the Lean compiler to verify the proof. Crucially, a tiny two-token shortcut (like
by grind) might triple elaboration compute, tanking the score. - Compatibility: The proof must still compile across multiple specified Lean versions.
There is one major catch: in the closed track, you are capped at $3.00 of LLM API spend per problem. Because you cannot accurately estimate token counts or compiler heartbeats from raw text, you have to measure every candidate inside Lean itself.
Methods
The authors treat proof refactoring as an optimization search against an exact, compiler-defined cost function, keeping a strict "incumbent" best proof that is only replaced if a new candidate compiles everywhere and achieves a strictly higher score.
Their four-stage pipeline introduces clever engineering around a frozen frontier LLM (gpt-6.1-sol):
- Fast Compiler Server (Novel Engineering): Compiling a whole Lean file from scratch took ~4.5 minutes—far too slow for iterative search. The authors spliced a custom Lean metaprogram directly above the target theorem. Lean loads the heavy file imports once, then evaluates candidate proofs in memory in a median of 5.7 seconds. It also extracts fine-grained diagnostics (open goals, unused steps, per-tactic heartbeat costs).
- Budget-Blind Agent Harness: Early tests showed that telling the LLM about its $3 budget made it overly conservative, quitting around $1.20. Instead, the agent is kept blind to its spend; a passive monitor forecasts turn costs and instructs the agent to wrap up right before hitting $3. To prevent the agent from getting stuck in a rut, sessions periodically flush their conversation history and restart from the current best proof. A smaller model (
gpt-6-luna) acts as a cheap "librarian" to look up valid theorem names. - Deterministic Polishing: After the API budget is spent, a non-LLM Lean optimizer spends up to an hour applying verified local edits—like delta-debugging (deleting unneeded steps) and collapsing final steps into single automation tactics—at zero API cost.
Datasets
No models were trained or fine-tuned. The system was developed on a 15-problem warm-up set and evaluated on the Lean Refactor Arena final benchmark: 60 real-world theorems drawn equally from five active Lean 4 repositories (Strata, PhysLib, CSLib, ArkLib, LeanPool) and PutnamBench (university-level math competition problems), spanning eight Lean toolchains (v4.26 to v4.35-rc3).
Key Insights and Ideas
- Shots on goal beat cleverness: Across 25+ development runs, performance tracked one thing above all: how many verified compiler evaluations $3 could buy. Switching to a 3x cheaper model per turn, spending the full $3 allowance via adaptive stopping, and using half-price "Flex" tier API tokens each drove major score jumps.
- What didn't work is surprising: Giving the agent extra help—automated lemma suggestions, offline pattern searches, planner-worker architectures, forced-diversity phases, or a list of 30 curated "code golf" idioms—yielded zero improvement (or actively hurt scores by eating up turns). Once the compiler gives exact feedback, a frontier model figures out the conceptual compression on its own; it just needs turns to refine it.
- Conceptual leaps vs. micro-edits: In one Putnam competition problem, the LLM recognized a 294-line (1,754-token) proof as a specific instance of an existing Mathlib theorem on intersecting sets, collapsing it to 27 tokens. Conversely, the deterministic non-LLM optimizer added only +0.07 to the final score because the LLM's proofs were already near local perfection.
Key Results
On the 60-problem final benchmark, the submission achieved an official combined arena score of 87.88:
- 80.2% reduction in proof length (85,453 total tokens shrunk to 11,356).
- 83.4% reduction in compiler heartbeats (7.44M kHB down to 0.58M kHB).
- 100% cross-toolchain compatibility (passing all 67 secondary version checks).
- Strict budget compliance: Averaged $2.83 per problem (max $2.97, total $169.89), verified to the exact cent against API gateway logs. PutnamBench compressed the most (97.86 score), while PhysLib compressed the least (74.14) due to ultra-short proofs bottlenecked by irreducible kernel computation.
Limitations and Future Work
- Hitting a structural wall on length: The biggest remaining gap is proof length, where a third of the remaining tokens are just lemma names. Closing this gap requires discovering fundamentally different proof structures early on—currently, the agent locks into its final proof structure within turns 4 to 8 and spends the rest of its budget polishing.
- Soft budget forecasts: Because the pipeline sets no hard output-token cap per API call, an unusually massive reasoning response could theoretically overshoot the $3 limit (though the closest call in practice still left a $0.03 buffer).
- Hidden compute: While LLM API spend was strictly under $170, the closed track does not cap compilation compute—reproducing the 4.3-hour run requires a Kubernetes cluster running ~130 warm Lean pods simultaneously.
Summary updated