The request path
402, sign, verify, stream, bill. Inference starts optimistically while settlement runs beside it.
A funded request carries the query, price, and payment authorization in one HTTP exchange. Production agents use prepaid credits and a boundedx-credit-key; the x402 protocol remains the one-time funding rail and the advanced direct-payment fallback.
Beat by beat
- POST. The client sends an OpenAI-compatible chat request to
/v1/chat/completionswithx-credit-key. - 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.
- Reserve. The router reserves the worst-case cost at the winner's cleared prices: estimated input tokens plus
max_tokensof output. This is an upper bound, not the bill. - Direct fallback. Without credits, the first request returns
402; the client signs a USDC authorization and retries withPAYMENT-SIGNATURE. Wallet libraries and the SDK do this automatically. - Start and meter. The router dispatches to the winning provider immediately. Tokens stream back through the metering proxy while the credit reservation remains held.
- Bill actuals. At stream end the router bills the metered token counts at the cleared rates, never the reserved maximum. The unused remainder releases immediately, 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/http/server.ts orchestrates auction, 402, verify, stream, bill
- router/src/money/payments.ts x402 flow and the optimistic-start exposure caps
- router/src/api/proxy.ts SSE passthrough, token metering, TTFT timing