DIM · Dagon Inference Marketplace

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. If url already ends in /v1, only /chat/completions is appended; if it already ends in the full path, nothing is.
  • Model pinning. The upstream always receives backend.model, whatever model the 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 is failed and 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 of completion_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 as Authorization: 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) and user. Text, system messages, base64 images and function tools translate in both response modes. Anything else is rejected as invalid_request (status not_served, charge 0) instead of being silently reinterpreted.
  • Usage mapping. Input is input_tokens + cache_read_input_tokens + cache_creation_input_tokens; cached input is cache_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 own data: [DONE]: the gateway writes event: im_receipt and then [DONE] itself. A frame carrying a data: [DONE] line ends the request as failed, unbillable, so no adapter can put [DONE] before the receipt.
  • Token marking. Set token: true on frames that carry visible content, reasoning, a refusal or a tool call. The first such frame stops the ttft_max_ms clock; 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 is not_served at zero.
  • Usage at the end. Attach usage to frames as it becomes known. A stream that reaches done without a token or without usage is failed.
  • First byte for buffered calls. In completion, call onFirstByte on the first non-empty upstream body chunk. That moment is the receipt's ttft_ms for non-streaming requests (A26), and ttft_max_ms is enforced against it.
  • Upstream errors. A 5xx is failed. Any other non-2xx is not_served (backend_rejected); an upstream 429 passes its numeric Retry-After through 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.