AxDafny: Agentic Verified Code Generation in Dafny

Benjamin Breen, Austin Letson, Borja Requena Pozo, Leopoldo SarraarXiv 2026

TL;DR

AxDafny introduces an agentic framework that uses iterative verifier feedback to jointly synthesize executable code and formal mathematical proofs in the Dafny programming language. Alongside a new competition-grade benchmark (LCB-Pro-Dafny), the authors demonstrate that verifier-guided repair dramatically boosts verification success rates, though bridging the gap between formal correctness and runtime efficiency remains an open challenge.

What Problem Does This Paper Address?

Large Language Models (LLMs) are remarkably good at generating code, but ensuring that code is actually correct remains a massive hurdle. Traditional software engineering relies on unit tests, which only check a finite set of inputs and can easily miss edge cases.

Formal verification offers a powerful alternative: it mathematically proves that a program satisfies its specification for all possible inputs. Dafny is a programming language designed exactly for this, requiring developers to write both the executable code and the proof annotations (like loop invariants and assertions) that the compiler's verifier checks.

However, writing these formal proofs is notoriously difficult and requires deep expertise. While prior AI research has focused on "proof synthesis" (fixing proofs for existing code), this paper tackles the harder problem of joint program and proof synthesis—generating both the working code and its mathematical proof from scratch.

Methods

The authors introduce AxDafny, an agentic framework built around an iterative generate-check-repair loop. It consists of three core components:

  1. The Proposer: An LLM that generates or refines the Dafny code along with its proof annotations.
  2. The Reviewer System: A two-stage validation process. First, deterministic checks ensure the model hasn't "cheated" by weakening the problem's specifications or using proof-bypass keywords (like assume or {:verify false}). Then, it runs dafny verify. If verification fails, the exact error messages and counterexamples are collected. Finally, an LLM-as-judge inspects the code for high-level logical bypasses.
  3. Context Management (Memory): To prevent the agent from running in circles, AxDafny maintains two types of memory: the verbatim code and feedback from the immediate previous attempt, and a rolling "scratchpad" summary that condenses lessons learned across all prior iterations.

Datasets

The paper evaluates AxDafny on two benchmarks:

  • DafnyBench: An established benchmark used to evaluate proof synthesis (filling in missing invariants and assertions for already-written code).
  • LiveCodeBench-Pro-Dafny (LCB-Pro-Dafny): A new benchmark introduced in this paper to evaluate joint program and proof synthesis. It contains 250 competition-style problems (100 easy, 100 medium, 50 hard) translated from Python into Dafny, complete with formal specifications.

Key Insights and Ideas

  • Verifier Feedback Scales Well with Iteration: For simple proof-filling tasks, the benefits of verifier feedback materialize quickly within the first 5 iterations. However, for full end-to-end program synthesis, the agent derives sustained benefits from a larger iteration budget.
  • Correctness Does Not Equal Efficiency: A fascinating finding emerged when the authors compiled verified Dafny code back into Python to run against original competitive programming test suites. Many formally "correct" programs failed due to Time Limit Exceeded (TLE) or Memory Limit Exceeded (MLE) errors. This is because formal specifications typically enforce functional correctness, not asymptotic complexity. An agent might write a highly inefficient algorithm that is easy to mathematically prove, but terrible to execute.
  • The Dafny Data Drought: LLMs struggle with languages like Dafny due to data scarcity. A GitHub code search reveals over 400 million Python files compared to just 120,000 Dafny files—a massive three-orders-of-magnitude gap.

Key Results

AxDafny achieves state-of-the-art performance across both benchmarks:

  • DafnyBench: AxDafny paired with Gemini-3.1-Pro successfully verified 92.7% of instances, outperforming the previous best baseline (DafnyPro) by 6.5 percentage points—notably achieving this without relying on a external proof-hint retrieval library.
Figure 2. Cumulative AxDafny pass rate on DafnyBench vs. itera- tion budget across model settings. The primary feedback gains are heavily concentrated within the first 5 iterations.
  • LCB-Pro-Dafny: AxDafny achieved an overall verification success rate of 56.4% across the 250 problems. This is a massive leap over the direct, non-iterative GPT-5.5 baseline, which only managed an 11.6% success rate.
Table 2. Results on LCB-Pro-Dafny by difficulty split. Each entry is the verification success rate (%).

As shown below, the end-to-end coding tasks show a steady, continuous improvement as the iteration budget increases up to 20 attempts:

Figure 3. Cumulative AxDafny pass rate on LCB-Pro-Dafny across difficulty splits. Accuracy scales steadily with an increased iter- ation budget across all levels. Compared to proof-hint filling on DafnyBench, end-to-end program synthesis derives more sustained benefits from more iterations.

Limitations and Future Work

While AxDafny proves the viability of verifier-guided coding agents, two major limitations remain:

  1. Algorithmic Complexity: The framework still struggles on the "hard" split of competitive programming tasks, verifying only 28.0% of them.
  2. The Runtime Gap: Future coding agents need to optimize for more than just a green checkmark from the verifier. Incorporating resource constraints (like time and memory complexity) directly into the agent's reward loop or formal specifications is a critical next step for making verified code generation practical in the real world.

Summary updated

BibTeX
@misc{breen2026axdafny,
  author = {Benjamin Breen and Austin Letson and Borja Requena Pozo and Leopoldo Sarra},
  title = {{AxDafny: Agentic Verified Code Generation in Dafny}},
  year = {2026},
  eprint = {2606.32007},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2606.32007},
}
Figure