Backend adapters
The gateway does not generate tokens. Each route forwards admitted requests to one upstream through a backend adapter, which turns the upstream's response into frames for the buyer and into the four usage counts a receipt bills. Adapters never see payment headers and cannot change an admission or billing decision.
OpenAI-compatible ("kind": "openai")
Use it for vLLM, SGLang, TensorRT-LLM's OpenAI server, llama.cpp's server, the repository simulator, or any hosted OpenAI-compatible API.
{ "kind": "openai", "url": "http://127.0.0.1:8000", "model": "meta-llama/Llama-3.3-70B-Instruct" }
- Target.
POST <url>/v1/chat/completions. Ifurlalready ends in/v1, only/chat/completionsis appended; if it already ends in the full path, nothing is. - Model pinning. The upstream always receives
backend.model, whatevermodelthe buyer sent. When the buyer's bytes already name that model unambiguously, they are forwarded byte for byte; otherwise the gateway re-serializes the body with the model pinned. - Streaming usage. For streaming requests the gateway sets
stream_options.include_usage: true. A stream that ends without a final usage chunk isfailedand billed 0, so the upstream must support usage reporting in streams (vLLM and the simulator do). - Usage mapping.
prompt_tokens→ input;prompt_tokens_details.cached_tokens→ cached input (clamped to input);completion_tokens_details.reasoning_tokens→ hidden output; the rest ofcompletion_tokens→ visible output. Reasoning tokens larger than completion tokens make the response invalid. - Frames. Upstream SSE frames are forwarded unchanged, including their line endings. A single frame may be at most 1 MiB; a buffered completion at most 8 MiB.
- Authentication. Optional
apiKeyFile, sent asAuthorization: Bearer <key>.
Anthropic Messages ("kind": "anthropic")
Buyers still speak OpenAI chat completions to your endpoint; the adapter translates to POST <url>/v1/messages with anthropic-version: 2023-06-01 and, if configured, x-api-key.
{ "kind": "anthropic", "url": "https://api.anthropic.com", "model": "claude-model-name", "apiKeyFile": "keys/anthropic.txt" }
- Accepted request fields:
messages,model,stream,stream_options,max_tokens,max_completion_tokens,temperature,top_p,stop,tools,tool_choice,parallel_tool_calls,n(only 1) anduser. Text, system messages, base64 images and function tools translate in both response modes. Anything else is rejected asinvalid_request(statusnot_served, charge 0) instead of being silently reinterpreted. - Usage mapping. Input is
input_tokens + cache_read_input_tokens + cache_creation_input_tokens; cached input iscache_read_input_tokens; all output is visible (output_tokens); hidden output is 0. - Default length. If the buyer sends no
max_tokens, the adapter asks for 1,024.
Replace claude-model-name with the exact upstream model ID you resell, and describe it truthfully in the route's model identity.
Writing your own adapter
Programmatic gateways (startGateway from services/gateway/src/index.ts) take any object implementing BackendAdapter:
interface BackendAdapter {
dispatch(body: ChatBody, signal: AbortSignal, rawJson?: string): Promise<Response>;
/** Invoke onFirstByte synchronously on the first nonempty upstream body chunk. */
completion(response: Response, signal: AbortSignal, onFirstByte: () => void): Promise<BackendCompletion>;
/** Must not yield a frame containing an SSE `data: [DONE]` line; signal the end with `{ kind: "done" }`. */
stream(response: Response, signal: AbortSignal): AsyncIterable<BackendEvent>;
}
Rules the gateway enforces, whatever the adapter does:
- One terminator. End a stream with
{ kind: "done" }. Never forward the provider's owndata: [DONE]: the gateway writesevent: im_receiptand then[DONE]itself. A frame carrying adata: [DONE]line ends the request asfailed, unbillable, so no adapter can put[DONE]before the receipt. - Token marking. Set
token: trueon frames that carry visible content, reasoning, a refusal or a tool call. The first such frame stops thettft_max_msclock; a role-only frame does not. - Forwarding decides cancellation. Once any frame has been written to the buyer, a client disconnect ends the request as
cancelled(input plus fee). Before that, it isnot_servedat zero. - Usage at the end. Attach
usageto frames as it becomes known. A stream that reachesdonewithout a token or without usage isfailed. - First byte for buffered calls. In
completion, callonFirstByteon the first non-empty upstream body chunk. That moment is the receipt'sttft_msfor non-streaming requests (A26), andttft_max_msis enforced against it. - Upstream errors. A 5xx is
failed. Any other non-2xx isnot_served(backend_rejected); an upstream 429 passes its numericRetry-Afterthrough when it is at most 60 seconds. - No content in logs. Adapters must not log prompts or outputs; the gateway never does.
The Anthropic adapter is the worked example: it adds a translation file and touches no routing, admission, voucher, receipt or settlement code.