Chat completion
chatRuns a chat completion through the metered gateway and returns the text with its receipt (tokens, price, cost, balance, settlement). Needs Authorization: Bearer sk_... and spends from that key's balance; a hold for the maximum cost is placed first. Not streamed. Output is capped at 4096 tokens. A call without a progress token answers within 55 s of arriving. With a progress token, progress is sent every 15 s and the call may run 10 minutes, so the client must reset its timeout on progress (resetTimeoutOnProgress: true in the TypeScript SDK) or wait that long. At its deadline, or when the client's connection closes, a call is cut: the text produced so far is returned with finish_reason "length" and billed on the provider's usage report where the provider gives one, otherwise on an estimate never above max_tokens; for a model whose reasoning is not streamed, at the held output budget. Cancellation messages are not supported: a call the client stops waiting for without disconnecting runs to completion or its deadline and is billed. With no key, the call is paid with x402 in USDF (Permit2) or USDG (EIP-3009), as POST /x402/v1/chat/completions: without a payment it returns isError with the payment requirements (x402Version, resource, accepts) as structuredContent; sign one accept and call again with the payment in _meta["x402/payment"], the payment argument or the PAYMENT-SIGNATURE header. The quote is the call's maximum cost at the published price (prompt at one token per byte, output at max_tokens, capped as above, no minimum), paid up front; the call is charged what it uses at that price and the rest is refunded in the asset paid, the gateway paying the gas. A paid call runs to its end or its deadline whether or not the client waits and returns the receipt and _meta["x402/payment-response"]; at the deadline the text so far is returned with finish_reason "length" and a note, charged what it used and the rest refunded. A call no provider accepted before its deadline, or whose provider failed or ended with nothing, is refunded in full in the asset paid; so is one cut by the deadline after the provider accepted it, with nothing produced. A call with less than 20 s left before its deadline is refused before the payment is settled (deadline_too_close), nothing charged. MPP clients pay the same quote: the payment-required result also carries _meta["org.paymentauth/payment-required"] with evm charge challenges (USDG by EIP-3009 authorization, USDF by Permit2); call again with the credential in _meta["org.paymentauth/credential"], and the paid result carries _meta["org.paymentauth/receipt"].
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| model | Yes | Model id from list_models | |
| payment | No | With no key: the x402 payment for this call's quote, as the PAYMENT-SIGNATURE value (base64 JSON) or the payload object; _meta["x402/payment"] and the PAYMENT-SIGNATURE header are read too | |
| messages | Yes | Chat messages in the OpenAI format: objects with a role (system, user, assistant or tool) and content (text, or an array of content parts) | |
| max_tokens | No | Output token budget, a positive integer; defaults to the model's default_max_output, capped at max_output | |
| temperature | No | Sampling temperature, 0 to 2 |