Basics
Agents
Pro Tips
Updates
Set Up Local Agents for Formal Verification in Lean
Apr 23, 2025
Building formal verification tools requires powerful language models that can assist with complex reasoning tasks. This post will guide you through setting up a minimal local agent that can be used for formal verification of smart contracts or mathematical proofs.
Prerequisites
Before getting started, you'll need to:
Download and install Ollama - a tool for running language models locally
Install uv - a fast Python package manager
Step 1: Download a Model
Ollama lets you run various language models locally. For formal verification work, consider one of these options:
Start the Ollama server:
Step 2: Set Up the Environment
Clone the repository and set up a virtual environment:
Step 3: Initialize Your Model
You can now use the formal verification agent in your projects. The sample code below demonstrates how to use different models to solve a simple theorem about coprime numbers:
Step 4: Run Your Agent
Execute your agent with:
The output will show you the tactics suggested by the model to prove the theorem:
The Cost-Effective Approach to Formal Verification
Building secure software has become increasingly accessible, and there's no longer any excuse for failing to identify critical bugs. As I shared in a recent LinkedIn post, we're reaching a stage where you can simply instruct an AI agent to "Please ensure my software/service is secure" and get meaningful results.
These agents can:
Start logical processes and provide proofs with specific guarantees
Analyze application logic within its context
Construct formal proofs by examining the context and reviewing all functions in your code
Transform informal specifications into formal proofs
However, cloud-based AI infrastructure remains expensive. Using services like Claude API can cost significant amounts - I spent $1.39 in just 25 minutes for a handful of inferences in Lean! Commercial deployment options often start at around $0.70 per hour, which translates to approximately $500 per month - a steep investment for early-stage startups.
The solution: stick to local setups. Even modest hardware like a 16GB Intel MacBook can run inference effectively. The setup described in this post costs $0 to operate (apart from your electricity bill), making formal verification accessible to everyone.
Next Steps
With this local setup, you can now start building more sophisticated formal verification tools. You could:
Create specialized prompts for software verification
Develop agents that can translate your code e.g Solidity to Lean theorems
Build interactive proof assistants that guide users through the verification process
Local agents provide privacy, lower latency, customization options, and most importantly, a cost-effective approach to formal verification that makes building secure software the standard, not the exception.