> ## Documentation Index
> Fetch the complete documentation index at: https://shinydapps-bd9fa40b.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Error Reference

> Semantic error codes from l402-kit for agents and developers.

All errors from l402-kit follow a structured JSON format so agents can parse and act on them programmatically.

```json theme={null}
{
  "error": "ERROR_CODE",
  "code": 402,
  "cause": "Human-readable explanation",
  "action": "recommended_action",
  "retry": false
}
```

***

## Error codes

### PAYMENT\_FAILED

```json theme={null}
{
  "error": "PAYMENT_FAILED",
  "code": 402,
  "cause": "Insufficient balance in wallet",
  "action": "increase_budget",
  "retry": false
}
```

Wallet could not pay the invoice. Check balance and `budgetSats` limit.

***

### TOKEN\_EXPIRED

```json theme={null}
{
  "error": "TOKEN_EXPIRED",
  "code": 402,
  "cause": "Macaroon expiry timestamp exceeded",
  "action": "request_new_invoice",
  "retry": true
}
```

The L402 token was generated but not used before its TTL (default: 1 hour). Retry the request to get a fresh invoice.

***

### BUDGET\_EXCEEDED

```json theme={null}
{
  "error": "BUDGET_EXCEEDED",
  "code": 402,
  "cause": "Session budget of {n} sats exhausted",
  "action": "stop_or_increase_budget",
  "retry": false
}
```

Agent has spent more than `budgetSats` in the current session. Raise the budget or end the session.

***

### INVALID\_MACAROON

```json theme={null}
{
  "error": "INVALID_MACAROON",
  "code": 402,
  "cause": "Macaroon is not valid base64 JSON",
  "action": "request_new_invoice",
  "retry": true
}
```

The macaroon could not be decoded. Usually caused by a corrupted token. Retry from scratch.

***

### TOKEN\_ALREADY\_USED

```json theme={null}
{
  "error": "TOKEN_ALREADY_USED",
  "code": 401,
  "cause": "Preimage was already presented — replay attack blocked",
  "action": "do_not_retry",
  "retry": false
}
```

Replay protection triggered. Each preimage can only be used once. Never reuse tokens.

***

### NODE\_UNREACHABLE

```json theme={null}
{
  "error": "NODE_UNREACHABLE",
  "code": 503,
  "cause": "Lightning node did not respond within timeout",
  "action": "retry_with_backoff",
  "retry": true
}
```

The Lightning provider is unreachable. Retry with exponential backoff.

***

### INVOICE\_EXPIRED

```json theme={null}
{
  "error": "INVOICE_EXPIRED",
  "code": 402,
  "cause": "BOLT11 invoice expired before payment was submitted",
  "action": "request_new_invoice",
  "retry": true
}
```

BOLT11 invoices expire (default: 600 seconds). Retry the original request to receive a fresh invoice.

***

### WRONG\_PREIMAGE

```json theme={null}
{
  "error": "WRONG_PREIMAGE",
  "code": 402,
  "cause": "SHA256(preimage) does not match paymentHash in macaroon",
  "action": "check_wallet_payment",
  "retry": false
}
```

Cryptographic verification failed. The preimage does not prove payment of this invoice.

***

## Handling errors in agents

```typescript theme={null}
import { l402 } from "l402-kit/agent";

try {
  const res = await client.fetch("https://api.example.com/data");
} catch (err: any) {
  if (err.error === "BUDGET_EXCEEDED") {
    // stop and report to user
  } else if (err.retry === true) {
    // safe to retry after short delay
    await sleep(1000);
    const res = await client.fetch("https://api.example.com/data");
  }
}
```
