Skip to content

DeFiPy: Python SDK for Agentic DeFi

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.

🔗 DAPX-Anchor: anchorregistry.ai/AR-2026-YdPXB5g

Terminal window
pip install defipy

Core install is pure math — no chain reads, no LLM dependencies. For MCP server, chain reads, or Foundry workflows, see Installation.

from defipy import AnalyzePosition
from defipy.twin import MockProvider, StateTwinBuilder
# Build a synthetic ETH/DAI Uniswap V2 pool
provider = MockProvider()
builder = StateTwinBuilder()
lp = builder.build(provider.snapshot("eth_dai_v2"))
# 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.

Terminal window
pip install defipy

Pure analytics — AMM math, primitives, State Twin, all 21 typed analytics functions. Zero web3 dependencies, zero LLM dependencies.

DeFiPy unifies four protocol-specific Python libraries under one consistent surface: