Skip to content

Review and improve error responses for agent self-correction #85

Open

Description

@whoabuddy

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:

  • retryable boolean — can the agent retry the same request?
  • retryAfter seconds — how long to wait before retry
  • nextSteps string — plain-English guidance for the agent
  • code string — 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_nonce on /storage/paste and /storage/kv (concurrent settlements)
  • 13:02 — 1x unexpected_settle_error on /hashing/sha512-256
  • Mar 23 17:01 — 1x unexpected_settle_error on /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

  1. added a commit that references this issue on Mar 27, 2026
    bcfd113
  2. anansutiawan commented on Apr 17, 2026

    @anansutiawan

    PR #106 addresses one of the gaps listed here — the conflicting_nonce case. 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 the conflicting_nonce scenario is handled transparently without agent involvement.

    What's still open from this issue:

    Happy to file a follow-up PR for structured error codes if the format gets agreed on in this issue.

  3. secret-mars commented on Nov 19, 2026

    @secret-mars
    Contributor

    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:

    • Settle failures on both paths now return retryable, nextSteps and, where set, retryAfter in the body, sends Retry-After, and puts the same hints in the payment-response header. On the exception path this comes from hintsFromClassifiedCode (src/middleware/x402.ts:231, body at :548-556). On the canonical path the hints come from the relay's status + terminalReason.
    • UNEXPECTED_SETTLE_ERROR is no longer a dead end. Network and timeout errors map to 502 with retryAfter: 5, relay-unavailable to 503 with retryAfter: 30, and the final fallback is still a 500 but with retryable: 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:

    1. "Payment expired, please sign a new payment" for anything containing 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, but INVALID_TRANSACTION_STATE maps to rebuild_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 in X402_ERROR_CODES). Matching on those before the substring check would separate "your nonce" from "the sponsor's nonce".
    2. 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.
    3. The final fallback message is still the generic "Payment processing error". That's fine now that it's marked retryable, but a bounded count would help: after N consecutive UNEXPECTED_SETTLE_ERROR for the same tx, switch to retryable: false with a "report it" next step, so a permanent failure doesn't look transient forever.

    x402-api#150 (open) leaves these lines unchanged (:1146-1166 on its head 4c0f6df), so they're independent of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions