Lean4Agent: Formal Modeling and Verification for Agent Workflow and Trajectory
summary
The gist
Equipping Large Language Models (LLMs) to execute reliable multi-step workflows has become a central challenge in artificial intelligence, necessitating formal methods for specifying, verifying, and
In short
Lean4Agent is a framework using dependent-type formal language to model, verify, and refine LLM agent workflows. It uses three layers—structural, semantic, and runtime trajectory verification—to ensure correctness from design through execution. This allows for 'correct-by-construction' workflow design and automated refinement of agent systems.
Key concepts
- Layer 1: Structural Verification
- This layer checks the basic structure of the agent workflow using a graph. It defines data types for variables and execution nodes, ensuring that the workflow graph is well-formed, similar to compiler checks for programs. It models the flow of execution through defined nodes and edges.
- Layer 2: Semantic Verification
- This layer uses a predicate system to check if the workflow makes sense under assumptions about local LLM correctness. It verifies that each step's preconditions are met by previously established predicates, ensuring static semantic soundness before deployment.
- Layer 3: Runtime Trajectory Checking
- This layer monitors actual execution paths (trajectories) using 'LLM-as-judge modules' and external validators. It analyzes the state of the execution against step-level postconditions to localize exactly which steps cause failures during runtime.
- LeanEvolve
- This is a dual-mode refinement approach that uses trajectory analysis to guide workflow improvement. The formal mode suggests specific revisions based on diagnostics, while a pure LLM mode allows for broader exploration of task outcomes.
Terminology used across episodes
This episode discusses
- Lean4Agent: Formal Modeling and Verification for Agent Workflow and Trajectory · Paper Radio
- Seed-Prover 1.5: Mastering Undergraduate-Level Theorem Proving via Learning from Experience
- ELAIPBench: A Benchmark for Expert-Level Artificial Intelligence Paper Understanding
- Towards Verifiably Safe Tool Use for LLM Agents
- Lean4Physics: Comprehensive Reasoning Framework for College-level Physics in Lean4
- Goedel-Code-Prover: Hierarchical Proof Search for Open State-of-the-Art Code Verification
- LLM-based Agents Suffer from Hallucinations: A Survey of Taxonomy, Methods, and Directions
- Goedel-Prover: A Frontier Model for Open-Source Automated Theorem Proving
- Goedel-Prover-V2: Scaling Formal Theorem Proving with Scaffolded Data Synthesis and Self-Correction
- ToolGate: Contract-Grounded and Verified Tool Execution for LLMs
- VeriGuard: Enhancing LLM Agent Safety via Verified Code Generation
- DeepSeek-Prover-V2: Advancing Formal Mathematical Reasoning via Reinforcement Learning for Subgoal Decomposition
- A Self-Improving Coding Agent
- Kimi K2.5: Visual Agentic Intelligence
- MiroThinker-1.7 & H1: Towards Heavy-Duty Research Agents via Verification
- Multi-Agent Collaboration Mechanisms: A Survey of LLMs
- PDL: A Declarative Prompt Programming Language
- AgentSPEX: An Agent SPecification and EXecution Language
- TheoremLlama: Transforming General-Purpose LLMs into Lean4 Experts
- Let's Reason Formally: Natural-Formal Hybrid Reasoning Enhances LLM's Math Capability
- MA-LoT: Model-Collaboration Lean-based Long Chain-of-Thought Reasoning enhances Formal Theorem Proving
The paper
Lean4Agent: Formal Modeling and Verification for Agent Workflow and Trajectory · Read on arXiv
Ruida Wang, Jerry Huang, Pengcheng Wang, Xuanqing Liu, Luyang Kong Tong Zhang
University of Illinois Urbana-Champaign
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.
Transcript
Introduction to the show: ident: AI Radio. Generated commentary on the latest Artificial Intelligence papers.
Tom: We're looking at the paper: "Lean4Agent: Formal Modeling and Verification for Agent Workflow and Trajectory".
Jane: Here's the gist of the paper. Equipping Large Language Models (LLMs) to execute reliable multi-step workflows has become a central challenge in artificial intelligence, necessitating formal methods for specifying, verifying, and debugging agent execution trajectories The gist is that Lean4Agent is the first framework using dependent-type formal language to uniformly model, verify, and refine LLM agent systems.
Tom: How it works Lean4Agent launches FormalAgentLib, an extensible three-layer Lean4 library for formally modeling and verifying agent behaviors across three levels of correctness: structural, semantic, and runtime trajectory Layer 1 verifies the structural well-formedness of the agent workflow through a workflow graph.
Jane: This layer defines data types for variables and execution nodes, enabling verification of "structural well-formedness" analogous to "compiler-level checks for programs".
Tom: It models an agent workflow as a heterogeneous graph G:= ⟨V, E⟩ where each node is represented as vi:= ⟨ri, wi, ti, τi⟩.
Title and authors: Lu: The paper introduces FormalAgentLib, which is an extensible three-layer Lean4 library for modeling and verifying agent behaviors across three levels of correctness: structural, semantic, and runtime trajectory.
Meng: Three layers of correctness sounds complex. Can you break down what each layer actually checks? What’s the first thing it looks at?
Lalam: Layer one verifies the structural well-formedness of the workflow through a workflow graph, defining data types for variables and execution nodes to check for structural errors, like making sure downstream nodes can actually read variables produced in parallel branches <ref:2606.06523#pg1>.
Tom: That makes sense. So Layer two is about what <ref:2606.06523#pg1>? Is it checking if the logic makes sense?
Lu: Layer two develops a predicate-based system to model the static semantic soundness of the workflow under explicit assumptions about local LLM correctness <ref:2606.06523#pg1>. It checks whether each step’s preconditions are entailed by previously established predicates, assuming that if an LLM step is executed correctly, it will produce a context satisfying its postconditions.
Jane: So it’s checking the logic *before* it runs, based on what we assume the AI steps can do? That’s interesting because that gets to the core issue of how we trust these models.
Meng: Okay, so Layer three sounds like where things get tricky—the actual execution <ref:2606.06523#pg2>. What does that layer do in practice? Does it just run tests?
Lalam: Layer three checks whether the LLMExec assumption holds on particular execution trajectories by using modules called "LLM-as-judge" and external Python validators to analyze the state against step-level postconditions, which lets them localize the failure-inducing steps <ref:2606.06523#pg2>.
The paper's summary: Lu: Beyond FormalAgentLib, they propose LeanEvolve, which is a runtime workflow-refinement method driven by trajectory verification and optional environment feedback. It uses the verification results from FormalAgentLib and an LLM together to identify flaws in current workflows and revise the specifications.
Meng: So if it finds a flaw at runtime, what does that mean for deployment? Does it just re-prompt the model, or is there something more systematic?
Lalam: LeanEvolve’s first mode is formal-guided evolve, which uses Layer three’s trajectory analysis to localize the likely failure-inducing step and violated predicates to give an LLM information to revise that specific part of the workflow <ref:2606.06523#pg2>.
Tom: So it doesn't just guess what went wrong; it points to the exact step and tells the AI how to fix that specific part. That sounds much better than a general system re-prompt.
Jane: And there’s also a pure LLM evolve add-on mode, which does a stepwise rewrite without any formal guidance, just to explore other possibilities. So they give you a choice in how you want to refine the workflow?
Lu: The paper shows that these verification-passing workflows outperform failing ones by an average of eleven point nine four percent on SWE and achieve an additional average improvement of seven point four seven percent on the SWE task with LeanEvolve.
Meng: An eleven point nine four percent gain is significant, especially in a field where we’re still figuring out how to make these agents robust at scale. So they have some solid experimental data there, I guess.
The paper's improvements: Tom: So we're wrapping up on Lean4Agent and LeanEvolve. Basically, this work gives us a framework that uses dependent type theory to model and verify agent behaviors, addressing the challenge of specifying and debugging complex agent execution trajectories.
Jane: It sets up a solid foundation for verifiable LLM-agent systems by decomposing behavior into structured steps and translating natural language requirements into checkable predicates.
Lu: It establishes a unified basis for modeling long-horizon black-box systems, which is something we really need as these agents get more complex.
Meng: I'm still thinking about the cost of running this verification system on actual traffic, but conceptually, this is the way forward for making these agents safer in high-stakes domains.
Lalam: The framework provides a promising foundation for verifiable, self-improving LLM-agent systems through dependent type formal languages and it offers principles for modeling long-horizon black-box systems >
Conclusion: Tom: So we’ve gone through how they build this whole three-layer system, from checking the basic structure to verifying what happens when it actually runs on a trajectory. It’s about using dependent type theory to give us formal proof of correctness for these agent workflows.
Jane: Exactly. The naive question is, why do we need this level of formal verification for agents? Because without it, when you have long-horizon tasks where the AI has to make many decisions in a sequence, those errors can get really hard to track down later.
Lu: What I find interesting is how they decompose the problem into these distinct layers of correctness. Layer one handles the structural stuff—making sure the workflow graph actually makes sense, like ensuring nodes can read what other nodes write, which stops you from having a "read-consistency error."
Meng: But on my side, I’m thinking about deployment. If we have a workflow that passes all these formal checks but still fails in production because of something the formal model didn't capture, does it actually help us deploy faster? What about the cost of running this verification layer at scale?
Lalam: The framework is designed to handle black-box LLM executions by breaking them down into structured steps and translating natural language requirements into checkable predicates. I’m pretty sure that decomposition is the key to making it work with these massive models.
Tom: And then you have LeanEvolve, which takes all that analysis and uses it for refinement. It lets the system localize the failure-inducing step by comparing the actual execution trajectory against those formal postconditions.
Jane: That’s where things get practical for a user. Instead of just getting a vague error message from an LLM, you get targeted feedback on exactly which part of your workflow needs revision, and then you can actually fix that specific part.
Lu: The experiments show that these verified workflows are significantly better than the ones that aren't verified. We’re seeing improvements like seventeen point eight percent on the SWE task just by using LeanEvolve to guide the repair process.
Meng: That seventeen point eight percent gain is solid, but I still see a gap between that lab result and what happens when we run this on real-world, noisy traffic where the LLM's behavior isn't perfectly modeled. We need to know how robust these formal checks are against genuine runtime surprises.
Lalam: The system is extensible too, so you can add your own specific predicates using that 'ext' type if you want to model those tricky semantic gaps that the core system might miss. I’m confident in its ability to identify context management errors before they ever roll out.
Jane: So, this isn't just a theoretical exercise anymore; it’s giving developers a concrete tool to build things that are more predictable and reliable, even when working with these complex language models. It moves us toward designing systems that are correct by construction.
Tom: Yep. To wrap up, "Lean4Agent: Formal Modeling and Verification for Agent Workflow and Trajectory" gives us a unified way to model, verify, and refine agent systems using dependent type theory across structural, semantic, and runtime levels.
Lu: It sets a path forward for modeling long-horizon black-box systems with the rigor we need as these agents get bigger.
Meng: Next up on our show, we’re looking at how embedding models are improving probabilistic race prediction. We'll see if that kind of representation learning can give us any useful structure when dealing with complex sequence prediction tasks.
More episodes
- 2505.06335-Remote Rowhammer Attack using Adversarial Observations on Federated Learning Clients
- 2506.07121-Quality-Diversity Red-Teaming: Automated Generation of High-Quality and Diverse Attackers for Large Language Models
- 2506.16590-Energy-Based Transfer for Reinforcement Learning
- 2507.00657-Generative Exaggeration in LLM Social Agents: Consistency, Bias, and Toxicity
- 2508.08370-The DNA of nuclear models: How AI predicts nuclear masses
- 2508.10954-UniPrompt-CL: Efficient Single-Inference Continual Learning for Medical Image Classification
- 2509.10528-STM-Graph: A Python Framework for Spatio-Temporal Mapping and Graph Neural Network Predictions
- 2509.25045-Hyperdimensional Probe: Decoding LLM Representations via Vector Symbolic Architectures
- 2510.06383-Protecting De-identified Documents from Search-based Linkage Attacks
- 2510.11953-Sculpting the Latent Space: Disentanglement with WAE-MMD and Programmable Priors