# Errors and error codes

> Every verification and settlement failure reason of the Algorand exact scheme, and the exception classes of ai-economy-sdk, x402-algorand and x402-avm-js.

Look up an error code or exception here to find out what caused it and how to fix it. For symptoms without a code, see [Troubleshooting](https://x402-avm.github.io/resources/troubleshooting).

## Facilitator error codes

When `ExactAvmFacilitatorScheme.verify()` rejects a payment, it returns `VerifyResponse(is_valid=False)` with `invalid_reason` set to one of these strings; `settle()` returns them in `error_reason`. The constants are defined in `x402_algorand.constants`. The order in which checks run is on [AVM exact scheme](https://x402-avm.github.io/reference/protocol/avm-exact-scheme#verification-steps).

| Code | Constant | Meaning and typical cause | Fix |
|---|---|---|---|
| `unsupported_scheme` | `ERR_UNSUPPORTED_SCHEME` | The accepted or required scheme is not `exact`. | Use scheme `"exact"`. |
| `network_mismatch` | `ERR_NETWORK_MISMATCH` | The network in the client's `accepted` requirement differs from the server's requirement. | Pay the offer as given; do not rewrite its network. |
| `invalid_exact_avm_payload_empty_group` | `ERR_EMPTY_GROUP` | `paymentGroup` is missing or empty. | Send the whole group in `payload.paymentGroup`. |
| `invalid_exact_avm_payload_group_too_large` | `ERR_GROUP_TOO_LARGE` | More than 16 transactions. | Algorand groups hold at most 16 transactions. |
| `invalid_exact_avm_payload_payment_index` | `ERR_INVALID_PAYMENT_INDEX` | `paymentIndex` is outside the group. | Point `paymentIndex` at the buyer's payment (1 with a fee payer, 0 without). |
| `invalid_exact_avm_payload_group_decode_failed` | `ERR_GROUP_DECODE_FAILED` | A transaction is not valid base64 msgpack. | Encode each transaction with msgpack, then base64. |
| `invalid_exact_avm_payload_missing_group_id` | `ERR_MISSING_GROUP_ID` | A multi-transaction group has a transaction without a group id. | Assign the group id to every transaction before signing. |
| `invalid_exact_avm_payload_group_id_mismatch` | `ERR_INVALID_GROUP_ID` | Transactions carry different group ids. | Compute the group id over the final transactions, then sign. |
| `invalid_exact_avm_payload_genesis_hash_mismatch` | `ERR_GENESIS_HASH_MISMATCH` | A transaction was built for another network than the requirement's. | Use an algod on the requirement's network. |
| `invalid_exact_avm_payload_close_to_detected` | `ERR_CLOSE_TO_DETECTED` | A transaction sets `close_remainder_to` or `asset_close_to`. | Remove close-out fields. |
| `invalid_exact_avm_payload_blocked_transaction_type` | `ERR_BLOCKED_TXN_TYPE` | A transaction is a key registration (`keyreg`). | Remove it from the group. |
| `invalid_exact_avm_payload_rekey_detected` | `ERR_REKEY_DETECTED` | A rekey that is not undone within the group. | Remove `rekey_to`. |
| `invalid_exact_avm_payload_asset_id_mismatch` | `ERR_INVALID_ASSET_ID` | Wrong transaction type for the asset (`pay` for ALGO, `axfer` for an ASA) or wrong ASA. | Pay the asset in `requirements.asset`. |
| `invalid_exact_avm_payload_recipient_mismatch` | `ERR_RECIPIENT_MISMATCH` | The receiver is not `payTo`. | Pay the `payTo` from the challenge. |
| `invalid_exact_avm_payload_amount_insufficient` | `ERR_AMOUNT_INSUFFICIENT` | The amount differs from the required amount (it must be equal). `invalid_message` shows both. | Pay exactly `requirements.amount`. |
| `invalid_exact_avm_payload_missing_signature` | `ERR_MISSING_SIGNATURE` | The buyer's payment is unsigned. | Sign the transaction at `paymentIndex`. |
| `invalid_exact_avm_payload_invalid_signature` | `ERR_INVALID_SIGNATURE` | The Ed25519 signature does not verify, for example a wrong key or a transaction changed after signing. | Sign with the sender's key after the group id is set. |
| `invalid_exact_avm_payload_fee_payer_transferring_funds` | `ERR_FEE_PAYER_TRANSFERRING` | The buyer's payment is sent from one of the facilitator's own addresses. | Use a buyer account different from the fee payer. |
| `fee_payer_not_managed_by_facilitator` | `ERR_FEE_PAYER_NOT_MANAGED` | `extra.feePayer` is not an address this facilitator signs for. | Use the fee payer the server advertised. |
| `invalid_exact_avm_payload_missing_fee_payer` | `ERR_FEE_PAYER_MISSING` | The requirement names a fee payer but no transaction in the group is from it. | Include the fee payer's transaction (index 0). |
| `invalid_exact_avm_payload_fee_payer_transaction_invalid` | `ERR_FEE_PAYER_INVALID_TXN` | The fee payer transaction is not a `pay` from the fee payer to itself, or its fee is above 16,000 microALGO. | Build it as a 0-amount self-payment with the pooled fee. |
| `invalid_exact_avm_payload_fee_payer_has_amount` | `ERR_FEE_PAYER_HAS_AMOUNT` | The fee payer transaction moves ALGO. | Set its amount to 0. |
| `invalid_exact_avm_payload_fee_payer_has_close_to` | `ERR_FEE_PAYER_HAS_CLOSE` | The fee payer transaction sets `close_remainder_to`. | Remove it. |
| `invalid_exact_avm_payload_fee_payer_has_rekey_to` | `ERR_FEE_PAYER_HAS_REKEY` | The fee payer transaction sets `rekey_to`. | Remove it. |
| `transaction_simulation_failed` | `ERR_SIMULATION_FAILED` | algod's simulation rejected the group: insufficient balance, an account below its minimum balance, a missing ASA opt-in, expired rounds. The node's message is in `invalid_message`. | Fund or opt in the account named in the message; rebuild with fresh parameters. |
| `transaction_failed` | `ERR_TRANSACTION_FAILED` | Settlement: submitting or confirming the group failed. | Check algod, the fee payer's balance and the validity rounds; the buyer can pay again. |

Three constants are defined for compatibility but not returned by the facilitator: `invalid_exact_avm_payload` (`ERR_INVALID_PAYLOAD`), `invalid_exact_avm_payload_transaction_decode_failed` (`ERR_TXN_DECODE_FAILED`) and `invalid_exact_avm_payload_transaction_type` (`ERR_INVALID_TXN_TYPE`). A wrong transaction type is reported as `invalid_exact_avm_payload_asset_id_mismatch`.

## ai-economy-sdk exceptions

Since 0.5.0, every exception `ai-economy-sdk` raises on purpose derives from `AIEconomyError` (in `ai_economy_sdk.exceptions`, also exported from `ai_economy_sdk`). Each class also derives from the type raised at the same place in 0.4.x, so existing handlers keep working.

```text
AIEconomyError
├── ConfigurationError (ValueError)
│   ├── UnknownNetworkError
│   └── AlgodClientRequiredError (also RuntimeError)
├── SpendLimitExceededError
├── PaymentVerificationError (RuntimeError)
├── PaymentRequiredError (fastmcp ToolError)
└── SettlementError (fastmcp ToolError)
```

| Exception | Also a | Raised when |
|---|---|---|
| `ConfigurationError` | `ValueError` | Invalid arguments, detected before any payment: a fixed ALGO price in `(0, 2000]` without `allow_loss=True` ([the fee floor](https://x402-avm.github.io/build/pricing#the-fee-floor)); an unsupported `transport=` in `wrap()` or `paid_mcp_session`; a `pricing_func` that is not callable or accepts neither `(args, sender)` nor `(args)`; `subscription_issuer` set with a `chain` whose `has_active_subscription()` has no `issuer` parameter. |
| `UnknownNetworkError` | `ConfigurationError` | A network name or CAIP-2 id cannot be resolved, for example `register_network()` with a 32-character id of an unknown network (pass the full genesis hash or use `network_from_algod`). |
| `AlgodClientRequiredError` | `ConfigurationError`, `RuntimeError` | `DefaultAvmSigner` was created without `algod_client` and is asked to simulate, send or confirm. |
| `SpendLimitExceededError` | | Since 0.5.1. `paid_mcp_session`: the buyer's `spend_controls` refused a payment (price above the cap, asset not allowed). Nothing was signed. The message includes the amount and asset the server asked for; x402's `PaymentError` is the `__cause__`. In 0.5.0 this arrived as an `ExceptionGroup`. |
| `PaymentVerificationError` | `RuntimeError` | `DefaultAvmSigner` simulated a payment group and algod reported a failure. |
| `PaymentRequiredError` | FastMCP `ToolError` | A priced tool call reached the FastMCP server without passing through the gate (for example over a transport the gate does not wrap), or its payment reservation expired. The call is refused, not served unpaid. |
| `SettlementError` | FastMCP `ToolError` | The tool succeeded but the payment could not be settled. The tool output is withheld and the reservation released, so the buyer is not charged. |

Other responses from a gated server:

| What the caller sees | Cause |
|---|---|
| `402` with `{"error": "Replay detected"}` | The same signed payment was presented again while its first use is still reserved. A payment that was already settled fails verification first: resending it returns `402` with `{"error": "transaction_simulation_failed"}`. |
| HTTP `400`, JSON-RPC error `batched tools/call requests are not supported` | A JSON-RPC batch contains a `tools/call`. |
| HTTP `500`, JSON-RPC error `payment gate misconfigured` | A priced call arrived but `SettleOnSuccessMiddleware` was never installed (the app was built without `wrap()` and without `mcp.add_middleware(SettleOnSuccessMiddleware(gate))`). |
| MCP result with `isError=True` | The tool itself failed; the payment was discarded and nothing was submitted. |

Refusals, settlement failures, expired reservations and failed chain lookups are logged at `WARNING` to the `ai_economy_sdk` logger.

## x402-algorand exceptions

Since 0.3.0, the exceptions `x402-algorand` raises derive from `X402AlgorandError` (in `x402_algorand.errors`, also exported from `x402_algorand`). Each also derives from the built-in type raised before 0.3.0.

| Exception | Also a | Raised when |
|---|---|---|
| `NetworkError` | `ValueError` | A network id is malformed, unsupported or not registered, for example a client scheme asked to pay on a network with no configured algod and no `algod_url`. |
| `NetworkMismatchError` | `NetworkError` | algod is on a different network than requested: the client's algod differs from the requirement's network (`algod is on <id> but the payment is for <id>`), or a `ServerAvmSigner`'s algod differs from the network it is used for. |
| `TransactionDecodeError` | `ValueError` | Bytes or base64 text are not a decodable Algorand transaction (`decode_transaction_bytes`, `decode_base64_transaction`). |
| `SigningError` | `ValueError` | `PayerAvmSigner` / `ServerAvmSigner` got a malformed key or mnemonic, or was asked to sign for an address it does not manage. |
| `SimulationError` | `RuntimeError` | `ServerAvmSigner.simulate_group` was told by algod that the group would fail. |

An invalid payment never raises: `verify()` and `settle()` return a response with one of the [facilitator error codes](#facilitator-error-codes) above. The package logs to the `x402_algorand` logger.

## x402 client errors (Python)

| Where | Error | Cause |
|---|---|---|
| `x402Client.create_payment_payload` | `NoMatchingRequirementsError` | No offer passes the spend controls and policies, or none is for a registered network. |
| `wrapHttpxWithPayment` / `x402HttpxClient`, and so `paid_mcp_session` | `x402.http.clients.httpx.PaymentError` (`Failed to handle payment: ...`), caused by the `NoMatchingRequirementsError` | The same refusal, raised by the HTTP client. `paid_mcp_session` converts it to [`SpendLimitExceededError`](#ai-economy-sdk-exceptions) (0.5.1; in 0.5.0 it reached your code inside an `ExceptionGroup`). |

## x402-avm-js errors

Since 0.4.0, every error extends `X402AvmError` and carries a stable `code` string; `name` is the class name.

| Class | `code` | Thrown when |
|---|---|---|
| `SpendLimitExceededError` | `spend_limit_exceeded` | The amount is above the cap (1,000,000 base units by default). Properties `asset`, `amount`, `limit`. Thrown before anything is signed. |
| `PaymentNotAllowedError` | `payment_not_allowed` | The asset, network or recipient is not on an allow list. Properties `field` (`asset`, `network` or `payTo`) and `value`. |
| `InvalidPaymentRequiredError` | `invalid_payment_required` | The `PAYMENT-REQUIRED` header or a requirement is malformed (`decodePaymentRequired`, `parsePaymentRequired`, `buildExactAvmGroup`). |
| `NoSupportedRequirementError` | `no_supported_requirement` | The challenge has no `accepts` entries. |
| `NetworkMismatchError` | `network_mismatch` | No offer is for the wallet's network, or a requirement's `extra.genesisHash` / `extra.genesisId` differs from algod's. Properties `offered`, `expected`. |
| `UnknownNetworkError` | `unknown_network` | A network name or CAIP-2 id is not recognised (`resolveNetwork`, `algodClient` without a URL). For LocalNet use `networkFromAlgod`. |
| `InvalidOptionsError` | `invalid_options` | A spend control is malformed, for example a negative `maxAmount`. |

Errors raised by algod or by your `signTxns` callback propagate unchanged.

Source: https://x402-avm.github.io/reference/errors
