DEV Community

Yaqing2023
Yaqing2023

Posted on

AI Agent USDC Payments: How to Build Agents That Pay Each Other

AI agent USDC payments enable autonomous agents to pay for services without human intervention. This tutorial shows you how to build AI agents that can send and receive USDC payments.

Why AI Agents Need USDC

AI agents are increasingly autonomous:

  • They browse the web
  • They call APIs
  • They make decisions

But when they need to pay for something, they hit a wall. Traditional payment systems require human identity.

USDC solves this:

  • No KYC required for wallets
  • Instant settlement
  • Global by default
  • Stable value (1 USDC = 1 USD)

Agent-to-Agent Payment Flow

┌─────────────┐     Request Service      ┌─────────────┐
│   Agent A   │ ───────────────────────► │   Agent B   │
│  (Buyer)    │                          │  (Seller)   │
└─────────────┘                          └─────────────┘
       │                                        │
       │         402 Payment Required           │
       │ ◄───────────────────────────────────── │
       │         (price: 0.99 USDC)             │
       │                                        │
       │         Signs USDC Transfer            │
       │ ───────────────────────────────────► │
       │         + Payment Proof                │
       │                                        │
       │         200 OK + Result                │
       │ ◄───────────────────────────────────── │
       │                                        │
Enter fullscreen mode Exit fullscreen mode

Setting Up an AI Agent Wallet

Step 1: Install MoltsPay

npm install moltspay
# or
pip install moltspay
Enter fullscreen mode Exit fullscreen mode

Step 2: Initialize Wallet

npx moltspay init --chain base
Enter fullscreen mode Exit fullscreen mode

This creates a wallet at ~/.moltspay/wallet.json.

Step 3: Fund the Wallet

For testing:

npx moltspay faucet  # Get free testnet USDC
Enter fullscreen mode Exit fullscreen mode

For production:

npx moltspay fund  # Opens onramp or shows deposit address
Enter fullscreen mode Exit fullscreen mode

Step 4: Set Spending Limits

npx moltspay config --max-per-tx 10 --max-per-day 100
Enter fullscreen mode Exit fullscreen mode

This limits autonomous spending to $10/tx and $100/day.

Building a Paying Agent (Buyer)

from moltspay import MoltsPay

# Initialize with wallet
client = MoltsPay(chain="base")

# Pay for a service
result = client.pay(
    provider_url="https://zen7.ai",
    service_id="text-to-video",
    prompt="a dragon flying over mountains"
)

print(f"Video URL: {result[url]}")
print(f"Paid: {result[amount]} USDC")
Enter fullscreen mode Exit fullscreen mode

Building a Receiving Agent (Seller)

Create moltspay.services.json:

{
  "provider": {
    "name": "My AI Service",
    "wallet": "0xYourWalletAddress"
  },
  "services": [
    {
      "id": "text-to-video",
      "function": "generateVideo",
      "price": 0.99,
      "currency": "USDC",
      "description": "Generate video from text prompt"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Create index.js:

export async function generateVideo({ prompt }) {
  // Your video generation logic
  const videoUrl = await myVideoModel.generate(prompt);
  return { url: videoUrl };
}
Enter fullscreen mode Exit fullscreen mode

Start the server:

npx moltspay start ./my-service --port 8402
Enter fullscreen mode Exit fullscreen mode

Your agent now accepts USDC payments at http://localhost:8402.

Integrating with AI Frameworks

CrewAI

from crewai_tools import MoltsPayTool

tool = MoltsPayTool()
agent = Agent(
    role="Buyer",
    tools=[tool]
)
Enter fullscreen mode Exit fullscreen mode

LangChain

from langchain_moltspay import MoltsPayTool

tools = [MoltsPayTool()]
agent = create_react_agent(llm, tools)
Enter fullscreen mode Exit fullscreen mode

AutoGPT

Add to your AutoGPT plugins:

pip install autogpt-moltspay-plugin
Enter fullscreen mode Exit fullscreen mode

Security Best Practices

  1. Set spending limits — Always configure max-per-tx and max-per-day
  2. Use testnet first — Verify logic before mainnet
  3. Monitor transactions — Log all payments for auditing
  4. Separate wallets — Use different wallets for different agents

Multi-Chain Support

AI agents can pay on multiple chains:

# Base (recommended - lowest fees)
client = MoltsPay(chain="base")

# Polygon
client = MoltsPay(chain="polygon")

# Testnet
client = MoltsPay(chain="base_sepolia")
Enter fullscreen mode Exit fullscreen mode

Example: Two Agents Trading

# Agent A: Needs image analysis
analysis = buyer_agent.pay(
    "https://vision-ai.example.com",
    "analyze-image",
    image_url="https://..."
)

# Agent B: Receives payment, returns analysis
# (Handled automatically by MoltsPay server)
Enter fullscreen mode Exit fullscreen mode

Resources


AI agent USDC payments unlock autonomous commerce. With MoltsPay, your agents can pay and receive payments in minutes.

Top comments (0)