# Pay per call with x402

An agent with no account can buy one chat completion and pay for it in USDC. It needs no API key, no workspace and no balance. The payment is the only credential.

The route uses [x402](https://x402.org), an open protocol for payment over HTTP. Any x402 client can pay it. The exchange runs the call through the same auction as an ordinary request.

## The route

```
POST https://router.inference.ai.exchange/x402/v1/chat/completions
```

- The body is an OpenAI chat completion request. It must have `model` and `messages`.
- `model` is a model name, such as `architect.auto`, or a name from [`GET /v1/models`](https://liquidinference.ai/docs/api-models.md). With `architect.auto`, the exchange picks the model.
- One route serves every model. The price depends on the model and on `max_tokens`.
- The answer is an ordinary chat completion with the `exchange` block.

| Item     | Value                                               |
| -------- | --------------------------------------------------- |
| Network  | Base (`eip155:8453`)                                |
| Asset    | USDC (`0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913`) |
| Scheme   | `exact`                                             |
| Protocol | x402 version 2                                      |

The facilitator pays the gas, so the wallet of the agent needs only USDC.

## The payment flow

1. Send the request with no payment. The exchange answers `402 Payment Required` with the price.
2. Sign a payment for that price with the wallet of the agent.
3. Send the same request again, with the payment in the `PAYMENT-SIGNATURE` header.
4. The exchange checks the payment and runs the call.
5. If the call succeeds, the payment settles. The answer has a `PAYMENT-RESPONSE` header with the transaction.

An x402 client library does steps 2 and 3 for you.

### The price

The `402` answer has the price in the `PAYMENT-REQUIRED` header and in the body. The `amount` is in USDC units of one millionth of a dollar, so `1000` is $0.001.

```bash
curl -i https://router.inference.ai.exchange/x402/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"model": "architect.auto", "messages": [{"role": "user", "content": "Say hello."}], "max_tokens": 64}'
```

```json
{
  "x402Version": 2,
  "error": "PAYMENT-SIGNATURE header is required",
  "resource": {
    "url": "https://router.inference.ai.exchange/x402/v1/chat/completions",
    "mimeType": "application/json",
    "serviceName": "Architect",
    "iconUrl": "https://inference.ai.exchange/favicon.png"
  },
  "accepts": [
    {
      "scheme": "exact",
      "network": "eip155:8453",
      "amount": "1000",
      "asset": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
      "payTo": "0x…",
      "maxTimeoutSeconds": 600
    }
  ]
}
```

The price is the [price cap](https://liquidinference.ai/docs/glossary.md) of the offer that the exchange tries first for a request of this size. A larger `max_tokens` or a longer prompt gives a higher price. The price changes with the book, so ask again before each call.

The amount that you pay is the price cap of the call. The exchange never spends more than that amount. You pay the full amount, not the measured use. An account pays only for the tokens used. See [Fees](https://liquidinference.ai/docs/fees.md).

### When you pay nothing

The payment settles only after the call succeeds. You pay nothing when:

- no offer can serve the call under the amount that you paid,
- the provider fails before the answer is complete,
- the exchange cannot reach the facilitator.

## Limits

- **No streaming.** The exchange settles the payment after the whole answer. A body with `stream: true` gets `400`.
- **Output.** `max_tokens` is 8,192 at most. When you do not set it, the exchange uses 1,024. `max_completion_tokens` is the same limit.
- **No purchasing rules.** The exchange ignores the `exchange` object of the body. The paid amount sets the cap, and the exchange uses its default rules.
- **One payment for each call.** The exchange refuses a payment that it saw before.
- **Daily pause.** The exchange can pause paid calls until the next UTC day. A paused call gets `503 x402_paused` and pays nothing.

For limits that are larger, streaming or [purchasing rules](https://liquidinference.ai/docs/rules.md), make an account. See the [Quickstart](https://liquidinference.ai/docs/quickstart.md).

## Errors

| Status | Code                      | Cause                                                                                    |
| ------ | ------------------------- | ---------------------------------------------------------------------------------------- |
| `400`  | `invalid_request`         | The body is not JSON, has no `model` or `messages`, streams or asks for too many tokens. |
| `400`  | `invalid_payment`         | The `PAYMENT-SIGNATURE` header cannot be read.                                           |
| `402`  | none                      | A price, or a payment that the exchange refused. `error` gives the reason. Pay again.    |
| `402`  | `settlement_failed`       | The call ran, but the payment did not settle.                                            |
| `404`  | `no_offer`                | No offer serves the model now.                                                           |
| `404`  | `x402_off`                | The exchange sells nothing over x402 now.                                                |
| `503`  | `x402_paused`             | Paid calls are paused until the next UTC day.                                            |
| `503`  | `facilitator_unavailable` | The exchange cannot reach the facilitator. You paid nothing.                             |

The other errors of a call are the same as for [Chat Completions](https://liquidinference.ai/docs/api-chat.md). See [Errors](https://liquidinference.ai/docs/errors.md).
