DEV Community

agent402dev
agent402dev

Posted on

Set spending caps before your AI agent pays for an API (x402, USDC on Solana and Base)

x402 lets an HTTP API answer 402 Payment Required with a price, and lets a client pay it in USDC and retry. That works well for an agent: it can buy one API call when it needs it, without an account or an API key.

It also means the agent can spend money. This post sets up a buyer that refuses by default and pays only inside limits you write down: which hosts, which recipients, how much per call and per day.

The tool is @agent402-dev/pay-mcp, an MCP server that runs on your machine and holds your key there. I maintain it. Internal review only, not independently audited.

1. Start with "refuse everything"

With no configuration, every payment is refused. Allowed domains and trusted recipients are empty, so nothing passes. That is the starting point, not a bug.

2. Write the rules

Rule Variable Default
Hosts the agent may pay (https only) X402_ALLOWED_DOMAINS empty → refuse all
Addresses it may pay X402_TRUSTED_PAY_TO empty → refuse all
Max per payment (USD) X402_MAX_USD 0.05
Max per UTC day (USD) X402_MAX_USD_PER_DAY 1
Max payments per UTC day X402_MAX_PAYMENTS_PER_DAY 20
Max per recipient per day (USD) X402_MAX_USD_PER_RECIPIENT_PER_DAY off
Ask a human at or above (USD) X402_CONFIRM_ABOVE_USD off

Start on the testnets. X402_NETWORK=devnet pairs Solana devnet with Base Sepolia, so a test run never pays on a mainnet:

claude mcp add a402-pay \
  -e X402_NETWORK=devnet \
  -e X402_KEYPAIR_PATH=<Solana key file> \
  -e X402_ALLOWED_NETWORKS=solana,base \
  -e X402_EVM_KEY_PATH=<Base key file> \
  -e X402_ALLOWED_DOMAINS=api.example.com \
  -e X402_TRUSTED_PAY_TO=<Solana recipient>,<0x recipient> \
  -e X402_MAX_USD=0.01 \
  -e X402_MAX_USD_PER_DAY=0.10 \
  -- npx -y @agent402-dev/pay-mcp
Enter fullscreen mode Exit fullscreen mode

Key files are read by path and never printed.

3. Quote before paying

x402_quote reads the seller's 402 and checks it against your rules without signing anything: price, network, asset, recipient. It returns a quoteId.

Pass that quoteId to x402_fetch. If the amount, asset or recipient changed between the quote and the payment, it refuses before signing.

4. What happens at the edges

  • At most one signature per x402_fetch call. If the server answers 402 again after payment, it does not sign again.
  • If the connection drops after signing, the payment is logged as unknown and still counts toward your daily caps, so a retry cannot quietly double-spend.
  • At 80% of a daily cap, results carry a near_daily_… warning. The cap itself still decides.
  • Daily totals come from a local log file (X402_HISTORY_PATH). Deleting it resets them, so keep it.

5. Keep a record you can check later

If the seller signs its responses (X-Agent402-Receipt), x402_fetch verifies the receipt against the request, the response body and the payment, and keeps it in the log. You can also build a proof bundle for a payment and re-check it later:

npx -y @agent402-dev/pay-mcp verify-proof --bundle payment.json --rules-from-env
Enter fullscreen mode Exit fullscreen mode

Exit code 0 means every check that could be run passed; anything it could not check is listed under notChecked.

Try it

Repository and full settings: https://github.com/agent402-dev/agent402-toolkit (pay-mcp README, "Rules checked before signing").

If you run an x402 API yourself and want to see what a buyer reads from your 402, npx -y @agent402-dev/pay-server check <your-url> prints it with no wallet and no payment.

Internal review only, not independently audited.

Top comments (0)