Uses Lean4 and the FormalAgentLib library to formally model and verify LLM-agent workflows and execution trajectories.
Abstract
Equipping Large Language Models (LLMs) to execute reliable multi-step workflows has become a central challenge in artificial intelligence. Despite recent advances in LLMs' agentic capabilities, most agent systems still lack formal methods for specifying, verifying, and debugging their workflow and execution trajectories. This challenge mirrors a long-standing problem in mathematics, where the ambiguity of natural languages (NLs) motivates the development of formal languages (FLs). Inspired by this paradigm, we propose **Lean4Agent**, to the best of our knowledge, the first framework that uses Lean4, a dependent-type FL to model and verify agent behavior. **Lean4Agent** launches **FormalAgentLib**, an extensible Lean4 library for formally modeling and verifying agent workflows' semantic consistency under explicit assumptions, and enabling localization of execution-time failures revealed by trajectories. Building on **FormalAgentLib**, we further develop **LeanEvolve**, which applies results in **FormalAgentLib** to revise workflows to enhance its capability. Extensive experiments on a hard problem subset of SWE-Bench-Verified and a subset of ELAIP-Bench across 5 leading LLMs indicate that the verification-passing workflows outperform the failing ones by an average of **11.94%**, and **LeanEvolve** further improves SWE performance by **7.47%** on average. Furthermore, **Lean4Agent** establishes a foundation for a new field of using expressive dependent-type FL to formally model and verify agent behavior.
Problem
Most LLM-agent systems lack formal methods for specifying, verifying, and debugging their workflows and execution trajectories.
Approach
Lean4Agent uses Lean4, a dependent-type formal language, to model and verify agent behavior. It launches FormalAgentLib, a three-layer Lean4 library that checks the semantic consistency of agent workflows under explicit assumptions and localizes execution-time failures from trajectories. LeanEvolve builds on these verification results to revise workflows.
Figure 1: Lean4Agent Framework: The Lean4Agent framework consists of two main components. (a) FormalAgentLib is a three-layer Lean4 library for formally modeling and verifying agent behaviors. Layer 1 verifies the structural correctness of the workflow through a workflow graph. Layer 2 develops a predicate (pred.) system to model pre- and post-conditions of agent executions and verify the semantic
Results
On a hard subset of SWE-Bench-Verified and a subset of ELAIP-Bench across five LLMs, verification-passing workflows outperform failing ones by an average of 11.94%, and LeanEvolve improves SWE performance by a further 7.47% on average.
Model
Passed
Failed
Diff.
GPT-5.2
62.67%
50.00%
12.67%
Kimi-K2.5
61.33%
46.00%
15.33%
Gemma-4-31B
60.00%
32.67%
27.33%
Avg score of verification-passing vs failing workflows (SWE-Bench-Verified)