-
Notifications
You must be signed in to change notification settings - Fork 263
Add x402 support: let AI agents pay per prediction autonomously (USDC, matches Replicate's per-run model) #458
Description
Enable autonomous agent payments on Replicate via x402
When AI agents call Replicate to run model predictions autonomously, they use an operator's API token. The operator manually tops up credits. This doesn't scale once agents run 24/7 on dozens of pipelines.
x402 fixes this. It's HTTP 402 Payment Required, repurposed for machine-to-machine micropayments (Coinbase-backed, live on Base mainnet). An agent calls your endpoint, gets a 402 challenge, pays the compute cost in USDC, retries — all without human involvement.
What the integration looks like
On Replicate's end: return 402 when a request arrives without a valid X-Payment header.
HTTP/1.1 402 Payment Required
Content-Type: application/json
{
"version": "x402/1.0",
"accepts": [{
"scheme": "exact",
"network": "base-mainnet",
"maxAmountRequired": "50000",
"resource": "https://api.replicate.com/v1/predictions",
"description": "Replicate prediction — per run",
"payTo": "0xYOUR_WALLET",
"asset": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"
}]
}The agent's payment handler sees the challenge, signs USDC on Base, retries with X-Payment: <proof>. Replicate verifies the proof (decode + on-chain check) and runs the prediction.
Why x402 fits Replicate particularly well
Replicate is already per-run priced — you charge by the second of GPU compute. x402 maps directly to that model. Every prediction = one payment. No subscription overhead, no credit pool sharing, no operator managing top-ups per-agent.
The payment amount in maxAmountRequired can be set dynamically per model/hardware tier.
Implementation
Full guide: https://github.com/tomopay/gateway/blob/main/docs/guides/x402-rest-api-integration.md
The verification step is ~10 lines of TypeScript or Python. No new infrastructure — just decode the proof, verify the on-chain transaction, and run the prediction normally.
Agents using @tomopay/client (npm) handle the payment side automatically.
Happy to help with the verification implementation or discuss how to handle variable pricing per model. This is a no-MCP, no-protocol-change addition — just a 402 response on existing prediction endpoints.