Formally verified AI guardrails for autonomous financial agents.
Lean-Agent Protocol replaces probabilistic safety filters with mathematical proof. When an AI agent wants to take an action — say, placing a large trade — that action is intercepted and submitted to the Lean 4 theorem prover as a formal conjecture. The Lean kernel either proves the action satisfies every loaded compliance policy, or refutes it with a machine-verifiable counter-example. Only proved actions execute.
Formal verification means the system doesn't estimate whether a rule was followed — it proves it, with the same mathematical rigor used to verify cryptographic protocols and operating system kernels. In finance, where a single miscalculated trade can cost millions, replacing "we think this is safe" with "this is provably safe" is the difference between a guardrail and a real guarantee.
The demo runs three pre-compiled policies (capital threshold, price deviation, position limit) against a mock trading agent cycling through six scenarios. Three pass, three fail — and every decision is logged to an immutable audit trail with a plain-language explanation of exactly why a blocked trade was blocked.
This project is based on the following foundational research paper: Original Research Paper
| Service | URL |
|---|---|
| Frontend | https://axiom.devrashie.space |
| Backend API | https://api.devrashie.space |
- Docker Desktop ≥ 4.x
- Node.js ≥ 18
- (Optional) elan + Lean 4 for editing
.leanfiles locally
# 1. Clone the repo
git clone https://github.com/YOURUSER/lean-agent-protocol
cd lean-agent-protocol
# 2. Create your .env from the example
cp .env.example .env
# Fill in ANTHROPIC_API_KEY (for plain-language explanations)
# ARISTOTLE_API_KEY is optional (for NL → Lean 4 translation)
# 3. Start backend + lean-worker (Caddy is skipped in local mode)
docker compose up --build backend lean-worker
# 4. In a separate terminal, start the frontend dev server
cd frontend
npm install
npm run dev
# → http://localhost:5173The Vite dev server proxies /api and /ws to http://localhost:8000, so no CORS configuration is needed locally.
Open https://YOURAPP.vercel.app and follow these steps:
- Click the Policies tab in the left panel
- Click the "Capital Threshold" chip (CAP-001)
- The natural language rule populates the textarea
- Click "Formalize →" — watch the right column fill with Lean 4 code
- Click "Compile & Deploy"
- The status indicator cycles: compiling… → ✓ Deployed as CAP-001
- The Lean kernel has type-checked the policy and compiled it to
.oleanbinaries
- Click the Agent tab
- Click "▶ Run Agent"
- Watch six mock trading scenarios fire sequentially (1.5 s apart)
- Each card shows: tool call →
→ verifying with Lean kernel…→ ALLOWED or BLOCKED badge with latency
- The right panel fills in real time as each scenario completes
- Each entry shows timestamp, agent ID, tool call, verdict, and plain-language explanation
- Blue left border = allowed · Amber left border = blocked
- Click any BLOCKED entry in the audit log
- Click "▼ Show Lean trace" at the bottom of the card
- The raw Lean 4 kernel output appears — e.g.:
This is the formal proof that the trade violated the policy.
Tactic `decide` proved that the proposition PolicyEnv.cap001Compliant 50000 400000 = true is false
- Click "Export JSONL" in the audit log header
- A
.jsonlfile downloads — one JSON object per line, immutable record of every decision
Judge's browser
│
├── HTTPS ──► Vercel CDN ──► React + Vite frontend
│ │
│ └── REST + WSS ──► api.YOURDOMAIN.com
│
└── api.YOURDOMAIN.com (Hetzner VPS)
│
└── Caddy (TLS termination + CORS)
│
├── /api/** ──► FastAPI backend :8000
│ │
│ └── HTTP ──► Lean worker :9000 (internal)
│
└── /ws/audit ──► FastAPI WebSocket (asyncio.Queue fan-out)
| Layer | Technology | Notes |
|---|---|---|
| Frontend | React + Vite + TypeScript | IBM Plex fonts, highlight.js Lean 4 |
| Backend | FastAPI + uvicorn | Orchestrator, back-translator, audit log |
| Lean worker | Python http.server + elan + lake | .olean binaries baked into Docker image |
| Reverse proxy | Traefik (via Coolify) | Automatic TLS via Let's Encrypt |
| Verification | Lean 4 decide tactic |
Nat basis-point arithmetic, fully decidable |
| Audit log | Append-only JSONL on shared Docker volume | Streams over WebSocket |
Lean 4's kernel can decide propositions over Nat (natural numbers) at compile time — decide either produces a proof term or a refutation. Float comparisons are not kernel-decidable. All policy thresholds are expressed as integer basis points (1 bps = 0.01%), so tradeValue ≤ 10% becomes tradeValue * 10000 ≤ availableCapital * 1000, which the kernel resolves in microseconds.
The backend runs on a VPS managed by Coolify. Coolify's built-in Traefik proxy handles TLS termination and routing — no Caddy or manual reverse proxy configuration needed.
# 1. In Coolify, create a new Docker Compose application pointing at this repo.
# 2. Configure secrets in the Coolify environment variables panel:
# ANTHROPIC_API_KEY=...
# FRONTEND_ORIGIN=https://axiom.devrashie.space
# 3. Coolify will run docker compose up automatically on each deploy.
# Traefik picks up the labels on the backend service and routes
# api.devrashie.space → backend:8000 with automatic TLS.The docker-compose.yml backend service carries Traefik labels so Coolify's proxy routes api.devrashie.space to it automatically. The lean-worker has no external port binding and is only reachable within the Docker network.
# From your local machine:
VPS_HOST=YOUR_VPS_IP ./deploy.shAPI_BASE=https://api.devrashie.space ./smoke-test.sh- Import the repo into Vercel
- Set root directory to
frontend - Add environment variable:
VITE_API_URL=https://api.devrashie.space - Deploy — Vercel auto-detects Vite and runs
npm run build
The vercel.json SPA rewrite rule handles client-side routing.
| Variable | Required | Description |
|---|---|---|
ANTHROPIC_API_KEY |
Yes | Claude API key for plain-language explanations |
ARISTOTLE_API_KEY |
Optional | Aristotle API key for NL → Lean 4 translation |
FRONTEND_ORIGIN |
Yes | Full Vercel URL, e.g. https://YOURAPP.vercel.app |
| Variable | Value |
|---|---|
VITE_API_URL |
https://api.YOURDOMAIN.com |
lean-agent-protocol/
├── frontend/ React + Vite SPA (deploys to Vercel)
├── backend/ FastAPI orchestrator (Docker)
├── lean-worker/ Lean 4 kernel service (Docker)
│ └── PolicyEnv/ Lake project — policy axioms baked in at build time
├── docker-compose.yml backend + lean-worker + Caddy
├── Caddyfile Reverse proxy config (fill in domain before deploy)
├── deploy.sh One-command VPS deploy script
├── smoke-test.sh Post-deploy API validation
└── .env.example Template — copy to .env and fill in secrets