How I Built an Autonomous AI Agent That Earns USDC While I Sleep
The promise of autonomous AI agents is often bottlenecked by a simple, practical problem: monetization. Subscription models require accounts, API keys, and complex billing integrations (like Stripe) that agents cannot easily navigate on their own.
To build a truly autonomous agent that operates in the wild, we need a machine-native payment rails system.
I built and deployed a suite of autonomous agents that sell utility services (text processing, code review, translation, and data synthesis) using HTTP 402 Payment Required protocols over Base (Coinbase’s Layer 2 network) using USDC.
Here is the exact architecture, the production code, and the hard engineering trade-offs of building self-monetizing AI services.
The System Architecture
The core concept relies on the standard but underutilized HTTP status code: 402 Payment Required.
┌────────┐ (1) Call Endpoint (No Tx) ┌────────┐
│ Client │─────────────────────────────────────────────────────>│ Agent │
│ │<─────────────────────────────────────────────────────│ Worker │
│ │ (2) HTTP 402: Pay X USDC to Y └────────┘
│ │
│ │ (3) Send USDC on Base L2
│ │─────────────────────────────────────────────────────> [ Base ]
│ │ [ Chain]
│ │ (4) Call Endpoint + Tx Hash ┌────────┐
│ │─────────────────────────────────────────────────────>│ Agent │
│ │ │ Worker │
│ │ └───┬────┘
│ │ │ (5) Verify Tx
│ │ │ & Dedup
│ │<─────────────────────────────────────────────────────────┘
└────────┘ (6) Return LLM Output
The Transaction Flow
-
The Handshake: The client requests a service (e.g., POST
/api/v1/review-code). -
The Demand: If no payment proof is attached, the server responds with a
402 Payment Requiredstatus, returning a JSON payload specifying the price (in USDC), the target wallet address, and the Base network details. - The Settlement: The client sends the exact micro-payment on Base.
-
The Execution: The client retries the API request, this time attaching the transaction hash in the
X-Payment-Txheader. - The Verification: The agent verifies the transaction on-chain, ensures it hasn't been reused, processes the request via LLM/computation, and delivers the payload.
Core Implementation
This implementation uses Cloudflare Workers (for global low-latency and edge execution) and Viem (a lightweight, type-safe library for interacting with the Base network).
1. Verifying the On-Chain Transaction
The worker must verify that the transaction hash provided by the client is valid, has succeeded, transferred the correct token (USDC), sent the correct amount, and went to the correct destination wallet.
typescript
import { createPublicClient, http } from 'viem';
import { base } from 'viem/chains';
const USDC_BASE_ADDRESS = '0x833589fCD6eDb6E08f4c7C32D4f71b54bda02913';
const AGENT_WALLET = '0xYourAgentWalletAddressHere...';
// Minimal ERC-20 Transfer Event signature
const TRANSFER_EVENT_TOPIC = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef';
const publicClient = createPublicClient({
chain: base,
transport: http('https://mainnet.base.org'), // Replace with a private RPC in production
});
async function verifyPayment(txHash: `0x${string}`, expectedUsdcAmount: number): Promise<boolean> {
try {
const receipt = await publicClient.getTransactionReceipt({ hash: txHash });
if (!receipt || receipt.status !== 'success') {
return false;
}
Top comments (0)