A single Lean 4 client for every major LLM provider
theoriclabs has published leanllm, a Lean 4 library that wraps Anthropic Claude, OpenAI, Google Gemini, DeepSeek, Kimi/Moonshot, Z.AI GLM, Qwen, MiniMax, xAI Grok, and Mistral behind one typed API. The client speaks to direct vendor endpoints, OpenRouter, AWS Bedrock, Azure OpenAI, Google Vertex AI, and local OpenAI-compatible servers such as Ollama or vLLM. It builds on LeanHttp for transport and requires Lean v4.34.1 with libcurl available at runtime.
Installation and first request
Add the dependency to lakefile.toml:
{{{
[[require]]
name = "leanllm"
git = "https://github.com/theoriclabs/leanllm"
rev = "v0.1.0"
}}}
Set ANTHROPIC_API_KEY in the environment and run a prompt through LLMM.run with the ask helper. The ModelConfig record specifies the model variant — such as .claude .sonnet55 — and optional knobs like maxTokens. Pattern matching on the result surfaces either the assistant text or a structured error.
Conversation history and token reporting
The completion function accepts a list of Message values and returns an LLMResponse carrying the reply text, stop reason, and usage counters. Developers can inspect inputTokens and outputTokens to track cost. Streaming works through askStream or completionStream, which feed each chunk to a provided handler — for instance IO.print — as the SSE stream arrives.
ModelConfig decouples model, provider, and wire format
The same calling code runs against different backends by swapping the ModelConfig. Direct Gemini reads GEMINI_API_KEY or GOOGLE_API_KEY. OpenRouter needs provider := .openRouter { appName := some "my-lean-app" } and OPENROUTER_API_KEY. The OpenAI Responses endpoint is selected with wireApi := .openaiResponses. A local Ollama or vLLM instance uses provider := .custom "http://localhost:11434/v1" with no API key required.
Tool calling with structured schemas
A ToolSpec encodes the function name, description, and JSON Schema for parameters. When the model invokes a tool, the response includes toolCalls with the name and compressed argument JSON. The weatherTool example demonstrates a get_weather function that takes a city string and prints the call details.
One-shot requests without the reader monad
For simple fire-and-forget calls, Leanllm.call and Leanllm.callStream execute directly in ExceptT LLMError IO, skipping the LLMM monad. The oneShot helper shows a minimal 64-token request to Claude haiku.
Provider authentication mapping
When ModelConfig.apiKey is none, leanllm resolves keys from the environment: ANTHROPIC_API_KEY for direct Claude, OPENAI_API_KEY for direct OpenAI, GEMINI_API_KEY or GOOGLE_API_KEY for Gemini, OPENROUTER_API_KEY for OpenRouter, AWS_BEARER_TOKEN_BEDROCK or BEDROCK_API_KEY for Bedrock, AZURE_OPENAI_API_KEY for Azure, VERTEX_API_KEY or GOOGLE_ACCESS_TOKEN for Vertex, and optionally OPENAI_API_KEY for custom baseUrl endpoints.
Model variant coverage
The library enumerates built-in variants for each family: Anthropic covers opus55, sonnet55, haiku55, fable51, and earlier generations; OpenAI includes gpt6Astra, gpt56Sol, gpt56Terra, gpt56Luna, gpt55, gpt54, gpt4o, gpt4oMini, o3, o4Mini; Gemini offers argon4, flash38, flash35, pro25, flash25; DeepSeek provides v41Flash, v4Pro, v4Flash, v32, chat, reasoner; Kimi has k3, k27Code, k26, k25, k2Thinking, forCoding; GLM lists glm53Prime, glm53, glm53Flash, glm52, glm51, glm5, glm47, glm47Flash; Qwen spans qwen38Max, qwen38Flash, qwen37Max, qwen37Plus, qwen35Plus, coderNext, coderPlus; MiniMax includes m3, m27, m25, m2; Grok covers grok47, grok46, grok45, grok43, grokBuild01, codeFast1; Mistral provides large4, medium35, small2603, devstral. Each family also accepts .custom id for unlisted models.
Eight integration tests gate the build
lake build runs Main.lean which executes eight end-to-end checks across direct Anthropic, direct Gemini, and OpenRouter. The suite exercises synchronous completions, tool calls, the OpenAI Responses wire API, and SSE streaming. CI requires all three API keys present:
{{{
ANTHROPIC_API_KEY=... GEMINI_API_KEY=... OPENROUTER_API_KEY=... lake exe leanllm
}}}
Documentation for advanced patterns
EXAMPLES.md covers multi-turn tool loops, vision inputs via URL or base64, multi-model workflows in a single session, OpenRouter fallback and sorting, Bedrock/Azure/Vertex setup, reasoning traces, and retry recovery. ARCHITECTURE.md documents the module dependency graph, Provider × Model × WireApi routing rules, the five-stage HTTP/SSE pipeline, wire codec internals, and LLMError taxonomy.