Calling a seller
To call a seller, send an ordinary OpenAI-compatible chat request straight to the route's /v1/chat/completions endpoint, with a signed hold in three headers and the salt for its request commitment in a fourth. The seller checks the hold, serves the request, and returns a signed receipt: in a response header for normal requests, or as a final im_receipt event for streams. The SDK does all of this inside chat.completions.create.
The code on this page assumes the sdk and route from the buyer quickstart.
With the SDK
Non-streaming returns the completion once its receipt is verified:
const completion = await sdk.chat.completions.create(
{ model: "my-model", messages: [{ role: "user", content: "Name three prime numbers." }], max_tokens: 64 },
{ capMicros: 10_000n, timeoutMs: 30_000 },
);
console.log(completion.choices[0]?.message.content, completion.usage);
Streaming returns an async iterable of OpenAI chunks. Consume it to the end or abort it:
const controller = new AbortController();
const chunks = await sdk.chat.completions.create(
{ model: "my-model", messages: [{ role: "user", content: "Count to five." }], stream: true },
{ signal: controller.signal },
);
let text = "";
for await (const chunk of chunks) {
text += String(chunk.choices[0]?.delta.content ?? "");
if (text.length > 2_000) controller.abort(); // stop early; you pay for what the receipt says
}
Each agent in a swarm should call through its own lane, sdk.agent("worker-7").chat.completions.create(...); see budgets for agent swarms.
What the SDK does on each call
- Serializes your body once and checks it is valid OpenAI JSON under 1 MiB.
- Asks the planner for an eligible route and reserves the cap on that seller's channel and your lane.
- Draws a fresh 32-byte request ID and salt, computes
SHA256(salt ‖ body bytes), and signs a hold that expires 10 seconds later. - Records the hold in its wallet journal, if one is configured (the Solana backend's default), then posts the exact same body bytes with redirects disabled.
- On a 2xx, streams or reads the body, verifies the receipt against the hold and the signed price schedule, and releases the unused part of the cap.
- Queues the receipt for the next voucher.
Retries
- Overload or transport failure before a status line (429, 5xx, connection error): the attempt is unbillable, and the SDK may try another eligible route with a fresh request ID and salt, up to
maxAttempts(default 3). - A 2xx status: the attempt may already be billable, so the SDK never retries it elsewhere. If the stream breaks, it recovers the receipt in the background.
- A draining gateway (
503 gateway_drainingwithRetry-After): no hold was consumed, so the SDK waits and replays the exact same hold, salt and body without spending an attempt. - 402: only
insufficient_channel_fundsis retried, and only after a voucher acknowledgment frees budget. See errors.
On the wire
If you implement a client yourself, the request is:
POST https://seller.example.com/v1/chat/completions
Content-Type: application/json
X-IM-Hold: <base64 of the 275-byte hold>
X-IM-Hold-Sig: <base64 of the Ed25519 signature by the session key>
X-IM-Session: <base58 session public key>
X-IM-Salt: <base64 of the 32-byte salt>
{"model":"…","messages":[…],"stream":true}
The commitment is over the exact body bytes you send; do not let a library re-serialize the body after you hash it.
A non-streaming success carries the receipt in a header:
HTTP/1.1 200 OK
X-IM-Receipt: <base64 receipt>.<base64 signature>
A stream ends with one receipt event, then [DONE]:
data: {"object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}], …}
event: im_receipt
data: {"receipt":"<base64 receipt>","sig":"<base64 signature>"}
data: [DONE]
Reject a stream with two receipts, or content after the receipt. If the receipt never arrives, fetch it with GET /im/v1/requests/{request_id_hex}/receipt, which returns {receipt, sig} or 404.
What decides whether you pay
- Any non-2xx answer, or a connection failure before the status line, is unbillable. A later receipt for it must say
failedornot_servedat charge 0. - After a 2xx status, the request is billable per the seller's receipt, even if you abort. A disconnect after output started is billed as
cancelled: input tokens plus the per-request fee. - For non-streaming requests, the receipt's
ttft_msis the time to the first upstream byte, and the gateway enforcesttft_max_msagainst it. If the model is too slow, the request endsfailedat charge 0.