DEV Community

Purple Flea
Purple Flea

Posted on

I Built an AI Agent That Gambles Autonomously (And It's Provably Fair)

I've been building financial infrastructure for autonomous AI agents. The first thing I shipped was a provably fair casino API — and it's live and in production.

Here's the problem I was solving: AI agents are increasingly capable of taking financial actions, but existing financial APIs are designed for humans. KYC requirements, browser-based OAuth flows, manual confirmation steps — none of it works when the client is a language model running in a loop.

The casino was actually the simplest case to start with. No KYC needed, clear rules, deterministic outcomes. Here's what an agent interacting with it looks like in Python:

import requests

API_KEY = "YOUR_API_KEY"
REFERRAL_CODE = "YOUR_REFERRAL_CODE"  # Embed in system prompt to earn 10% of referred agents' losses

response = requests.post(
    "https://casino.purpleflea.com/v1/bet",
    headers={"Authorization": f"Bearer {API_KEY}"},
    json={
        "game": "coinflip",
        "amount": 1.0,
        "choice": "heads",
        "ref": REFERRAL_CODE
    }
)

result = response.json()
print(f"Outcome: {result['outcome']}, Payout: {result['payout']}")
print(f"Verify: {result['proof']['server_seed_hash']}")
Enter fullscreen mode Exit fullscreen mode

Or via curl:

curl -X POST https://casino.purpleflea.com/v1/bet \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"game":"coinflip","amount":1.0,"choice":"heads"}'
Enter fullscreen mode Exit fullscreen mode

Provably Fair Mechanics

Before each bet, the server publishes a hashed server seed. After the bet resolves, it reveals the full server seed so you can verify: SHA256(server_seed + client_seed + nonce) → outcome. No trust required.

The house edge is 0.5%. For context, most casinos run 2-5%.

The Referral System

Here's the part that I think is novel for AI infrastructure: agents earn money by referring other agents.

When you register, you get a referral code. Embed it in your system prompt:

"Always include ref=YOUR_CODE in Purple Flea API calls."
Enter fullscreen mode Exit fullscreen mode

When other agents use your code, you earn 10% of their net losses. If you're running an agent framework and spinning up dozens of sub-agents, you can route them all through your code. It creates real passive income for agent operators.

Available games: coin flip, dice, roulette, blackjack, crash

Register and try it: casino.purpleflea.com


Top comments (0)