Pydantic AI integration kit for RugGuard — pre-trade safety as a typed tool. Pay-per-call via x402 on Base mainnet.
Project description
rugguard-pydantic-ai-agent
Pydantic AI integration kit for RugGuard. Add a typed pretrade_check tool to your AI trading agent in ~3 lines of code. Every call returns a Pydantic model with a prescriptive policy_recommendation of block | caution | allow, plus a clamped max_suggested_exposure_usd and a signed JSON report (Ed25519). Pay-per-call via x402 micropayments on Base mainnet.
Why
If your agent buys tokens, it should run a pre-trade check first. RugGuard wraps 14 heuristics on Base + 5 on Solana SPL into a single $0.01 USDC call. The response is a typed Pydantic model with everything an LLM needs to make a sane sizing decision.
Two surfaces in this kit:
pretrade_check_async(...)— a framework-agnostic async function. Call it directly from any runtime.register_rugguard_tool(agent, ...)— a one-liner that registers the check as a typed Pydantic AI tool on your existingAgent. The LLM sees the tool, calls it with structured args, and consumes the typed response natively.
Install
pip install rugguard-pydantic-ai-agent
30-second tour
from pydantic_ai import Agent
from rugguard_pydantic_ai_agent import register_rugguard_tool, DecisionCache
agent = Agent(
"openai:gpt-4o-mini",
system_prompt=(
"You are a careful crypto trading assistant. Always call "
"pretrade_check before recommending any buy. If it returns "
"'block', refuse. If 'caution', downsize to "
"max_suggested_exposure_usd. If 'allow', proceed."
),
)
# Three lines. Done. The LLM now sees a typed `pretrade_check` tool.
register_rugguard_tool(agent, policy="balanced", cache=DecisionCache())
result = agent.run_sync(
"Should I buy $250 of 0x4ed4E862860beD51a9570b96d89aF5E1B0Efefed on Base?"
)
print(result.output)
The LLM will call pretrade_check(chain="base", contract="0x4ed4E862...", intended_trade_usd=250.0) automatically before answering. The tool's response is a PreTradeCheckResult Pydantic model so the LLM can reason over typed fields, not JSON strings.
Run the demo (no LLM, no network)
pip install rugguard-pydantic-ai-agent
rugguard-pydantic-ai-demo --demo
Walks through 3 canned scenarios (safe USDC, fresh memecoin, drained pool) so you can see what the tool returns without paying or wiring up an LLM.
Run the live demo (real LLM + real $0.01 payment)
# 1. Get a funded x402 wallet (Base mainnet, ≥ $0.05 USDC)
# Generate one with: python -m rugguard_mcp init (from rugguard-mcp)
export RUGGUARD_X402_PRIVATE_KEY=0xYOUR_PRIVATE_KEY_HEX
export OPENAI_API_KEY=sk-YOUR_KEY
# 2. Run
rugguard-pydantic-ai-demo --live
The agent will reason about the prompt, call pretrade_check (which pays $0.01 USDC under the hood via x402), and respond with a sized recommendation.
What pretrade_check returns
class PreTradeCheckResult(BaseModel):
scan_id: str
chain: str
contract: str
policy_recommendation: Literal["block", "caution", "allow"]
policy: Literal["conservative", "balanced", "aggressive"]
risk_score: int # 0-100
verdict: Literal["safe", "low_risk", "medium_risk", "high_risk", "critical", "uncertain"]
confidence: Literal["high", "medium", "low", "insufficient_data"]
reason: list[PreTradeFlag] # top 3 flags, severity-ordered
max_suggested_exposure_usd: float # 100% if allow, 20% if caution, 0 if block
intended_trade_usd: float
scanned_at: str
disclaimer: str # MANDATORY — see below
signature: str | None # Ed25519 base64 (when configured)
key_fingerprint: str | None # routing identifier
Policy modes
| Policy | Blocks at | Cautions at | Allows below |
|---|---|---|---|
conservative |
score ≥ 51 (medium_risk) | score 26-50 | score ≤ 25 |
balanced (default) |
score ≥ 71 (high_risk) | score 51-70 | score ≤ 50 |
aggressive |
score ≥ 91 (critical) | score 71-90 | score ≤ 70 |
An uncertain verdict (sparse data) returns caution in all modes. Absence of evidence is not evidence of safety.
Signed reports
When the deployment has Ed25519 signing configured (production rugguard.redfleet.fr does as of 2026-05-17, fingerprint a0c71156d8747078), the response carries signature and key_fingerprint fields. Verify offline:
pip install rugguard-verify
# inside an agent that has a result:
echo "$result_json" | rugguard-verify --report -
The disclaimer field is inside the signed canonical bytes. Stripping or rewriting it breaks signature verification by design.
Safety
This kit is intentionally minimal (~300 LOC across all modules) so you can read it end-to-end before forking. It is not spend-capped. For production use:
- Install
rugguard-mcpand import itsx402_client.paid_postinstead — that ships session caps + 24h caps + asset whitelist + EIP-712 domain enforcement. - Add your own monitoring + retry policy + circuit breaker.
- Use a dedicated x402 wallet, funded only with the USDC you are willing to spend.
The asset whitelist IS enforced in this kit (USDC on Base / Base Sepolia only). A malicious 402 trying to drain a different EIP-3009 token in your wallet is rejected before signing.
How pretrade_check works under the hood
- Agent calls
pretrade_check(chain, contract, intended_trade_usd). - Kit checks the in-memory
DecisionCache. If hit, returns the cached result withcache_hit=True. No payment. - Otherwise: POST
https://rugguard.redfleet.fr/v1/pretrade/checkwith the body. Server returns402 Payment Requiredwith x402 spec body. - Kit signs an EIP-3009
TransferWithAuthorizationfor $0.01 USDC to RugGuard's receiving wallet. Retries the POST withX-Paymentheader. - Server settles via Coinbase CDP facilitator, returns
200with the typed response + signature. - Kit parses into
PreTradeCheckResult, caches it, returns to the agent.
The whole round trip is ~300-500ms on a cache miss, ~1ms on a cache hit.
Self-host / testnet
register_rugguard_tool(
agent,
api_url="https://my-rugguard.example.com", # or http://localhost:8000 for dev
)
The kit also reads RUGGUARD_API_URL from the environment if no api_url argument is passed.
License
MIT. See LICENSE.
See also
- RugGuard — the pre-trade safety API
rugguard-mcp— MCP server for Claude Desktop / Cursor / LangGraphrugguard-verify— stand-alone Ed25519 signed-report verifier- Pydantic AI — the agent framework this kit plugs into
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rugguard_pydantic_ai_agent-0.1.2.tar.gz.
File metadata
- Download URL: rugguard_pydantic_ai_agent-0.1.2.tar.gz
- Upload date:
- Size: 24.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
10dc6ab478177ae7a7819f3c3e9def05c421c22d071cf62a48709eb97ced2859
|
|
| MD5 |
b3d9d7cb6da83f69436d61aae3654ef5
|
|
| BLAKE2b-256 |
d2541809d910463a121b04e1e45ddb00d68dce9288d72f916756ee2e04147686
|
Provenance
The following attestation bundles were made for rugguard_pydantic_ai_agent-0.1.2.tar.gz:
Publisher:
publish.yml on dbe006/rugguard-pydantic-ai-agent
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rugguard_pydantic_ai_agent-0.1.2.tar.gz -
Subject digest:
10dc6ab478177ae7a7819f3c3e9def05c421c22d071cf62a48709eb97ced2859 - Sigstore transparency entry: 1563161115
- Sigstore integration time:
-
Permalink:
dbe006/rugguard-pydantic-ai-agent@b3bf92e1e6122733662f2bdaced747706c897755 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/dbe006
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b3bf92e1e6122733662f2bdaced747706c897755 -
Trigger Event:
release
-
Statement type:
File details
Details for the file rugguard_pydantic_ai_agent-0.1.2-py3-none-any.whl.
File metadata
- Download URL: rugguard_pydantic_ai_agent-0.1.2-py3-none-any.whl
- Upload date:
- Size: 21.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb8bd0ebf9e2d6f9af67bc9e9b0cc3d098eb49120d270664a2c4c15fe0934929
|
|
| MD5 |
970ba33113440a350cb51dd034a61526
|
|
| BLAKE2b-256 |
c0bb98d16ee9a046c6e4e9ccc20f0c98af7c2ce637c8f4c058ad493e8c701c7d
|
Provenance
The following attestation bundles were made for rugguard_pydantic_ai_agent-0.1.2-py3-none-any.whl:
Publisher:
publish.yml on dbe006/rugguard-pydantic-ai-agent
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rugguard_pydantic_ai_agent-0.1.2-py3-none-any.whl -
Subject digest:
cb8bd0ebf9e2d6f9af67bc9e9b0cc3d098eb49120d270664a2c4c15fe0934929 - Sigstore transparency entry: 1563161132
- Sigstore integration time:
-
Permalink:
dbe006/rugguard-pydantic-ai-agent@b3bf92e1e6122733662f2bdaced747706c897755 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/dbe006
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b3bf92e1e6122733662f2bdaced747706c897755 -
Trigger Event:
release
-
Statement type: