LLMLean

LLM-powered automation for Lean.

Lean 4.35.0 CI MIT license

Installation · Providers · Usage · Demo · Configuration

LLMLean integrates LLMs and Lean for tactic suggestions, proof completion, proof optimization, and more.

Invoke llmqed on the Cauchy–Schwarz inequality, then receive a proof suggestion checked by Lean to close all goals. Click Try this to insert it.

You can use an LLM running on your laptop or in the cloud.

llmstep suggests a step, llmqed completes a proof, and llmoptimize rewrites a proof. Lean checks suggestions from cloud models (OpenAI or OpenRouter) or local models (LM Studio or Ollama).

Usage

LLMLean is used interactively in VS Code with the Lean extension. Suggestions appear in Lean's Infoview.

Suggest the next step

Use llmstep for a tactic that makes progress on the current goals:

example (n : Nat) : n + 0 = n := by
  llmstep

You can also give a prefix, such as llmstep "apply ".

Complete a proof

Use llmqed for a completion that closes all remaining goals:

example (P Q : Prop) (h : P ∧ Q) : Q ∧ P := by
  llmqed

Both tactics check suggestions in Lean and can retry with Lean's error messages. Click Try this in the Infoview to insert a suggestion into your proof.

Watch the llmqed proof completion demo

The demo project has examples to try in VS Code, with worked solutions.

Rewrite a proof

Use LLMLean: Optimize proof to shorten a proof. At equal length, it prefers fewer explicitly named lemmas.

 theorem swap (P Q : Prop) (h : P ∧ Q) : Q ∧ P := by
-  have hp : P := h.left
-  have hq : Q := h.right
-  exact ⟨hq, hp⟩
+  grind

Watch the llmoptimize proof rewrite demo

Inspired by the proof optimization metrics in ImProver 2.

Proof optimization is also available on the CLI.

lake exe llmlean optimize LLMleanDemo/Demo.lean --decl Demo.swap

See the LM Studio dropdown below to use LLMLean-Optimize-9B, a local model fine-tuned for Lean proof optimization.

Installation

Requires Lean 4.35.0.

  1. Add LLMLean to your project's lakefile.lean:

    require llmlean from git
      "https://github.com/cmu-l3/llmlean.git"
    
  2. Run lake update llmlean from your project directory.

  3. Import LLMLean in your Lean file:

    import LLMlean
    

Setup

OpenAI with gpt-6-luna is the default. To customize the provider or model, add llmlean.toml to your project root.

OpenAI · Cloud models · Default

Get an OpenAI API key and set it in your terminal:

export OPENAI_API_KEY="<your-api-key>"

No configuration file is needed. To customize the model, create llmlean.toml in your project directory:

provider = "openai"
model = "gpt-6-luna"

Quit VS Code, then open your project from the same terminal so that it picks up the key:

code .
OpenRouter · Cloud models

Set your OpenRouter key in your terminal:

export OPENROUTER_API_KEY="<your-api-key>"

Create llmlean.toml in your project directory, using a model identifier from OpenRouter:

provider = "openrouter"
model = "<model-id-listed-by-the-provider>"

Quit VS Code, then run code . from the same terminal so that it picks up the key.

LM Studio · Local models

Load a model in LM Studio and start its local server. Create llmlean.toml in your project directory, using the model identifier shown in the Developer tab:

provider = "local"
base_url = "http://localhost:1234/v1"
model = "qwen/qwen3.5-9b"
reasoning_effort = "none"

No API key is needed for the default local server. For this model, reasoning_effort = "none" turns thinking off for LLMLean requests.

LM Studio · LLMLean-Optimize-9B

Download and serve the model with the LM Studio CLI.

lms get l3lab/llmlean-optimize-9b-v0.1
lms load l3lab/llmlean-optimize-9b-v0.1 --identifier llmlean-optimize-9b-v0.1
lms server start --port 1234

Create llmlean-optimize.toml in your project root.

provider = "local"
base_url = "http://localhost:1234/v1"
model = "llmlean-optimize-9b-v0.1"

Optimize proof samples four rewrites by default and suggests the best improvement that Lean verifies.

For other servers and connection details, see the provider guide. Local servers must support the Responses API.

Customizing LLMLean's proof optimization

To customize LLMLean's proof optimization features (see Rewrite a proof), create llmlean-optimize.toml in your project root. Otherwise, proof optimization uses your existing LLMLean settings, including those from llmlean.toml.

Set optimize_candidates (1–32) to change how many local rewrites to sample. With Lean 4.35 and vLLM, set optimize_retrieval_tokens = 1024 to include up to 1,024 tokens of ranked project theorem signatures. Retrieval is off by default.

Project context

LLMLean uses context from both the current file and imported files in your project. Along with the current goals and proof so far, it includes earlier definitions, lemma statements, and notation to help the model use results you have already proved.

Context from the current Lean file and imported project files is combined into a prompt for the LLM.

Use llmcontext inside a proof to preview the prompt without making a model request. See context selection for budgets and comparison modes.

Documentation

GuideContents
Demo projectExamples and worked solutions in VS Code
ConfigurationModels, generation budgets, project context, and Lean options
BenchmarksExperiments comparing context policies and proof completion
ProvidersCloud APIs and local server configuration
MigrationUpdating from older versions

Development

Issues and pull requests are welcome. To run the checks from the repository directory:

lake build llmlean
lake test
python3 scripts/test_transport.py

Mathlib examples are in LLMleanTest.

Run python3 scripts/benchmark_context.py for offline context benchmarks covering long proofs, many declarations, and direct/transitive imports. --prove opts into model-backed comparisons using your configured provider.

Citation

LLMLean evolved from llmstep. If you would like to cite LLMLean in an academic context, please cite the llmstep paper (NeurIPS 2023 Workshop on MATH-AI):

@inproceedings{welleck2023llmstep,
  title = {{LLMSTEP}: {LLM} proofstep suggestions in {Lean}},
  author = {Welleck, Sean and Saha, Rahul},
  booktitle = {NeurIPS 2023 Workshop on MATH-AI},
  year = {2023},
  url = {https://mathai2023.github.io/papers/40.pdf}
}

License

MIT.