How I Built an Autonomous AI Agent That Earns USDC While I Sleep
Description and use
Built an autonomous AI agent that accepts USDC micro‑payments via x402, processes requests with FastAPI and Redis, runs AI logic, and scales horizontally.
Problem
Developers want to create a self‑serving AI agent that can accept micro‑payments for its work, offset hosting costs, and learn on‑chain payment economics.
Outcome
The agent processes requests in ~120 ms, charges callers directly in USDC, and can be scaled by adding worker instances.
Workflow
- 1. Deploy FastAPI front‑end that validates X‑Payment header and enqueues job in Redis.
- 2. Run async worker pool that pulls jobs from Redis, executes AI logic (e.g., sentiment analysis), and stores result in Redis.
- 3. Verify x402 payment header using web3.py against USDC contract on Base and ensure amount matches price.
- 4. Return result to caller via HTTP after job completion.
- 5. Scale workers horizontally by adding more instances sharing the same Redis queue.
Evidence
The x402 spec defines a payment header: X-Payment: <token_address>:<chain_id>:<amount>:<receiver>:<nonce>:<signature>
We verify the signature against the token contract (USDC on Base) and ensure the amount matches our price.
- Record date: 2026-09-14T12:46:21.741Z