This walkthrough follows examples/binance_trading_agent.py in the Python SDK repo — an 850-line example of a real autonomous agent governed by Regent end-to-end.
This is what you’ll see in the demo video. Every action visible there is reproduced by code below.
What the agent does
- Verifies its identity on startup — calls
r.identity.get_agent(), confirms KMS-signed payload + Solana anchor - Loads its mandate —
$50per-tx,$300daily,$5,000monthly - Connects to Binance testnet and reads BTC/USDT price + balances
- Trades on price signal — for every trade:
- Authorize via
r.payment.authorize(...)(mandate decision) - On approval, fill the order on Binance
- Log the action to the audit trail with the authorization JWT (
jti) - On rejection (over limit, agent revoked, etc.) log a
trade.rejectedevent with the reason
- Authorize via
- Logs everything to Regent’s audit trail — every approve, reject, fill, error
Architecture
Setup
pip install regent rich httpxEnv vars:
export REGENT_API_KEY="rgnt_..."
export REGENT_ORG_ID="..."
export REGENT_AGENT_ID="agent_..." # registered ahead of time on the dashboard
export REGENT_MANDATE_ID="..." # 50/300/5000 USD
export BINANCE_API_KEY="..." # https://testnet.binance.vision
export BINANCE_API_SECRET="..."The authorize → fill → audit cycle
The heart of the agent is _execute_trade:
async def _execute_trade(self, side: str, amount: float) -> None:
# 1. Ask the mandate
auth = await self.regent.payment.authorize(
self.mandate_id,
AuthorizeRequest(amount=Decimal(str(amount)), currency="USD"),
)
# 2. Fill the order on Binance
order = await self.binance.place_order(SYMBOL, side, amount)
order_id = order["orderId"]
filled_qty = float(order["executedQty"])
filled_quote = float(order["cummulativeQuoteQty"])
# 3. Log to Regent audit
await self.regent.audit.ingest_event(IngestEventRequest(
event_id=f"trade-{order_id}-{uuid.uuid4().hex[:8]}",
agent_id=self.agent_id,
event_type="trade.executed",
payload={
"exchange": "binance-testnet",
"symbol": SYMBOL,
"side": side,
"price": str(self._last_price),
"amount_usd": str(filled_quote),
"btc_qty": str(filled_qty),
"order_id": str(order_id),
"authorization_jti": auth.jti,
},
))Every successful trade carries the authorization jti, the Binance order ID, the executed quantity, and the realized price. This is the complete proof bundle an auditor would need.
What happens when the mandate denies
Replace auth = await ...authorize(...) with a try/except:
try:
auth = await self.regent.payment.authorize(
self.mandate_id,
AuthorizeRequest(amount=Decimal("200"), currency="USD"), # over per-tx $50
)
except RegentAPIError as e:
# e.code will be "MANDATE_LIMIT_EXCEEDED"
await self.regent.audit.ingest_event(IngestEventRequest(
event_id=f"trade-rejected-{uuid.uuid4().hex[:12]}",
agent_id=self.agent_id,
event_type="trade.rejected",
payload={"side": "BUY", "amount": "200", "reason": e.code, "price": price},
))
return # don't touch Binance — the order never happensThe rejection itself is an audit event — the protocol records refusals as carefully as it records approvals. This is a regulatory feature: an auditor can see what the agent tried to do, not just what it succeeded at.
The kill switch
While the bot is running, an operator can revoke the agent from the dashboard. Within ~1 second:
api-identityflips the agent’s status torevokedand publishesagent.revokedto RabbitMQapi-paymentconsumer receives the event, invalidates the identity-status cache, and suspends all active mandates for this agent- The next time the bot calls
/authorize, it getsMANDATE_SUSPENDED(orAGENT_NOT_ACTIVE) instantly - The bot logs the rejection as a
trade.rejectedevent and stops trading
There is no “wait for the next polling interval” or “wait for the cache to expire.” The kill switch is immediate.
# In the bot's main loop
while running:
side, amount = await self.decide_trade() # your strategy
try:
await self._execute_trade(side, amount)
except RegentAPIError as e:
if e.code in ("AGENT_NOT_ACTIVE", "MANDATE_SUSPENDED"):
print(f"Halted: {e.code}")
breakReal numbers from a live demo
From a recent demo run (full output in the SDK examples):
Trades executed: 4
[1] BUY $49.20 @ $80,661.63
[2] SELL $49.20 @ $80,660.67
[3] BUY $49.20 @ $80,660.67
[4] SELL $49.21 @ $80,675.09
Rejections: 8
BUY $200.00 — LIMIT_EXCEEDED (per-tx breach)
BUY $500.00 — LIMIT_EXCEEDED (per-tx)
BUY $1000.00 — LIMIT_EXCEEDED (per-tx)
BUY $50.00 — MANDATE_SUSPENDED (agent revoked mid-run)
...
On-chain: revoked_onchain
Solana TX: https://explorer.solana.com/tx/...?cluster=devnetEvery line is observable on the dashboard, in the audit log, and on Solana Explorer.
Source
Full implementation: abay94/regent-sdk-python/examples/binance_trading_agent.py.
Run it yourself with DEMO_MODE=true for the scripted 11-trade screencast sequence, or no flag for autonomous price-signal trading.