Introducing a new way to review your PRs.

Read the announcement

solana-foundation / pay

CLI for Agentic payments (x402, MPP, AP2).

View on GitHub

30 documents · Updated Nov 11, 2026

docs / runtime-inference-marketplace-selling

Inference marketplace selling

Inference marketplace selling lets an account publish a paid OpenAI-compatible model offer and let a local agent answer buyer requests. Pay-connect owns the public payment boundary and request queue. The seller-side CLI or MCP flow runs the agent and sends each answer back for delivery and settlement. See the parent overview in Rust payment runtime and the related pages on session channels and metering, client payment flows, CLI server and inference, MCP payment tools, and cloud payment onboarding.

Marketplace role

The marketplace separates a public paid offer from the private machine that serves it. A buyer calls the public model URL. Pay-connect checks payment, reserves room for the request, and waits for the seller worker to return the model response.

This diagram shows the main boundary between the buyer, the marketplace, and the seller machine.

flowchart LR
    B[Buyer] -->|paid OpenAI request| C[Pay-connect endpoint]
    C -->|verify payment and admit| Q[Seller request queue]
    W[Seller CLI or MCP worker] -->|claim and answer| Q
    Q -->|response stream| C
    C -->|OpenAI response| B
    C -->|settle earnings| S[Seller recipient]

Offer publication

An offer names one model, its payee, accepted stablecoins, network, price, and earning cap. The public offer exposes a model listing route for discovery and a paid chat-completions route for inference. The creator must have a wallet-backed account because the marketplace limits endpoint ownership and pays earnings to a wallet. rust/…/routes.rs• L63-95

References: rust/…/sell_inference.rs• L118-142 rust/…/routes.rs• L512-567

Payment boundary

The paid route is an ordinary payment-gated API. Connect builds payment backends for that endpoint and directs settled value to the seller's recipient. The offer may use session payments and x402 up-to payments. Flat request prices can also use batch settlement when the deployment has durable voucher storage. rust/…/backends.rs• L362-405

The payment gate runs before the request enters the seller queue. An unpaid request therefore does not consume seller capacity or create a model turn. rust/…/sell_pregate_memory.rs• L67-130

References: rust/…/mod.rs• L1-13 rust/…/backends.rs• L418-450

Seller lifecycle

Selling has three linked stages: create a public offer, run a worker that answers its queue, and stop or reprice the offer as needed. The offer remains useful only while the seller worker can claim requests and the endpoint has earning capacity.

Endpoint creation

The seller sends pricing and model terms to POST /v1/endpoints. Connect returns the public base URL and an owner token. The token is the worker's control credential. It is returned at creation and is not part of the buyer-facing URL. rust/…/routes.rs• L736-800

The CLI stores the creation result in a local seller record so the worker can start without copying the token into a later command. The MCP tool follows the same remote creation path and then starts the worker on the local machine. rust/…/sell_client.rs• L131-185 rust/…/sell_inference.rs• L321-349

References: rust/…/mod.rs• L106-280 rust/…/sell_client.rs• L204-257

Worker fulfillment

The worker long-polls GET /v1/endpoints/<id>/queue/next. Connect gives it the buyer's request and marks the request claimed. The worker drives a fresh agent turn for that buyer, sends streamed chunks when requested, and finishes or fails the request through the worker API. rust/…/mod.rs• L415-480

The worker returns OpenAI-shaped completion objects. It can stream chat.completion.chunk values or return one chat.completion object. Its usage data supports token-based billing. The current ACP worker estimates tokens from character counts because its agent protocol does not provide exact token counts. rust/…/mod.rs• L765-905

This sequence shows how a paid request moves from admission to delivery.

sequenceDiagram
    participant Buyer
    participant Connect
    participant Worker
    participant Agent
    Buyer->>Connect: POST chat/completions
    Connect->>Connect: verify payment and reserve capacity
    Connect->>Worker: long-poll response with request
    Worker->>Agent: start one buyer turn
    Agent-->>Worker: text events
    Worker-->>Connect: chunks or final completion
    Connect-->>Buyer: response
    Connect->>Connect: settle observed earnings

References: rust/…/queue.rs• L1-9 rust/…/mod.rs• L524-593 rust/…/mod.rs• L651-689

Offer management

The owner can inspect an endpoint, change its pricing, or delete it through the owner-token API. Repricing changes the terms for new requests while in-flight requests keep the terms under which they were admitted. Deletion removes the public offer and lets the worker finish its current poll or exit. rust/…/routes.rs• L803-837

The CLI reports queue depth, worker state, earnings, and progress toward the cap. The MCP tool exposes the same lifecycle as create, status, reprice, and stop. rust/…/sell_inference.rs• L36-48 rust/…/sell_inference.rs• L635-714

References: rust/…/routes.rs• L680-699 rust/…/sell_inference.rs• L707-728

Capacity-aware fulfillment

The runtime protects both the marketplace and the seller from work that cannot be completed. It bounds endpoint count, waiting requests, response buffering, and total unpaid earning exposure. These limits make payment admission depend on available seller capacity rather than only on a valid payment.

Earning capacity

Each endpoint has a small earning cap. Connect reserves the maximum possible earning for every admitted request before payment handling and queueing. A request that leaves, fails, or uses less than its ceiling releases unused capacity. Settled earnings close the endpoint when the cap is reached. rust/…/routes.rs• L259-355

When no unreserved capacity remains, buyers receive a closed-capacity response before they are charged. The endpoint also closes itself after the cap is reached. This prevents concurrent requests from earning more than the seller's stated limit. rust/…/routes.rs• L1001-1069

References: rust/…/routes.rs• L38-60 rust/…/routes.rs• L1043-1060

Queue capacity

The queue keeps waiting requests separate from claimed requests. It accepts a bounded number of waiting requests and holds streamed output under both per-request and per-endpoint byte limits. A buyer that stops reading causes later worker pushes to report backpressure instead of allowing unlimited memory growth. rust/…/queue.rs• L20-27

The worker treats a full queue as a temporary service problem and treats a gone buyer as a request that no longer needs delivery. These outcomes release the related reservation when no response was delivered. rust/…/queue.rs• L203-213 rust/…/routes.rs• L935-990

References: rust/…/queue.rs• L169-200 rust/…/queue.rs• L264-337

Billing completion

Flat pricing earns the request price only when the worker completes a delivered response. Token pricing earns the reported input and output usage up to its per-request ceiling. Missing usage under token pricing earns nothing and is refunded by the metered payment path. rust/…/routes.rs• L370-410

The runtime advertises payment schemes that match the pricing model. A flat offer supports session, up-to, and batch settlement when the deployment supports them. A token offer uses response-aware session and up-to settlement because the final response is needed to know the amount. rust/…/sell_inference.rs• L49-79

References: rust/…/sell_inference.rs• L332-385 rust/…/routes.rs• L947-970

Selling interfaces

The CLI, MCP tool, and Connect HTTP API are three views of one seller lifecycle. The CLI and MCP tool create and manage offers. Connect provides the public buyer route and the private queue protocol used by the worker.

CLI selling flow

pay sell create publishes an offer and prints its base URL, model, payment schemes, recipient, and owner token. pay sell serve <id> uses that record or an explicit token to run the seller agent. The CLI keeps endpoint credentials away from the agent process and defaults the agent to a separate work directory with tool access refused. rust/…/mod.rs• L352-413

The CLI is the direct seller interface for scripted or terminal use. It accepts either a flat request price or per-token rates with a request ceiling. It also exposes the endpoint's network, currencies, session close setting, and earning cap at creation. rust/…/mod.rs• L108-165

References: rust/…/mod.rs• L44-59 rust/…/sell_cli.rs• L8-83

MCP selling flow

The sell_inference MCP tool gives an agent a controlled way to publish its own inference service. It asks for an earning cap when the caller did not provide one, so publishing a stranger-callable paid endpoint is an explicit user action. It refuses hosted sessions without local files because the worker must run on the seller's machine. rust/…/sell_inference.rs• L134-154

After creation, the tool starts the same pay sell serve worker used by the CLI. It saves the endpoint record and reports the buyer URL, model, price, worker, log, recipient, and cap. Buyers are charged only for answered requests. rust/…/sell_inference.rs• L321-397

References: rust/…/sell_inference.rs• L251-341 rust/…/sell_inference.rs• L458-505

Connect HTTP interface

Connect exposes two related HTTP surfaces. Sellers use owner-authenticated endpoint routes to create, view, reprice, delete, poll, push chunks, complete, and fail requests. Buyers use the public endpoint URL and the OpenAI-compatible model and chat-completions routes. rust/…/routes.rs• L614-640

The endpoint view is the shared contract for seller tools and status displays. It reports the base URL, model, pricing, accepted schemes, recipient, queue depth, earned value, pending value, cap, and closed state. The owner token appears only in the creation response. rust/…/routes.rs• L543-567

References: rust/…/sell_client.rs• L43-128 rust/…/routes.rs• L680-699

Inference offer contract

The offer contract keeps model discovery, request format, and usage billing aligned. Buyers can use normal OpenAI-shaped clients while sellers choose whether to charge per request or by observed token use.

OpenAI-compatible surface

The marketplace publishes GET /v1/models as a free discovery call and POST /v1/chat/completions as the paid inference call. The model listing returns the single model named by the seller. The paid call supports both buffered and streamed responses. rust/…/sell_inference.rs• L32-41 rust/…/routes.rs• L1093-1101

The wider runtime also defines an OpenAI-compatible profile with responses, chat completions, embeddings, models, and legacy completions surfaces. Marketplace selling currently publishes the chat-completions and models routes because the seller worker answers chat requests. rust/…/inference.rs• L7-52

References: rust/…/sell_inference.rs• L241-281 rust/…/inference.rs• L6-28

Pricing and usage

Flat pricing charges one amount for each delivered request. Per-token pricing sets input and output rates per million tokens and a maximum request amount. Sellers may provide a default rate and model-specific rates. The runtime uses the model in the buyer request to select the applicable rate. rust/…/routes.rs• L428-510

For token offers, the response supplies prompt and completion usage in the OpenAI usage shape. The paywall reads those values after the response and caps the result at the seller's maximum. This is why token pricing does not use payment schemes that settle before response usage is known. rust/…/sell_inference.rs• L353-385

References: rust/…/sell_inference.rs• L389-433 rust/…/routes.rs• L962-970

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the pricing-to-scheme rules aligned across SellPricing, the generated paywall, and Connect's available payment backends.
  • Keep the owner-token creation response aligned with the local SellRecord fields used by both CLI and MCP workers.
  • Keep queue admission, reservation release, and settlement completion changes consistent so abandoned or partial requests cannot consume the earning cap.
  • Keep worker chunk sizing below the Connect push limit and preserve backpressure handling for slow buyers.
  • Keep OpenAI usage field names aligned between worker responses and token metering.
  • Keep session terms stable for live channels when repricing an endpoint.

Important failure modes

TriggerConsequenceGuard
Seller worker is offline or misses the claim windowBuyer receives a service-unavailable response and the request does not earn for the sellerConnect waits only for a bounded claim period and releases the reservation
Endpoint has no unreserved earning capacityBuyer is rejected before payment reaches the queueConnect reserves each request's maximum possible earning before admission
Buyer stops reading a streamWorker pushes hit backpressure instead of growing memory without limitQueue bounds buffered bytes per request and per endpoint
Token pricing response has no usable usage dataNo token earnings are recorded for that requestToken metering refunds missing usage and the seller worker reports usage in the response
Seller reaches the earning capNew buyer and worker requests receive endpoint-closed responsesSettled earnings close the endpoint and the worker exits after its queue drains
Session terms change while buyers hold channelsRebuild of the payment backend is refusedSession backends retain live channels and require stable session terms

Key files

FileWhy to read it
rust/…/sell_inference.rsDefines the seller offer, pricing modes, paywall routes, metering, and payment scheme rules.
rust/…/routes.rsShows the public buyer boundary, seller API, admission, capacity, settlement, and endpoint lifecycle.
rust/…/queue.rsExplains request ownership, worker claims, streaming delivery, and queue backpressure.
rust/…/backends.rsShows how each seller endpoint gets payment backends and how live session channels survive repricing.
rust/…/mod.rsShows the CLI create and serve flows that run the seller worker.
rust/…/sell_inference.rsShows how MCP asks for consent, creates an offer, starts the worker, and manages it.
rust/…/sell_client.rsDefines the HTTP client and local seller record shared by CLI and MCP.
rust/…/inference.rsDefines the wider OpenAI-compatible inference surface used by runtime model serving.