The request path
402, sign, verify, stream, bill. Inference starts optimistically while settlement runs beside it.
HTTP has had a status code for payments since 1997: 402 Payment Required. The x402 protocol finally uses it, and the whole request path is that status code doing its job. No account, no API key, no subscription: a single HTTP exchange carries the query, the price, and the payment.
Beat by beat
- POST. The client sends an OpenAI-compatible chat request to
/v1/chat/completions. No payment attached yet. - Auction. The router runs the second-score auction against the in-memory quote book in under 1 ms. No round trip to providers, no added latency: the competition already happened, in the standing quotes.
- 402. The response is
402 Payment Requiredwith the worst-case cost at the winner's cleared prices: estimated input tokens plusmax_tokensof output, at the rates the auction just cleared. This is an upper bound, not the bill. - Sign and retry. The client signs a USDC authorization for that maximum and retries the same request with the x402 v2
PAYMENT-SIGNATUREheader. Wallet libraries and the SDK do this automatically. - Verify, then start optimistically. The router verifies the signature and balance in about 100 ms, then dispatches to the winning provider immediately. On-chain settlement on HyperEVM runs concurrently with inference, so chain latency never touches time-to-first-token. Tokens stream back through the metering proxy while the transaction confirms.
- Bill actuals. At stream end the router bills the metered token counts at the cleared rates, never the authorized maximum. Overage refunds automatically (above a $0.01 dust floor, via the hourly cron), and the request gets a signed receipt.
What optimistic start risks, and how it is bounded
Starting inference before settlement confirms means the router briefly fronts the cost of a request whose payment could still fail. That exposure is deliberately small and capped twice: per wallet (a hard limit on unsettled value any one payer can have in flight) and router-wide (a global cap across all wallets). A wallet whose settlement fails is demoted to settle-before-stream until it proves good again. The worst case is a bounded, priced risk, and in exchange the customer never waits on a chain.
/v1/analytics shows exactly how the price was formed.- router/src/server.ts orchestrates auction, 402, verify, stream, bill
- router/src/payments.ts x402 flow and the optimistic-start exposure caps
- router/src/proxy.ts SSE passthrough, token metering, TTFT timing