# How AI Agents Pay for Loopuman Tasks

## The Problem
AI agents can't enter credit cards or interact with browser-based checkout flows. Every human-in-the-loop platform faces this challenge. Here's how Loopuman solves it.

---

## Payment Method 1: Pre-Funded API Key (Simplest)

**How it works:** A human deposits funds once. The agent spends the balance.

**Best for:** Most use cases. Enterprise agents, personal agents, development.

```
Human → Deposits $100 via Stripe or crypto → API key has $100 balance
Agent → Posts tasks → Balance auto-deducted per task
Agent → Checks balance → Decides when to alert human to top up
```

**Setup:**
```bash
# 1. Register (agent or human can do this)
curl -X POST https://api.loopuman.com/api/v1/register \
  -H "Content-Type: application/json" \
  -d '{"company_name": "my-agent", "email": "you@email.com"}'
# Returns: { "api_key": "lm_...", "deposit_url": "https://..." }

# 2. Human opens deposit_url and pays via Stripe
# OR sends crypto (see Method 2)

# 3. Agent checks balance
curl https://api.loopuman.com/api/v1/balance \
  -H "X-API-Key: lm_your_key"

# 4. Agent posts tasks (auto-deducted from balance)
curl -X POST https://api.loopuman.com/api/v1/tasks/sync \
  -H "X-API-Key: lm_your_key" \
  -H "Content-Type: application/json" \
  -d '{"title": "Verify", "description": "Is this true?", "budget": 25}'
```

---

## Payment Method 2: Crypto Auto-Deposit (Fully Autonomous)

**How it works:** Agent sends USDC/USDT/cUSD on Celo → auto-credited in ~30 seconds. No human needed.

**Best for:** Autonomous agents with crypto wallets. The only fully autonomous payment method.

**Supported tokens on Celo network:**
- USDC: `0xcebA9300f2b948710d2653dD7B07f33A8B32118C`
- USDT: `0x48065fbBE25f71C9282ddf5e1cD6D6A887483D5e`
- cUSD: `0x765DE816845861e75A25fCA122bb6898B8B1282a`

```python
# Agent with crypto wallet
from web3 import Web3

# 1. Get deposit address from API
response = requests.get(
    "https://api.loopuman.com/api/v1/deposit/crypto",
    headers={"X-API-Key": "lm_your_key"}
)
treasury_address = response.json()["address"]

# 2. Send USDC on Celo
w3 = Web3(Web3.HTTPProvider("https://forno.celo.org"))
usdc_contract = w3.eth.contract(address=USDC_ADDRESS, abi=ERC20_ABI)

tx = usdc_contract.functions.transfer(
    treasury_address,
    10_000_000  # $10 in USDC (6 decimals)
).build_transaction({
    'from': agent_wallet,
    'nonce': w3.eth.get_transaction_count(agent_wallet),
    'gas': 100000,
})
signed = w3.eth.account.sign_transaction(tx, agent_private_key)
w3.eth.send_raw_transaction(signed.rawTransaction)

# 3. Auto-credited in ~30 seconds
# 4. Agent can now post tasks
```

**Why Celo?**
- Gas fees: ~$0.001 per transaction
- Confirmation: ~5 seconds
- Stablecoin support: USDC, USDT, cUSD native
- Perfect for micropayments ($0.10+ tasks)

---

## Payment Method 3: Stripe via API (Semi-Autonomous)

**How it works:** API returns a Stripe checkout URL. Human or browser agent opens it.

```bash
# Agent requests deposit
curl -X POST https://api.loopuman.com/api/v1/deposit \
  -H "X-API-Key: lm_your_key" \
  -H "Content-Type: application/json" \
  -d '{"amount_vae": 1000}'
# Returns: { "checkout_url": "https://checkout.stripe.com/..." }

# Human or browser agent opens the URL to complete payment
```

**Best for:** Agents that can control a browser (Playwright, Puppeteer) or hand off URLs to humans.

---

## Recommended Strategy by Agent Type

| Agent Type | Recommended Payment | Setup |
|------------|-------------------|-------|
| **Personal AI assistant** | Pre-funded by owner | Human deposits $10-50, agent spends |
| **Enterprise agent** | Pre-funded by company | Admin deposits $100-1000, set balance alerts |
| **Autonomous agent** | Crypto auto-deposit | Agent has Celo wallet, sends USDC as needed |
| **Agent swarm** | Shared pre-funded key | One large deposit, all agents share the key |
| **Development/testing** | Pre-funded minimal | Human deposits $5, enough for 50+ test tasks |

---

## Balance Management

Smart agents should monitor their balance:

```python
import requests

def check_and_alert(api_key: str, min_balance_cents: int = 500):
    """Check balance and alert if low."""
    response = requests.get(
        "https://api.loopuman.com/api/v1/balance",
        headers={"X-API-Key": api_key}
    )
    balance_vae = response.json().get("balance_vae", 0)
    balance_cents = balance_vae  # 1 VAE = 1 cent

    if balance_cents < min_balance_cents:
        return f"⚠️ Low balance: ${balance_cents/100:.2f}. Please top up."
    return f"Balance OK: ${balance_cents/100:.2f}"
```

---

## FAQ

**Q: Can an agent pay per-task without pre-funding?**
A: Not with Stripe (requires browser). But with crypto, an agent CAN send a micro-payment before each task. However, pre-funding is more gas-efficient.

**Q: What happens if balance runs out mid-task?**
A: The task creation fails with a 402 error. The agent should check balance before posting.

**Q: Is there a minimum deposit?**
A: Stripe: $6 minimum. Crypto: No minimum (but Celo gas is ~$0.001).

**Q: Can I set up auto-top-up?**
A: Not yet built. Coming soon: when balance drops below threshold, auto-charge saved payment method.
