DeFiPy is a unified Python SDK for agentic DeFi, covering analytics, simulation, and LLM-native tool schemas for autonomous agents. Built with modularity in mind, it exposes DeFi primitives as MCP tools and lets you isolate and extend your analytics by protocol using:
New here? The Quick Start walks through the essential Core Primitives — pool setup, Join, Swap — protocol by protocol. For onchain event access, pair with Web3Scout.
Looking for the older docs? They’re still live at defipy.readthedocs.io while the migration to defipy.org continues.
🤖 Building an agent?Wire DeFiPy primitives into Claude Desktop, Claude Code, or any MCP-compatible client. 10 curated tools ship with schemas; a reference MCP server closes the end-to-end loop.
📊 Running analytics?21 stateless, typed primitives across 9 categories — position analysis, risk, pool health, price scenarios, portfolio aggregation. Every primitive returns a structured dataclass.
📐 Learning the math?Hand-derived AMM math across all four protocols — sqrt-price and ticks for Uniswap V3, weighted pools for Balancer, amplification for Stableswap. Runnable as Jupyter notebooks.
# Ask the primitive: why is this LP position gaining or losing money?
result =AnalyzePosition().apply(
lp,
lp_init_amt=1.0,
entry_x_amt=1000,
entry_y_amt=100000,
)
print(f"Diagnosis: {result.diagnosis}")
print(f"Net PnL: {result.net_pnl:.4f}")
print(f"IL %: {result.il_percentage:.4f}")
print(f"Current val: {result.current_value:.4f}")
MockProvider ships canonical synthetic pools; StateTwinBuilder turns a snapshot into a usable exchange object; any primitive runs against it. The same pattern works against live chain state once LiveProvider ships in v2.1 — the primitives don’t care where the pool state came from.
Most Python DeFi libraries wrap chain queries from The Graph. DeFiPy ships the math — hand-derived invariants for four AMM families, exposed as composable typed primitives that an LLM can call directly via Model Context Protocol.
Exact math, not approximations
No textbook IL formulas. Closed-form expressions derived from each protocol’s invariant — xy = k for V2, L = √(xy) with concentrated ticks for V3, weighted geometric mean for Balancer, the ε ↔ δ relation for Stableswap.
Substrate, not agent
DeFiPy is the foundation. It has zero LLM dependencies and zero web3 dependencies in the core install. Build whatever agent you want on top — DeFiMind, LangChain, custom MCP server, your own framework.
Composable typed primitives
Every primitive follows the same contract: stateless construction, computation at .apply(), typed dataclass return. Same call shape from a Jupyter notebook or from Claude calling it as a tool.
Plan, don't execute
No signing keys. Ever. Primitives like OptimalDepositSplit and EvaluateRebalance return structured plans the caller can inspect, serialize, approve, or hand to whatever execution runtime they trust.
📘 DeFiPy: Python SDK for On-Chain AnalyticsTextbook covering AMM math, risk modeling, and agent-based simulation. Chapter 9 builds autonomous DeFi agents on the legacy event-driven runtime; chapter content compatible with v2 primitives lands in the second edition.
🎓 On-Chain Analytics FoundationsPractical course on transforming raw blockchain data into Python analytics pipelines. Companion to the book.