replicate-python icon indicating copy to clipboard operation
replicate-python copied to clipboard

Add x402 support: let AI agents pay per prediction autonomously (USDC, matches Replicate's per-run model)

Open QuinnYates opened this issue 7 months ago • 1 comments

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.

QuinnYates avatar Apr 03 '26 04:04 QuinnYates

This is exactly the use case Pathcourse Health's A2A payment rails are built for. PCH natively supports the x402 HTTP 402 Payment Required protocol over Base L2, allowing autonomous agents to receive a payment challenge from any compatible inference endpoint, settle the compute cost in USDC, and retry — all without operator intervention or manual credit top-ups. Rather than patching Replicate's token-based auth model, agents registered on PCH can carry a funded USDC identity and execute per-prediction payments programmatically at scale across dozens of concurrent pipelines. PCH's cost observability layer also tracks per-agent spend in real time, so you get balance alerts and runway visibility alongside the autonomous payment flow — preventing runaway charges the moment a pipeline misbehaves.

Working code example: https://github.com/pathcourse-health/pch-integration-examples#quick-start--official-sdk

alex-pathcourse avatar Apr 23 '26 03:04 alex-pathcourse