x402 payments for AI agents, with a budget

The 402 revival is the most interesting payment primitive for agents in years: a resource says its price, the client pays it, the resource is returned. The catch is that it is designed to be automatic, and automatic spending needs a ceiling.

x402 & machine payments7 min readBy the Patram team

HTTP status 402 Payment Required sat unused for thirty years because browsers had no wallet. Agents do, which is why x402 turns it into a working paywall for software: the server quotes a price, the client sends a stablecoin payment, the server returns the resource.

How the exchange works

  1. 01The client requests a resource without payment.
  2. 02The server answers 402 with machine-readable terms: amount, asset, network and recipient.
  3. 03The client settles the payment and retries the request with proof of payment.
  4. 04The server verifies settlement and returns the resource.
The shape of the exchange
GET /v1/dataset
402 Payment Required
{ "amount": "0.002", "asset": "USDC", "network": "solana", "payTo": "..." }

-- client settles, retries --

GET /v1/dataset
200 OK
{ "rows": [ ... ] }

Why agents adopt it

  • No signup, no card, no invoice cycle: a new data source is usable in the same tool call that discovers it.
  • Pricing is per request, so an agent pays for exactly what it reads.
  • The terms are in the response, so an agent can decide whether a source is worth the price.

The risk: automation without a ceiling

Every one of those properties is also a failure mode. An agent that can pay any 402 it meets can be led by a page it reads, can loop when a server keeps returning 402 after payment, and can spend a month of budget on retries in ninety seconds. Small payments are not safe payments; they are just harder to notice.

The retry loop problem

The classic bug: the client pays, the server fails to verify, the client sees 402 again, and pays again. Nothing in the protocol bounds that. Bound it in your client: a per-request ceiling, a per-run total, and a hard stop after one paid retry per URL.

Bounded payer
const MAX_PER_REQUEST = 5_000_000; // 0.005 SOL
const MAX_PER_RUN = 50_000_000;   // 0.05 SOL

let spentThisRun = 0;
const paidUrls = new Set<string>();

async function paidFetch(url: string, init?: RequestInit) {
  if (paidUrls.has(url)) throw new Error("Already paid for " + url);
  if (spentThisRun >= MAX_PER_RUN) throw new Error("Run budget exhausted");

  const response = await patramFetch(url, init); // settles 402 inside the guard
  paidUrls.add(url);
  spentThisRun += readAmountFromReceipt(response);
  return response;
}

Where the cap belongs

Put the ceiling in the vault, not only in the client. The client bound stops a buggy loop; the on-chain limit stops a modified client, a stolen key or a second process. Together they give you a bounded blast radius and a readable record of what happened.

Settling inside the vault
import Patram from "patram-sdk";

// Settles an x402 invoice through the agent's vault.
// Over-limit requests become approval items instead of payments.
const response = await Patram.patramFetch("https://api.example.com/v1/dataset", {
  headers: { accept: "application/json" },
});

Operational rules that save money

  • Cache aggressively. The cheapest 402 is the one your agent does not need to make.
  • Log every quote you accepted with its URL and amount. Price anomalies are the earliest signal of a bad endpoint.
  • Give research agents a much smaller budget than treasury agents. Discovery is inherently exploratory; payment should not be.
  • Never let a 402 response inject instructions. Treat the body as data, not as a new goal.