DEV Community

Cover image for x402 for AI APIs: The Replay Test Before the Agent Pays Twice
AI x Crypto Systems
AI x Crypto Systems

Posted on

x402 for AI APIs: The Replay Test Before the Agent Pays Twice

x402 Replay Settlement Proof

Disclosure: AI tools were used for source collection and editorial review. The article was written by a human author, who checked the facts, code, and conclusions.

Crypto risk disclosure: This article is a technical explanation, not investment advice. It is not a recommendation to buy, sell, or hold any cryptoasset.

Start with a boring question: what happens when the paid retry gets sent twice? x402 makes a paid HTTP retry possible — a server answers with payment requirements, and the client retries with payment material. For AI APIs that matters, because the caller can be a tool-using agent instead of a human filling out a checkout form. So the proof an operator actually needs isn't a slogan about instant payments. It's a replay test: evidence that one agent task can't quietly turn into ambiguous spend. That is what x402 Replay Settlement Proof is for.

x402 replay proof cover

Replay Receipt

The x402 replay proof treats every paid retry as a state transition you log before you trust it. Official x402 documentation and Coinbase's x402 flow documentation describe the sequence: a request, a 402 Payment Required response, a payment payload, a retry, then verification and settlement. That is a transport flow, though, not a complete agent-control policy. The replay receipt fills the operational gap nobody wrote down: when the same task shows up again, should the API hand back the stored result, reject the duplicate, reconcile settlement, or ask for a new payment?

x402 replay state machine

The replay receipt can stay small:

{
  "request_id": "req_7f91",
  "resource_hash": "sha256:resource:9f02...",
  "payment_terms_hash": "sha256:terms:51ab...",
  "retry_fingerprint": "sha256:signed-retry:88c1...",
  "facilitator_verify": "pass",
  "settlement_state": "unknown_after_timeout",
  "response_validation": "not_started",
  "ledger_action": "hold_retry_and_reconcile"
}
Enter fullscreen mode Exit fullscreen mode

This x402 receipt never stores private prompts, wallet secrets, or raw signed payloads. What it keeps is hashes, state names, and the one decision that matters to the agent. That keeps the artifact handy during a support review without turning payment logs into a second pile of sensitive data.

Terms Hash

The whole x402 replay proof hangs on one binding: the signed retry has to match the terms the agent actually meant to buy. Coinbase's x402 overview covers payment instructions and a signed payment header, and Coinbase's flow documentation splits payment creation, retry, verification, and settlement into separate steps. A replay test should pull all of it into the terms record the server audits: the resource, amount, asset, network, recipient, expiry, and request identifier. Accept that signed material for a different resource, or after the window has gone stale, and the agent just handed over more authority than the task ever called for.

x402 terms binding card

The first negative test is simple: change one term, expect a rejection. The same retry fingerprint should not buy a different report, a larger amount, a different recipient, or a later request window. x402's payment-identifier extension helps here, because where it is in use the independent replay check can line up one payment identifier against the request fingerprint and flag a mismatched fingerprint as a conflict. The safe claim stays narrow: an AI API needs a visible terms boundary before it lets a model-driven caller spend again.

Facilitator Result

Here the x402 replay proof draws a line between facilitator success and merchant judgment. Coinbase's facilitator documentation says a facilitator can verify payment payloads, settle payments onchain, and return verification or settlement results to the server, which takes a lot of the blockchain operational work off the seller. What the facilitator result still cannot do is decide whether the endpoint should fulfill a restricted resource, whether the response is useful, or whether a refund or support rule applies.

x402 facilitator boundary

So the receipt records the facilitator result as one field, not as the final story. A verify=pass result means the payment payload satisfied a verification path. A settle=confirmed result means the payment execution reached the expected settlement state. Neither field proves the AI API delivered the correct data, honored the user's task boundary, or avoided prompt leakage in the resource URL. That gap is exactly why the x402 replay proof earns its keep: the receipt keeps those boundaries visible.

Settlement Unknown

The x402 replay proof earns the most when the network never answers at all. Amazon Bedrock AgentCore's payments documentation walks through an agent payment flow that checks limits, signs, retries with X-PAYMENT, verifies and settles, then updates state, and if a step fails, the transaction is recorded as failed and the limit reservation is released. That managed-service behavior is AWS-specific, but the operational lesson generalizes: the timeout branch needs its own state. When the retry times out after the signed payload has already left the client, the agent must not blindly spend again.

x402 timeout reconciliation

The timeout branch should be boring and strict:

Case What changed Expected decision
duplicate retry nothing return stored result or reject duplicate
wrong resource resource hash reject binding mismatch
expired terms expiry window reject or ask for new terms
settlement timeout facilitator response missing hold and reconcile
response validation fail content check fails after payment record paid-but-undelivered

Read the table as a test harness, not as protocol law: that is how the x402 replay proof treats it. Different payment schemes and facilitators can implement replay handling differently. Either way, the system still needs a replay policy a reviewer can inspect before production traffic reaches a funded agent wallet.

Paid But Undelivered

The x402 replay proof also has to cover the awkward state where payment succeeds and the API response fails validation. The arXiv preprint "Five Attacks on x402 Agentic Payment Protocol" reports attack classes around authorization, binding, replay protection, and web-layer handling, with the caveat — worth repeating — that the work is a May 2026 preprint rather than a final standard verdict. For an AI API the useful takeaway is not panic. It is to test the paid-but-denied and paid-but-undelivered branches before a model can trigger repeated paid calls.

x402 paid but undelivered ledger

The ledger row can state the failure without overclaiming:

request_id=req_7f91
payment_terms_hash=sha256:terms:51ab...
settlement_state=confirmed
response_status=502
response_validation=fail
agent_retry_decision=blocked_pending_reconcile
support_state=paid_but_undelivered
Enter fullscreen mode Exit fullscreen mode

Throughout, the x402 replay proof keeps payment proof and response proof separate. A blockchain settlement can prove that a payment execution happened under a payment path. A response validator can say whether the paid API returned the expected shape, source, or status. The agent should not treat the first proof as the second proof.

Final Test

The x402 replay proof should pass before the AI API leaves sandbox traffic. Run one valid paid call, then throw the rest at it: the same signed retry, a wrong-resource retry, a stale retry, a facilitator-timeout simulation, and a paid-but-undelivered response. Each case should produce one compact receipt. If that receipt cannot explain why the agent paid, whether settlement happened, and why another retry was blocked or allowed, the integration is not ready for autonomous spend.

x402 final replay checklist

The developer rule stays intentionally narrow: prove the replay boundary before funding the agent. x402 can make HTTP-native machine payment practical, and facilitators can make settlement easier to operate. What x402 Replay Settlement Proof adds is the part a production AI system still needs: a readable record that one task, one terms hash, and one retry decision stayed under control.

Top comments (0)