CLI server setup and inference tooling
The pay CLI turns a local API or inference service into a payment-aware gateway. It helps an operator create a paywall, start and fund a local server, publish provider identity, manage subscription plans, and inspect live inference use. It also lets agent tools choose a compatible local or hosted model through one payer route.
System role
This page sits below the payment CLI overview. It focuses on server setup and inference paths. Payment operations are covered in CLI payment operations, catalog use is covered in CLI skills and catalog, and account context is covered in CLI account management.
The CLI has two related server paths:
| Path | Main goal | Result |
|---|---|---|
pay gate api | Put payment rules in front of an existing API | A proxy that challenges paid requests and forwards valid requests |
pay gate inference | Find local model servers and expose them through one gateway | A provider-routed inference gateway with model and usage views |
The gate command exposes demo, API, inference, and scaffold workflows. The hidden gate start form remains a compatibility alias for gate api. pay gate demo provides a ready local payment example. pay gate inference can run with a terminal monitor or in headless mode. pay gate inference uses free passthrough unless pricing is supplied, and per-token pricing is restricted to sandbox mode. rust/…/mod.rs• L66-147 rust/…/start.rs• L853-919
Local gateway setup
Paywall scaffolding
The scaffold command creates a starter YAML file that describes an API identity, its upstream, and endpoint payment rules. It gives the operator a free health route and a metered content route so the file can be edited before the gateway starts. pay gate scaffold writes paywall.yml by default and refuses to replace an existing file.
The normal setup loop is:
- Create a starter file with
pay gate scaffold. - Edit the upstream, endpoint list, and payment rules.
- Start the proxy with
pay gate api paywall.yml.
References: rust/…/scaffold.rs• L3-58 rust/…/mod.rs• L13-27
Gateway startup
pay gate api loads and checks the YAML before it accepts traffic. It resolves environment templates, applies scheme defaults, and validates the full payment spec before it prepares currencies, signers, payment backends, and the proxy. Malformed routes and payment rules therefore stop startup instead of failing on the first paid request. rust/…/start.rs• L1555-1607
The gateway keeps account choice separate from the older raw signer path. An explicit CLI account takes priority, followed by PAY_ACTIVE_ACCOUNT and the account linked to the selected network. The legacy signer source is used only when no account is selected. Sandbox mode takes priority over the paywall network and pins payment setup to localnet. rust/…/mod.rs• L89-122 rust/…/start.rs• L1665-1704
The gateway can run with native TLS. The certificate and private key must be supplied as a pair. Its public listener fronts an internal control plane. Free endpoints pass through, while metered endpoints return a payment challenge until the request carries valid payment. Session endpoints also need an operator signer because the gateway manages channel settlement and close. For live MPP sessions, the gateway settles each configured interval when the session payment watermark advances. rust/…/start.rs• L1529-1553 rust/…/start.rs• L1839-1844 rust/…/start.rs• L2048-2157 rust/…/start.rs• L2912-2999
The gateway can serve an OpenAPI or Discovery document. It trims that document to the allowed endpoints, adds payment metadata, removes upstream authentication details, and presents the proxy as the public server. The public URL comes from --public-url or the request host. This lets downstream agents discover the payment-facing API without learning the upstream address. rust/…/start.rs• L1609-1648 rust/…/start.rs• L3131-3175
This diagram shows the main request path.
flowchart LR
Client[Client request] --> Gateway[Pay gateway]
Gateway -->|free route| Upstream[Upstream API]
Gateway -->|paid route without proof| Challenge[Payment challenge]
Challenge --> Client
Client -->|paid retry| Gateway
Gateway -->|verified payment| Upstream
Upstream --> Gateway
Gateway --> Response[Client response]
References: rust/…/start.rs• L1555-1607 rust/…/start.rs• L2411-2524 rust/…/start.rs• L2740-2999 rust/…/start.rs• L3131-3175
Batch payment settlement
Batch settlement lets one payment channel cover many small requests. The gateway needs an operator signer and channel state to accept this scheme. A durable Redis store lets an external worker reconcile channel state. Without a durable store, the local gateway owns reconciliation and warns that a restart can lose unclaimed voucher state. Production deployment policy can disable the backend until a durable store is available. rust/…/start.rs• L2526-2673
The local reconciliation loop settles due channels, distributes settled value, finalizes closes, and reclaims sealed channels. It scans large channel sets in bounded rotating pages. A durable store moves reconciliation to pay-worker, while the gateway records the state needed by that worker. rust/…/start.rs• L2444-2466 rust/…/start.rs• L578-850 rust/…/start.rs• L2344-2476
References: rust/…/start.rs• L2526-2673 rust/…/start.rs• L260-269 rust/…/start.rs• L578-850 rust/…/start.rs• L2344-2476
Sandbox payment preparation
Sandbox mode gives local testing a separate payment world. The gateway pins the network to localnet and uses hosted Surfpool by default. An explicit CLI RPC value or PAY_RPC_URL can point to a local validator. The shared payment tools can create an ephemeral gateway account, fund local wallets, prepare stable-token accounts, and use that account as the fee payer. A real cluster does not receive automatic funding. rust/…/payments.rs• L21-78 rust/…/mod.rs• L808-889
Startup checks the SOL balance of each signer wallet and checks the token accounts needed by stable-token payout recipients. Sandbox funding can create missing accounts. On a real cluster, the operator must prepare them first. A recipient with no SOL can still be valid because the recipient does not sign the setup transaction. rust/…/payments.rs• L219-331 rust/…/payments.rs• L383-497
pay gate demo writes the bundled playground paywall to pay-demo.yaml only when that file does not already exist. It creates a challenge-binding secret on first use and keeps the file so subscription plan data and the secret remain stable across restarts. Demo mode always uses sandbox mode. rust/…/demo.rs• L39-99
References: rust/…/payments.rs• L21-78 rust/…/payments.rs• L219-331 rust/…/payments.rs• L383-497 rust/…/mod.rs• L808-889 rust/…/demo.rs• L39-99
Subscription plan tooling
Subscription endpoints need an on-chain Plan account before the gateway can issue a usable subscription challenge. Gateway startup derives the plan identity, checks the chain, and reuses an existing plan. If the plan is missing, an interactive publish step can create it with the operator signer and write the resulting plan metadata back to the YAML. rust/…/start.rs• L1847-1873 rust/…/start.rs• L3327-3517
pay plans publish is the explicit plan workflow. Its safe default derives and prints the addresses. --write records the derived plan_id values in the YAML after another tool has published the accounts on chain. This command does not broadcast plan creation in its current version. Gateway startup also requires a stable challenge-binding secret for subscription endpoints. It rejects a missing secret because changing it across restarts would invalidate active session tokens. rust/…/plans.rs• L22-43 rust/…/plans.rs• L77-153 rust/…/start.rs• L3294-3313
Startup checks that a stored plan matches its route terms and rejects a stale or conflicting plan identity. A configured free-trial field is not honored in the current subscription flow. rust/…/start.rs• L3332-3344 rust/…/start.rs• L3374-3427
References: rust/…/plans.rs• L22-43 rust/…/plans.rs• L77-153 rust/…/start.rs• L1847-1873 rust/…/start.rs• L3294-3517
Provider identity
On-chain provider registration
Provider registration makes a public gateway discoverable through the provider registry. A registration binds the service authority to a category, protocol, name, description, and reachable endpoint. A repeat start renews an identical entry instead of creating a second identity. A conflicting entry must be removed before the service metadata can change. rust/…/provider_registration.rs• L37-89 rust/…/provider_registration.rs• L123-202
Registration is opt-in through a public URL and a versioned API profile. The API gateway uses the profile to choose its registry category and protocol. The inference gateway registers as an OpenAI-compatible inference service. The operator can pass --no-register when the service must stay private or the account is not ready to sign. Inference registration uses the resolved public address and requires an active account when no sandbox signer is available. rust/…/start.rs• L2684-2718 rust/…/mod.rs• L443-490
The running service sends a daily renewal. If renewals stop, the registry grace period makes the entry eligible for eviction. rust/…/provider_registration.rs• L170-188
References: rust/…/provider_registration.rs• L37-224 rust/…/start.rs• L2684-2718 rust/…/mod.rs• L443-490
Local catalog registration
An API gateway also exposes a local skills document at the well-known catalog path. The CLI adds an ephemeral local source so an MCP agent on the same host can find the running service. On the next gateway start, unreachable old entries are removed. rust/…/local_registration.rs• L1-15 rust/…/local_registration.rs• L43-95
This local source is separate from public on-chain registration. It supports same-machine discovery even when the gateway has no public URL or no registry signer. The gateway serves the local catalog beside its payment and discovery routes. The current API server reaps stale entries on the next start because its long-running proxy owns process exit. rust/…/start.rs• L2796-2827 rust/…/start.rs• L2941-2974
References: rust/…/local_registration.rs• L27-95 rust/…/start.rs• L2796-2827 rust/…/start.rs• L2941-2974
Inference gateway
Provider discovery
pay gate inference probes known local inference services and builds one route per service. A local provider is accepted only when its identifying response matches its expected API shape. The gateway then reads the provider's model list and serves the provider through a subdomain such as ollama.localhost. rust/…/mod.rs• L561-623 rust/…/discovery.rs• L182-267
The built-in registry covers Ollama, LM Studio, llama.cpp, vLLM, and exo. A user registry at ~/…/inference-providers.yml can add providers or replace a built-in entry by slug. Probe order decides which provider claims a shared port. The --providers filter limits discovery to selected slugs. rust/…/mod.rs• L127-180 rust/…/discovery.rs• L140-169 rust/…/mod.rs• L566-570
The command stops before opening the gateway when no provider is found. It can watch provider status after startup. A server that goes down becomes unavailable in the live view. A newly found provider needs a gateway restart before it becomes a routed endpoint. rust/…/mod.rs• L617-623 rust/…/mod.rs• L965-1008
References: rust/…/mod.rs• L561-623 rust/…/discovery.rs• L12-17 rust/…/discovery.rs• L182-267 rust/…/mod.rs• L965-1008
Provider adapters
An inference provider adapter gives the gateway one common view of a server. It identifies the server, lists models, names the routes that can be paid, and classifies request types. This keeps provider-specific API shapes at the edge while the gateway uses one routing and metering model. rust/…/mod.rs• L26-41 rust/…/mod.rs• L127-180
The main adapter groups are:
| Provider group | User-facing behavior |
|---|---|
| Ollama | Supports native, OpenAI-compatible, and Anthropic-compatible routes |
| LM Studio, llama.cpp, vLLM, exo | Use OpenAI-compatible model and generation routes, with provider-specific identification |
| Custom local provider | Describes probes, model JSON shape, and paid routes in the user registry |
| Hosted catalog provider | Uses catalog endpoints, model data, and pricing instead of local port probes |
The adapter also declares a wire dialect. Claude can use Anthropic or OpenAI-compatible providers. Codex, Goose, and Qoder require an OpenAI-compatible route. rust/…/mod.rs• L31-99
References: rust/…/mod.rs• L127-180 rust/…/custom.rs• L37-119 rust/…/mod.rs• L75-99
Hosted catalog providers
Hosted catalog providers let the agent picker show remote inference gateways beside local servers. The provider identity, service URL, endpoints, model data, and pricing come from a resolved skills-catalog entry. The hosted entry is checked for reachability rather than a local provider signature. rust/…/catalog.rs• L39-159 rust/…/catalog.rs• L345-417
The default hosted set includes Alibaba Model Studio, Google Gemini, and BlockRun. Alibaba and Gemini keep fallback route and model information available while their catalog details are missing or stale. A catalog entry can expose native routes or OpenAI-compatible routes. Gemini model lists are filtered to models that support content generation, while OpenAI-style model lists can also provide per-model prices. rust/…/catalog.rs• L11-25 rust/…/catalog.rs• L162-343 rust/…/catalog.rs• L645-725
Hosted pricing follows the catalog's model variants. The first matching model family wins, and a default variant covers models only when the catalog declares that fallback. This keeps the picker price close to the gateway's runtime price instead of showing an unrelated provider-wide range. rust/…/catalog.rs• L469-548 rust/…/catalog.rs• L599-619
References: rust/…/catalog.rs• L11-25 rust/…/catalog.rs• L39-159 rust/…/catalog.rs• L162-343 rust/…/catalog.rs• L345-548 rust/…/catalog.rs• L645-808 rust/…/catalog.rs• L855-905
Inference pricing
Without a pricing source, the inference gateway is a free passthrough. It still records exchanges for the debugger and live view. With a rates file or --price, the gateway creates priced routes for the provider's supported generation endpoints. Each priced route uses an x402-upto payment with a per-request USD ceiling. rust/…/mod.rs• L1-15 rust/…/mod.rs• L116-147 rust/…/mod.rs• L273-321
Rates use USD per one million tokens. They can be set for named models or through a default rate. A base model name can match a tagged served model such as gemma4:latest. The gateway checks named rates against discovered models and checks that each routed provider has a usable rate. rust/…/mod.rs• L907-932 rust/…/pricing.rs• L1-4
Per-token charging is sandbox-only. The gateway authorizes an upper amount before the request, observes input and output usage after the response, and settles the measured cost. It reads usage from the provider response and charges input and output tokens at their configured rates. The payment stack uses localnet, USDC, an ephemeral gateway wallet, Surfpool preparation, and x402-upto. rust/…/mod.rs• L273-289 rust/…/mod.rs• L799-890 rust/…/payments.rs• L666-720
The rates parser is shared with the core pricing model. The CLI accepts one YAML file through positional RATES or the legacy --pricing option, or one inline --price value. These sources are mutually exclusive. rust/…/mod.rs• L292-321 rust/…/pricing.rs• L1-4
References: rust/…/mod.rs• L1-15 rust/…/mod.rs• L273-321 rust/…/mod.rs• L907-932 rust/…/pricing.rs• L1-4 rust/…/payments.rs• L666-720
Inference discovery documents
Each discovered provider receives a generated API description. The document includes the provider's supported routes and model discovery route. In priced mode it also carries payment offers and per-model metering hints so tools can inspect the service before making a request. The runtime payment challenge remains authoritative. rust/…/mod.rs• L1017-1059
The gateway serves the matching document from /openapi.json. Requests use the provider host name to select the right document. This gives agent clients a stable discovery surface while the gateway keeps the local upstream addresses private. rust/…/mod.rs• L699-731
References: rust/…/mod.rs• L646-731 rust/…/mod.rs• L1017-1059
Agent and TUI workflows
Provider picker
The provider picker is the interactive choice point for alternate agent routing. It combines local providers, a running local inference gateway, and hosted providers from the skills catalog. The user can search by provider or model, move between models, or enter a custom discovery URL. rust/…/provider_picker.rs• L31-69 rust/…/mod.rs• L277-337
Hosted rows are loaded in the background so the picker can open while remote discovery continues. A custom source is checked for an OpenAPI or catalog document, then saved as an inference source when discovery succeeds. Catalog model prices and model-specific variants are shown when the source provides them. rust/…/provider_picker.rs• L85-150 rust/…/mod.rs• L648-780 rust/…/catalog.rs• L469-548
References: rust/…/provider_picker.rs• L153-417 rust/…/mod.rs• L137-257 rust/…/mod.rs• L968-1081 rust/…/catalog.rs• L469-548
Agent routing
Claude, Codex, Goose, and Qoder use the same provider choice and payer proxy flow. The launcher maps the chosen provider and model into the environment or configuration shape expected by that agent. This keeps payment and discovery logic in the CLI while each agent keeps its own process contract. rust/…/mod.rs• L137-257 rust/…/claude.rs• L19-121
When Claude targets an OpenAI-compatible provider, the payer proxy translates the Anthropic Messages request and response. The stream path preserves incremental output and usage events. The translation is not lossless for some Anthropic-only content such as images and thinking blocks. rust/…/translate.rs• L1-13 rust/…/translate.rs• L310-317
Goose and Qoder use their OpenAI-compatible or direct external-provider settings. Qoder checks that the account can use direct external providers before it starts an alternate route. rust/…/goose.rs• L28-115 rust/…/mod.rs• L81-217
References: rust/…/mod.rs• L452-545 rust/…/claude.rs• L132-220 rust/…/goose.rs• L28-115 rust/…/mod.rs• L81-159
Live inference monitor
The inference TUI is a live monitor for the gateway. It shows reachable inference servers with their models and prices. It also shows connection aggregates with request counts, token totals, paid totals, and recent activity. It consumes debugger events without changing the stored debugger history. rust/…/inference.rs• L30-104 rust/…/inference.rs• L433-607
The monitor follows the newest connection by default. The user can pin a row, filter to errors or one provider, clear the display, open the web UI, or quit. Provider status updates and connection updates arrive while the gateway continues to serve requests. rust/…/inference.rs• L181-357 rust/…/inference.rs• L376-410
In a non-interactive terminal, --no-tui leaves the gateway in headless mode. In an interactive terminal, the gateway runs beside the TUI and stops cleanly when the user quits. rust/…/mod.rs• L493-559
References: rust/…/inference.rs• L129-233 rust/…/inference.rs• L670-925 rust/…/mod.rs• L493-559
Change checklist
When these files or sections are changed, remember to consider these:
- Provider adapter route or dialect changes must stay aligned with agent compatibility checks, payer paths, endpoint classification, and the generated inference spec.
- Hosted catalog changes must keep service URLs, model-list paths, fallback models, endpoint allowlists, model-family filters, and picker pricing aligned.
- OpenAI profile surface changes must stay aligned with provider registry protocol keys, paid endpoint lists, and response usage paths used for token pricing.
- Pricing format changes must update file parsing, inline parsing, model matching, generated variants, and picker display together.
- Inference spec changes must preserve the intended choice between per-request x402-upto pricing and batch settlement.
- Sandbox payment changes must keep localnet selection, RPC fallback rules, auto-funding, signer balance checks, and payout token-account preparation aligned.
- Account selection changes must keep explicit account overrides,
PAY_ACTIVE_ACCOUNT, network-linked accounts, and legacy signer fallback in the same order. - Startup validation changes must keep environment resolution, scheme defaults, payment-spec checks, and backend setup in the same boot-time contract.
- x402-upto changes must keep the operator signer, payout recipient behavior, response-usage meters, request ceiling, and missing-usage refund policy aligned.
- Batch-settlement changes must keep the signer, channel store durability, reconciliation owner, external worker handoff, payout state, and restart warning aligned.
- MPP session settlement changes must keep the settlement cadence, payment watermark, close timing, signer requirements, and session-store ownership aligned.
- Gateway host or subdomain changes must stay aligned with inference routing, /openapi.json lookup, and the payer proxy host header.
- Public registration changes must preserve the same identity rules for first registration, renewal, and conflict handling.
- Local catalog lifecycle changes must account for both stale-source cleanup and the well-known catalog route served by the gateway.
- Subscription startup changes must keep Plan derivation, challenge-binding secret checks, interactive publication, YAML write-back, and subscriber-visible plan fields aligned.
- Explicit plan tooling changes must keep PDA derivation and YAML write-back consistent with the plan identity that gateway startup checks and publishes.
- API startup changes must keep the public proxy, internal control plane, TLS scheme, allowed endpoint list, discovery URL rewriting, and upstream-auth filtering aligned.
- Demo startup changes must keep the persisted paywall secret and plan data stable while preserving the sandbox choice.
- TUI event changes must preserve the link between provider status, flow updates, connection aggregates, and the selected or filtered row.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| No local inference provider answers an identification probe | pay gate inference stops before serving | Start a supported server or use a valid custom registry entry |
| Another process uses the inference gateway bind address | Startup fails instead of leaving a hidden proxy failure | Stop the other gateway or pass a free --bind address |
| The paywall has an invalid route, scheme, split, or tier after resolution | API startup stops before it serves a request | Fix the YAML and its referenced environment values |
| Rates name a model that was not discovered | Priced inference does not start | Use a served model name or add a default rate |
Per-model pricing runs without --sandbox | Startup rejects the configuration because mainnet token charging is not wired | Run priced inference on localnet sandbox |
| A sandbox RPC override points to a cluster that cannot fund or serve the local payment setup | Startup fails before the gateway accepts traffic | Use hosted Surfpool or a prepared local validator and pass its RPC URL explicitly |
| A hosted catalog entry has no reachable service URL | The provider is skipped or does not appear as available | Fix the catalog service URL or wait for the published entry |
A hosted provider has model-specific variants but no matching model or default variant | The picker cannot show a valid price for that model | Use a model covered by the catalog or publish a default variant |
| A signer wallet has no SOL | Startup aborts because the gateway cannot use the payment configuration | Fund the signer or use the sandbox funding path |
| A stable-token payout account is missing on a real cluster | Startup aborts before paid traffic is served | Create the associated token account or use the localnet sandbox |
fee_payer: true or a live MPP session has no operator signer | Startup aborts instead of allowing paid requests that will fail later | Configure an operator signer or disable fee sponsorship where supported |
| A batch-settlement endpoint has no operator signer | The batch payment backend is not available for the configured route | Configure an operator signer and fee-payer path |
| Batch settlement uses in-memory channel state and the gateway restarts | Unclaimed voucher state is lost and payment recovery is unsafe | Configure a durable Redis channel store before taking real payments |
| Public registration has no usable signer | The gateway cannot publish its provider identity | Configure an account or use --no-register |
| A subscription endpoint has no challenge-binding secret | Gateway startup aborts before it serves subscription traffic | Set operator.challenge_binding_secret and keep it stable across restarts |
| A subscription Plan is absent and startup has no signer or the operator declines publication | Subscription gateway startup cannot complete | Configure an operator signer and confirm publication, or publish the Plan before startup |
pay plans publish --write records a derived Plan before the account exists on chain | The YAML looks configured but the subscription challenge still cannot work | Publish the account on chain first; use explicit plan tooling for derivation and write-back only |
| The TLS certificate and key are not both present or readable | Gateway startup fails before it binds the public listener | Supply a readable certificate and key as a pair |
| A chosen provider has no route for the agent's wire dialect | Provider selection or launch fails | Choose a compatible provider or use a supported agent route |
| Claude sends content the translator cannot represent | Some request or response content is dropped or simplified | Use a native Anthropic route when those content types matter |
| pay-demo.yaml is removed or replaced between demo runs | The demo can lose its stable subscription secret or plan data | Keep the generated file in place while testing subscription flows |
Key files
| File | Why to read it |
|---|---|
| rust/…/main.rs | CLI-wide command dispatch, first-run setup, network mode, and account context |
| rust/…/mod.rs | User-visible command tree and the boundary between server commands and payment requests |
| rust/…/mod.rs | Current gate command names, compatibility alias, plan dispatch, and account-aware signer selection |
| rust/…/scaffold.rs | Starter paywall workflow |
| rust/…/demo.rs | Bundled demo paywall, persisted demo state, sandbox choice, debugger, and telemetry startup |
| rust/…/start.rs | API gateway startup, account and sandbox selection, subscription readiness and publication, payment backends, batch settlement, discovery documents, local catalog route, and public proxy setup |
| rust/…/payments.rs | Shared sandbox wallet, balance checks, payout preparation, and payment backends used by API and priced inference gateways |
| rust/…/plans.rs | Subscription Plan derivation, preview, and explicit YAML write-back workflow |
| rust/…/provider_registration.rs | Public provider identity, renewal, and registry connection rules |
| rust/…/mod.rs | Inference gateway lifecycle, provider probing, pricing mode, discovery documents, registration, and TUI handoff |
| rust/…/discovery.rs | Local provider probing and user registry merge behavior |
| rust/…/mod.rs | Common provider contract, dialects, endpoint kinds, and built-in order |
| rust/…/catalog.rs | Hosted catalog providers, model discovery, compatibility routes, and catalog pricing |
| rust/…/pricing.rs | Shared pricing model used by rates files and inline inference prices |
| rust/…/spec.rs | Conversion from discovered providers and rates to gateway payment behavior, including the explicit non-batch inference spec |
| rust/…/mod.rs | Shared provider selection, compatibility checks, and payer routes for agent launchers |
| rust/…/translate.rs | Anthropic and OpenAI-compatible message and stream boundary |
| rust/…/provider_picker.rs | Interactive provider and model selection |
| rust/…/inference.rs | Live inference server and connection monitor |