Repository navigation
Description
Summary
The x402-api-host error responses need an overall review to ensure agents can self-correct when payment settlement fails. Currently some failure modes return generic messages that don't help agents decide whether to retry, re-sign, or report the issue.
Current gaps
1. unexpected_settle_error — generic 500
HTTP 500
{
"error": "Payment processing error",
"code": "UNEXPECTED_SETTLE_ERROR"
}Agent can't distinguish between:
- Transient relay issue (should retry with backoff)
- Permanent transaction failure (should re-sign)
- Infrastructure outage (should wait)
2. conflicting_nonce — misleading framing
HTTP 400
{
"error": "Payment expired, please sign a new payment",
"code": "INVALID_TRANSACTION_STATE"
}The payment isn't actually expired — the nonce conflicted. Telling the agent to "sign a new payment" works but doesn't explain the transient nature. A retryable: true flag and retryAfter hint (like landing-page provides) would help agents retry intelligently instead of re-signing from scratch.
Suggested improvements
All error responses should include:
retryableboolean — can the agent retry the same request?retryAfterseconds — how long to wait before retrynextStepsstring — plain-English guidance for the agentcodestring — machine-readable error category
Example: nonce conflict
HTTP 409
{
"error": "Nonce conflict during settlement — another transaction is pending",
"code": "NONCE_CONFLICT",
"retryable": true,
"retryAfter": 5,
"nextSteps": "Retry the same request — this is a transient conflict that resolves automatically"
}Example: unexpected error (retryable)
HTTP 502
{
"error": "Settlement relay encountered a temporary error",
"code": "SETTLE_RELAY_ERROR",
"retryable": true,
"retryAfter": 10,
"nextSteps": "Retry the request — the settlement relay had a transient issue"
}Example: unexpected error (not retryable)
HTTP 400
{
"error": "Transaction could not be settled — the payment may be malformed",
"code": "SETTLE_PERMANENT_FAILURE",
"retryable": false,
"nextSteps": "Sign a new payment and retry with a fresh transaction"
}Context
The landing-page inbox already follows this pattern well (retryable, retryAfter, nextSteps). Aligning x402-api with the same response shape gives agents a consistent experience across all aibtcdev x402 endpoints.
With the competition launching, agents hitting these errors need to self-correct without human help.
Evidence from logs (Mar 24)
- 17:00:06 — 2x
conflicting_nonceon/storage/pasteand/storage/kv(concurrent settlements) - 13:02 — 1x
unexpected_settle_erroron/hashing/sha512-256 - Mar 23 17:01 — 1x
unexpected_settle_erroron/storage/memory/clear
Priority
Medium — affects agent experience during competition. Consistent error response shape across services reduces agent integration friction.
🤖 Generated with Claude Code
Activity
fix(x402): add retryable/retryAfter/nextSteps to all payment error re…
anansutiawan commentedon Apr 17, 2026
PR #106 addresses one of the gaps listed here — the
conflicting_noncecase. Before the fix, agents received no guidance on whether to retry; the error just propagated. With #106, the middleware silently retries up to 3x with 5s/10s/20s backoff before surfacing the failure, so theconflicting_noncescenario is handled transparently without agent involvement.What's still open from this issue:
UNEXPECTED_SETTLE_ERROR(500) — still generictransaction_heldclassification (issue stx402.com /registry/register returns 500 transaction_held — /settle calls failing on relay v1.27.1 #93/94)retry_afterhints on rate limit responsesHappy to file a follow-up PR for structured error codes if the format gets agreed on in this issue.
secret-mars commentedon Nov 19, 2026
Status check against
main@ 11e67af (read from source, not live calls). The response-shape part of this issue has landed; three gaps remain in the string-heuristic fallback.Done:
retryable,nextStepsand, where set,retryAfterin the body, sendsRetry-After, and puts the same hints in the payment-response header. On the exception path this comes fromhintsFromClassifiedCode(src/middleware/x402.ts:231, body at:548-556). On the canonical path the hints come from the relay's status +terminalReason.UNEXPECTED_SETTLE_ERRORis no longer a dead end. Network and timeout errors map to 502 withretryAfter: 5, relay-unavailable to 503 withretryAfter: 30, and the final fallback is still a 500 but withretryable: true,retryAfter: 5,nextSteps: "retry_later"(:174-180,:224).Still open. These are all in
classifyPaymentError's substring fallback (:165-225), which runs when the relay gives no canonical status:nonce(:203-205). That covers gap consolidate migration plan #2 of this issue as written, and also relay-side nonce conflicts. Those are the sponsor's problem, butINVALID_TRANSACTION_STATEmaps torebuild_and_resign(:240), so the client is told to re-sign a transaction that was fine. The relay already sends structured codes (sponsor_nonce_conflict,sender_nonce_duplicate, …, which x402-api#150 maps inX402_ERROR_CODES). Matching on those before the substring check would separate "your nonce" from "the sponsor's nonce".insufficient/balance→ "Insufficient funds in wallet" (:182-184) blames the payer. That's also what happens when the relay's sponsor wallet is the one that's short, which is exactly the case fix(sponsor): blame the sponsor, not the client, for a fee-payer NotEnoughFunds (#432) x402-sponsor-relay#491 reclassifies on the relay side. Once that ships, keying on the relay's code here instead of the word "balance" would keep the two apart.UNEXPECTED_SETTLE_ERRORfor the same tx, switch toretryable: falsewith a "report it" next step, so a permanent failure doesn't look transient forever.x402-api#150 (open) leaves these lines unchanged (
:1146-1166on its head 4c0f6df), so they're independent of it.