# OpenAI-compatible API

Source: https://docs.surogate.ai/engine/inference/api/

Point any OpenAI-compatible client at `surogate serve` by setting its base URL. The server
also speaks the Anthropic Messages API. This page covers what each endpoint supports and
where its limits are. The examples assume the server was started with `--served-model-name qwen3.6-27b`.

```python
from openai import OpenAI

client = OpenAI(base_url="http://127.0.0.1:8080/v1", api_key="not-needed")
print(client.chat.completions.create(
    model="qwen3.6-27b",
    messages=[{"role": "user", "content": "Suggest three easy vegetarian dinners."}],
).choices[0].message.content)
```

`--api-key KEY` enables authentication with either `Authorization: Bearer KEY` or `x-api-key:
KEY`; without it authentication is disabled. `/health` and CORS preflight requests are exempt.
`--api-key-file PATH` reads the key from a file instead: one line of visible ASCII,
with surrounding whitespace ignored. This keeps the key out of the process's command line, which every local user
can read in `/proc`. The server refuses `--api-key` and `--api-key-file` together, and warns when
other users can read the file.
`--cors` enables browser cross-origin requests. `--served-model-name ID` overrides the model id
the server reports, so a client with a hard-coded model string keeps working.

## OpenRouter provider catalog

`--openrouter-models-file PATH` enables `GET /openrouter/v1/models`, backed by a JSON
document supplied by the operator. It uses the same API-key authentication as inference.
The file is read once at startup; unreadable, oversized or malformed catalogs fail startup,
as do entries naming models the server does not serve. Validate the entire document against
the [OpenRouter provider schema](https://openrouter.ai/docs/assets/provider-monitor-schema-v2.openapi.json)
before installing it. The engine checks only the envelope and the required modality fields,
not the complete external JSON schema.

For example, the catalog for an unpublished text decisions model can start like this:

```json
{"data": [{
  "schema_version": "2.4",
  "id": "my-model",
  "name": "My decision model",
  "input_modalities": [{"type": "text"}],
  "output_modalities": [{"type": "decisions", "supported_parameters": {}}],
  "is_ready": false
}]}
```

The matching provider base path is `/openrouter`: `POST /openrouter/v1/decisions`
and `/openrouter/api/alpha/decisions` use the ordinary Decisions handler, authentication,
admission limits and usage accounting. These routes are enabled only with the catalog.
`GET /v1/models` keeps its OpenAI response shape for existing clients. The System One
compatibility aliases `/v1/systemone` and `/api/v1/systemone` also use the Decisions handler;
they require an actual served model ID and do not rewrite another provider's model names.

Fill in real prices, capacity and policy declarations before launch, because the server cannot
infer them. `is_ready: false` stages the listing; it does not stage the inference route. Even
when the server supports images and thinking natively, OpenRouter may not forward those
extensions, so test their adapter before advertising those capabilities through the router. See the
[provider guide](https://openrouter.ai/docs/guides/community/for-providers) for onboarding.

## Endpoints

| Method | Path | Purpose |
|---|---|---|
| `POST` | `/v1/chat/completions` | Chat Completions, streaming or not |
| `POST` | `/v1/chat/completions/tokens` | Chat completions with an optional `tokens` array for exact prompt ids |
| `POST` | `/v1/completions` | Raw text completion without a chat template |
| `POST` | `/v1/decisions` (stable v1; aliases `/api/alpha/decisions`, `/api/v1/decisions`) | Decisions API: several single-token questions over one shared state, with opt-in [thinking](/engine/inference/decisions/#thinking) for unsure questions and opt-in [option-order averaging](/engine/inference/decisions/#option-order-averaging); see [decisions.md](/engine/inference/decisions/) |
| `POST` | `/tokenize` | Render and tokenize `messages` or a raw `prompt` without generating |
| `POST` | `/v1/responses` | Responses API |
| `POST` | `/v1/responses/input_tokens` | Count input tokens without generating |
| `POST` | `/v1/responses/compact` | Returns `400 compaction_not_supported` |
| `GET` | `/v1/models`, `/v1/models/{id}` | Model listing; each entry also carries the served context in tokens as `max_model_len` and `context_length` |
| `POST` | `/v1/messages` | **Anthropic** Messages API |
| `POST` | `/v1/messages/count_tokens` | **Anthropic** token counting |
| `POST` | `/sleep` | Free a model's GPU memory while saving it in system RAM; requires `--enable-sleep-mode` |
| `POST` | `/wake_up` | Restore a sleeping model |
| `GET` | `/is_sleeping` | Sleep state (`?model=NAME`) |
| `POST` | `/v1/load_lora_adapter` | Load a PEFT adapter at runtime, or replace a loaded one, draining or in place (`--enable-lora`; `?model=NAME` targets a specific model on multi-model servers) |
| `POST` | `/v1/unload_lora_adapter` | Unload an adapter by name (`?model=NAME`) |
| `GET` | `/health` | Process health; also answers while a model sleeps. `503` once an inference engine has stopped (see below) |
| `GET` | `/kv_stats` | Cache memory use and active or waiting requests per model |
| `GET` | `/metrics` | Prometheus counters and gauges for throughput, requests, KV, weights, and sleep state |
| `POST` | `/v1/embeddings` | Embeddings — served by `surogate serve --embed`, a separate process |

On a server with several models, add `?model=NAME` to sleep, wake, sleep-state, and adapter
management requests to select the model.

If the GPU runs out of memory, the server does not stop. When it happens while a request is being
admitted, only that request fails. When it happens in the middle of a batch, every request being
generated at that moment fails. Failed requests get `429` with error code `server_overloaded`,
which clients may retry. Requests still waiting in the queue, and later ones, are served as
usual. An engine that fails in a way it cannot recover from makes `/health`
answer `503` with `"status": "unavailable"`. About two seconds later the process exits with
status 1, so a supervisor such as systemd can restart it and health checks stop routing to it.

## Chat Completions

### Supported request fields

| Field | Notes |
|---|---|
| `model` | Selects a served model or one of its named LoRA adapters |
| `messages` | Roles `system`, `user`, `assistant`, `tool` |
| `stream`, `stream_options.include_usage` | SSE; with `include_usage` a dedicated usage chunk precedes `[DONE]` |
| `max_completion_tokens`, `max_tokens` | `max_completion_tokens` wins; without one, `--default-max-tokens` capped to the rest of the context, or the whole rest when that flag is not given (vLLM on a checkpoint without a generation-config `max_new_tokens`). `/v1/completions` without `max_tokens` (or with `null`) asks for 16, as vLLM does. As in vLLM, a given value larger than the context, or one that does not fit next to the prompt, is HTTP 400 before generation (`--overlong-max-tokens`) |
| `thinking_token_budget` | Cumulative maximum reasoning tokens for a rendered chat with a single-token thought opener and closer. The model may finish early. |
| `temperature`, `top_p`, `top_k`, `min_p` | Request values override sampling defaults; `top_k: -1` or `0` disables the request's top-k limit |
| `repetition_penalty` | Multiplicative penalty; must be positive; `1` disables it |
| `presence_penalty`, `frequency_penalty` | Adjust repetition penalties for previously used tokens |
| `seed` | Sets the sampler seed; an omitted seed uses the server seed or a fresh per-request seed |
| `stop` | String or array of strings |
| `logit_bias` | Map token-id strings to biases in `[-100,100]`; applies to greedy and sampled generation |
| `tools`, `tool_choice` | Function tools only; `none`, `auto`, `required`, or a named function object. Automatic choice requires `--enable-auto-tool-choice` |
| `response_format` | `text`, `json_object`, or `json_schema`; see [Structured output](#structured-output) |
| `parallel_decoding` | Opt into independent boolean/enum classification on JSON-schema chat requests; see [Parallel constrained decoding](#parallel-constrained-decoding) |
| `parallel_tool_calls` | Defaults to `true`; `false` returns at most one tool call |
| `reasoning_effort` | `none`, `minimal`, `low`, `medium`, `high`, `xhigh`, or `max`; the loaded template must support the requested value |
| `chat_template_kwargs.enable_thinking` | Per-request thinking toggle; also accepted as top-level `enable_thinking` |
| `chat_template_kwargs.preserve_thinking` | Keep earlier assistant reasoning in later prompts; also accepted as top-level `preserve_thinking` |
| `ignore_eos` | Ignore the model's stop tokens; explicit stop strings and context limits still apply |
| `min_tokens` | Suppress stop tokens until this many tokens have been generated; stop strings remain active |
| `tokens` | Nonempty array of exact prompt token ids to use instead of the formatted messages; `messages` is still required |
| `add_generation_prompt` | Whether the template appends the assistant-generation prefix; defaults to `true` |
| `logprobs`, `top_logprobs`, `prompt_logprobs`, `return_token_ids` | Raw token details for text and tool-call Chat Completion responses, including SSE; see below |

If the server was started with `--greedy`, temperature is always zero even when a request
specifies another value.

Message content may be a plain string or an array of `text`, `image_url`, and `video_url` parts.
Images and video require `--vision` and a supported vision model; sources may be
local paths, HTTP(S) URLs, or base64 data URIs. `input_audio` is refused with
`modality_not_supported`.

For automatic tool choice, start the server with `--enable-auto-tool-choice` and a matching
`--tool-call-parser` (default `hermes`, and `qwen3_coder` for Qwen3.5 / Qwen3.6 checkpoints; `qwen3_xml`, `spark25`,
`muse_glimmer`, `llama3_json`, and `llama4_json` are also accepted). The model generates tool calls, and your application
executes them. With `qwen3_coder`, the parsed calls match what vLLM 0.31 returns for the same tokens,
including calls to names the request did not define. A request with tools also gets the same content as vLLM, `null` when empty.

Tool choice follows these rules for Chat Completions and Responses:

| Choice | Behavior |
|---|---|
| `none` | Tools are disabled, and their argument schemas are not enforced. |
| `auto`, with no tool marked `strict: true` | The model freely chooses an answer or tool calls. Argument schemas are not enforced. |
| `auto`, with at least one tool marked `strict: true` | Normal text remains allowed. When the model starts a tool call, its name and arguments are constrained. Tools explicitly marked `strict: false` remain exempt from argument-schema enforcement; tools with omitted `strict` use their schemas. |
| `required` | A completed response must contain a tool call. Argument schemas apply unless a tool explicitly sets `strict: false`. |
| Named function | A completed response calls the selected function once. Its argument schema applies unless it explicitly sets `strict: false`. |

As in vLLM, automatic choice with `parallel_tool_calls: false` returns only the first call if
multiple calls were generated. The setting does not change automatic generation; usage and
raw token details still cover the whole generated sequence.

Constrained tool arguments support nested objects and arrays, required properties,
additional-property rules, enums, constants, numeric bounds, array length limits, `anyOf`,
and local references. Declare required fields in `properties`. Their schema support is narrower
than JSON answer formats: string patterns, string length limits, formats, `multipleOf`,
`allOf`, `oneOf`, and assertion siblings alongside `$ref` or `anyOf` are currently rejected.
Unsupported tool constraints return HTTP `400` with `tool_schema_not_supported` instead of
being silently ignored. Schemas on disabled or unconstrained tools are not compiled.

Constrained tools require `ignore_eos: false`, `min_tokens: 0`, and no custom stop strings.
If generation reaches its output or context limit, the call may be incomplete; no executable
tool call is returned from that incomplete response. Validate unconstrained arguments in your
application before executing them.

When tool-derived constraints are active, they take precedence over `response_format`
(or Responses `text.format`), matching vLLM. With unconstrained automatic tools, a JSON answer
format still applies to the generated answer.

`logprobs: true` returns each generated token's log-probability, text, and bytes in
`choices[].logprobs.content`. Add `top_logprobs: 5` for the five most likely alternatives at
each position; counts from 0 through 20 are supported. The selected token is always scored,
even when it is outside those alternatives.

`prompt_logprobs: 5` returns a top-level `prompt_logprobs` array aligned with the prompt
tokens. Its first entry is `null`, because the first token has no preceding context. Each
remaining entry maps token ids to `logprob`, `rank`, and `decoded_token`, including the actual
prompt token and up to five alternatives. Use `prompt_logprobs: 0` to score only the actual
prompt tokens. `return_token_ids: true` includes the exact prompt and completion ids.

Probabilities use the full model distribution before temperature, penalties, logit bias, or
sampling constraints, matching vLLM's default raw log-probabilities. They are not probabilities
renormalized over the tokens allowed by a schema or sampling filter. Generated scores include
reasoning and tool-call syntax. Ordinary, MTP, and DFlash generation support these fields,
including pipeline serving.

Numerical rounding can cause small differences between scores returned during generation
and scores from a separate prompt-scoring request. Use the generated-token scores when
you need the probabilities from the original rollout.

Requesting and retaining scores adds work and uses system memory. Repeated or extended
prompts can reuse cached scores when the model, adapter, and prefix match and the cached entries contain enough alternatives for the
new request. Missing scores are computed as needed; an unscored prefix may require a fresh
prefill. Cached scores are discarded when their prefix is evicted or the adapter is replaced.

With `stream: true`, generated-token scores arrive incrementally in chunks with an empty
`delta`. Concatenate `choices[].logprobs.content` across those chunks. Prompt scores accompany
the first score chunk; requested token-id arrays are sent once before the finish chunk.

### Usage and cached tokens

Every response reports `usage`: `prompt_tokens`, `completion_tokens`, `total_tokens` and
`prompt_tokens_details.cached_tokens`. The last is the part of the prompt the prefix cache
supplied instead of prefilling, which is the part a cached-input price applies to. `prompt_tokens`
still counts the whole prompt, cached part included. `/v1/completions` reports the same
fields. When streaming with `stream_options.include_usage`, the usage chunk carries them too.
The number equals `prefix_cache_hit_tokens` in the request's `request_done` log record
(`--request-log-jsonl`), clamped to the prompt. The Responses API reports the same number as
`usage.input_tokens_details.cached_tokens`, and Anthropic Messages as
`usage.cache_read_input_tokens`.

### Reasoning models

With a matching reasoning parser, non-streaming responses put reasoning in
`message.reasoning_content` and the answer in `message.content`. Streaming responses use
`reasoning_content` and `content` deltas respectively.

Where the template has a thinking toggle, a request that does not set `enable_thinking` gets
the template's own default, as vLLM does: Qwen and Granite think, Gemma 4 answers directly.
`--thinking` or `--no-thinking` replaces that default; per-request `enable_thinking` overrides
both. Some models always reason and cannot
disable it. Unsupported per-request toggles or effort values are refused. `--preserve-thinking`
keeps earlier assistant reasoning in later prompts; the request's `preserve_thinking` overrides
that default. If you supply both a top-level setting and its `chat_template_kwargs` equivalent,
their values must agree.

`preserve_thinking` renders the reasoning a client sends back. A client that sends its assistant
turns back as content and tool calls only can be served with `--thinking-retention loop|all`
([CLI](/engine/inference/cli/#thinking)): the server keeps the reasoning it generated and puts it back into those
turns itself, per API key and model.

A server started with `--thinking-loop-guard` or `--thinking-answer-reserve N` ([CLI](/engine/inference/cli/#thinking-guard))
closes a thought that loops, or that would leave no room for the answer, instead of returning an empty answer.
- The reasoning then ends with the sentence "Considering the limited time by the user, I have to give the
  solution based on the thinking directly now."
- The answer follows in the same response, and `usage` and `finish_reason` describe the whole reply.
- Nothing changes for a request whose thought ends by itself.

Set `thinking_token_budget` to a nonnegative integer to cap the thought spans in one completion. The closing control token counts toward the cap. A later span draws on the same budget. When one reasoning token remains, the sampler allows the tokenizer's closing control and then continues sampling the answer. Zero requests a closed thinking prompt. `max_completion_tokens`, stop conditions, and context capacity still bound the whole reply. Allow enough output tokens for a visible answer.

The extension uses the parsed tokenizer controls, including during speculative verification. It requires a supported thought-span tokenizer, an enabled reasoning parser, and a chat rendered by the server. Parallel field decoding and token-ID inputs use other execution paths. Normal responses and streamed usage report the final accepted count in `completion_tokens_details.reasoning_tokens`. A per-request cap takes precedence over the server's loop-guard continuation for that request.

With a JSON schema or a tool grammar, the grammar governs the answer: only the thought the prompt opened counts as a thought, and the opener is barred after it. If the grammar cannot accept the closing control when the cap is reached, the grammar takes precedence and the thought continues until it can close, so such a request can exceed the cap; it never fails other requests. Models that open their own thought (Inkling-2) refuse the field with HTTP 400. A capped request is sampled under a token mask in every round, as a JSON schema is; on an engine that chains decode bursts, a batch that holds one runs one token per round.

### Not supported

These are refused with a `400` and a specific error code instead of being silently ignored:

| Field | Reason |
|---|---|
| `n` > 1 | One completion per request (`n_not_supported`) |
| `functions`, `function_call` | Legacy pre-`tools` API (`tools_not_supported`) |
| Message role `function` | Use role `tool` (`unsupported_role`) |
| Unsupported content parts, including `input_audio` | `modality_not_supported` |

Models without a chat template use `/v1/completions`; chat generation endpoints return
`chat_not_supported`.

### Structured output

Chat completions support `response_format: {"type":"json_object"}` for a JSON object, or
`json_schema` to specify its contents:

```json
{
  "model": "my-model",
  "messages": [{"role": "user", "content": "Is the task complete?"}],
  "response_format": {
    "type": "json_schema",
    "json_schema": {
      "name": "answer",
      "strict": true,
      "schema": {
        "type": "object",
        "properties": {"complete": {"type": "boolean"}},
        "required": ["complete"],
        "additionalProperties": false
      }
    }
  }
}
```

Supported schemas include nested objects and arrays, required properties, additional-property
rules, enums and constants, numeric bounds and `multipleOf`, array and string length limits,
string patterns, recognized formats, `anyOf`, and local `$ref` references. `allOf` intersections
and `oneOf` alternatives are supported when their constraints can be combined or proven
mutually exclusive.

JSON Schema support is partial. The following restrictions apply to both chat completions
`response_format` and Responses `text.format`, including streaming requests. Unsupported
constraints return HTTP `400` before generation instead of being silently ignored.

| Limitation | Meaning and suggested alternative |
|---|---|
| Overlapping `oneOf` alternatives | `oneOf` requires exactly one alternative to match. Alternatives that could both match are unsupported. Use distinct required `const` values to distinguish alternatives, or use `anyOf` only if matching more than one alternative is acceptable. |
| Some `allOf` intersections | Not every combination of constraints can be enforced together. Where possible, combine compatible requirements into a single schema. |
| Conditional schemas and general negation | `if`/`then`/`else` and general `not` constraints are unsupported. Express allowed values or supported alternatives directly, or validate these rules in your application. |
| `uniqueItems: true` | Array element uniqueness cannot be enforced during generation. Validate uniqueness in your application and retry if needed. |
| Remote `$ref` references | References outside the submitted schema are unsupported. Include the referenced schemas under `$defs` and use local references such as `#/$defs/address`. |

Removing an unsupported constraint also removes its generation guarantee. If you simplify a
schema to make a request acceptable, validate the result against your original requirements
before using it.

The constraint applies while generating, including streaming. An answer stopped by a token or
context limit can still be incomplete; check `finish_reason` before parsing it. Set
`max_tokens` high enough, keep `ignore_eos` false and `min_tokens` at zero, and omit custom `stop` strings. JSON is
returned as answer content.

With thinking on, a model whose template opens its reasoning in the prompt (Qwen3.5 and Qwen3.6
templates do) reasons first, and the schema constrains the answer that follows its reasoning, as
vLLM's structured outputs do under a reasoning parser. The reasoning itself is not constrained,
except that the reply cannot end before the reasoning is closed. It is returned separately
(`reasoning_content`, a Responses reasoning item) and counts toward `max_tokens`, so leave room for
both: a reply cut off while still reasoning has no JSON yet. Requests without thinking, a
server started with `--reasoning-parser none`, and models whose reasoning is not a span the prompt
opens are constrained from the first generated token, with no separate reasoning.

Structured requests work on single- and multi-GPU servers, including MTP and DFlash speculative
decoding. The speed benefit depends on how often the proposed tokens satisfy the requested
format and are accepted by the target model.

The Responses API supports the same constraints through `text.format`. Unlike chat completions,
place `name`, `strict`, and `schema` directly inside the format object:

```json
{
  "model": "my-model",
  "input": "Is the task complete?",
  "text": {
    "format": {
      "type": "json_schema",
      "name": "answer",
      "strict": true,
      "schema": {
        "type": "object",
        "properties": {"complete": {"type": "boolean"}},
        "required": ["complete"],
        "additionalProperties": false
      }
    }
  }
}
```

Use `"text": {"format": {"type": "json_object"}}` when any JSON object is sufficient.
Both formats work with streaming and non-streaming requests.

### Parallel constrained decoding

Set `parallel_decoding: true` on `/v1/chat/completions` to classify several independent fields
from the same conversation. Each field receives its highest-probability allowed value, and
`choices[0].message.content` contains the assembled JSON object. No separate checkpoint is
required. Use an instruction-tuned model suited to your classification task.

```json
{
  "model": "my-model",
  "messages": [{"role": "user", "content": "Our production server is down. Please help immediately."}],
  "parallel_decoding": true,
  "temperature": 1,
  "max_tokens": 128,
  "response_format": {
    "type": "json_schema",
    "json_schema": {
      "name": "triage",
      "strict": true,
      "schema": {
        "type": "object",
        "properties": {
          "urgent": {"type": "boolean", "description": "Does this need immediate attention?"},
          "category": {"type": "string", "enum": ["outage", "billing", "other"]}
        },
        "required": ["urgent", "category"],
        "additionalProperties": false
      }
    }
  }
}
```

With the OpenAI Python client, pass the opt-in through
`extra_body={"parallel_decoding": True}`. Start the server with `--max-num-seqs 4` or higher
to evaluate fields concurrently. The shared conversation is processed once; each field then
continues from it. Increasing the sequence limit allows more fields to run together, up to 64.
A server with one sequence slot also accepts these requests. Classification needs GPU cache
space for the shared conversation and the field continuations.
Speedups depend on the model, prompt length, number of fields, and available memory, so benchmark
your own workload.

The response also includes `parallel_decoding.fields`. Each field contains its selected
`value` and a `probabilities` array of `{ "value": ..., "probability": ... }` entries.
Probabilities sum to one over that field's allowed choices. They describe the model's
constrained choice distribution, not calibrated confidence or a guarantee of correctness.
Multi-token choices, including choices that share prefixes, are supported.

Fields are evaluated independently. Use ordinary structured output when one field should depend
on another. This mode accepts a flat object with 1–64 required properties, each a boolean or an
enum of up to 256 distinct scalar values. Set `additionalProperties: false`. Nested objects,
arrays, optional fields, references, and other schema constraints return HTTP `400`. Very long
choices or large combined schemas can exceed the request limit; shorten them or split the request.

Classification always chooses the highest-probability value. `temperature` follows the normal
request/CLI/model default precedence and controls the choice probabilities; zero uses unscaled
probabilities. Sampling filters and repetition penalties are not used. Explicit non-neutral
filters, penalties, or `logit_bias` return HTTP `400`. Custom stops, tool calls, media, explicit
token prompts, and token-level logprobs/IDs are unavailable in this mode. Give `max_tokens`
enough room for any allowed result; an insufficient budget returns HTTP `400`.

With `stream: true`, the complete JSON is delivered after classification finishes, followed by
field probabilities on the final chunk. Usage counts the shared prompt once plus field-query
suffixes, and counts the returned JSON as completion tokens. Ordinary JSON-schema requests
keep their existing behavior when the opt-in is absent or false.

### Streaming shape

The role chunk is sent immediately before the first output item, so its arrival tells you
generation is under way. Reasoning is emitted as `reasoning_content` deltas and answer text as
`content` deltas; parsed tool calls are emitted when generation finishes. A final chunk carries
`finish_reason` and an empty delta. With
`stream_options.include_usage`, content chunks carry `usage: null` and one usage-only chunk
(empty `choices`) is emitted before `data: [DONE]`.

```bash
curl -N http://127.0.0.1:8080/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -d '{"model":"qwen3.6-27b","stream":true,
       "stream_options":{"include_usage":true},
       "messages":[{"role":"user","content":"Count to five."}]}'
```

## Raw completions and tokenization

`POST /v1/completions` tokenizes `prompt` exactly as written, with no chat template. It accepts
a string or a one-element string array and supports streaming, sampling, stops, and adapter
selection. Set `logprobs` to an integer from 0 through 20 to receive token probabilities and
that many alternatives in `choices[].logprobs`. `prompt_logprobs` and `return_token_ids` work
as described above. Streaming delivers score chunks incrementally; concatenate their token
and probability arrays. Text offsets continue across chunks. Batched prompts and token arrays
in `prompt` are refused. Requests for `echo`, `suffix`, or `best_of > 1` are unsupported on
this endpoint.

`POST /tokenize` accepts either `messages` or a raw `prompt` and returns `count`,
`max_model_len`, and `tokens`. Set `with_token_strings: true` to include `token_strs`, or
`add_generation_prompt: false` to render a chat fragment without the assistant-generation
prefix. The ids use the loaded model's tokenizer and template.

`/v1/chat/completions/tokens` uses the same schema as `/v1/chat/completions`: include
`messages` and a nonempty `tokens` array. The explicit ids become the model's prompt, which
allows a client to extend an exact previous token sequence without re-rendering its history.

## Sleep mode

Start with `--enable-sleep-mode` to free a model's GPU memory without shutting down the server.
It is not available on the DGX Spark, whose GPU shares the system's memory.

```bash
curl -X POST -d '' http://localhost:8080/sleep
curl -X POST -d '' http://localhost:8080/wake_up
curl http://localhost:8080/is_sleeping
```

Manual sleep waits for active requests to finish, then saves the model and its cache in
system RAM. Waking restores them, including reusable prompts. Allow enough RAM for the model
and its cache. The first sleep on a single-model server can take longer while this memory is
prepared; servers with several models prepare it at startup.

While asleep, `/health` still answers normally. A single-model server returns `503` for
generation requests until you call `/wake_up`. A server with several models can make room
and wake the requested model automatically while the request waits. Send the empty body
(`-d ''`) shown above with sleep and wake requests to avoid a read-timeout delay.

With `--data-parallel`, sleep and wake apply to every replica of the model at once, and
`/is_sleeping` reports `true` only when all of them are asleep.

## Monitoring memory and requests

Use `/kv_stats` to check cache memory use and how many requests are running or waiting:

```bash
curl http://127.0.0.1:8080/kv_stats
```

The response has a `models` array with an entry for each served model; a model served with
`--data-parallel` has one entry per replica, each with a `replica` index (`/metrics` labels its
series the same way). Useful fields include:

| Field | Meaning |
|---|---|
| `model`, `sleeping` | Model name and sleep state |
| `running_requests`, `waiting_requests` | Requests generating or waiting to start |
| `kv_capacity_tokens` | Configured cache capacity in tokens |
| `weights_bytes` | Model weight size in bytes |
| `pool_bytes` | Full configured cache size |
| `in_use_bytes` | Cache space in use |
| `mapped_bytes` | Cache memory currently allocated on the GPU |

Cache memory grows with demand by default, so `mapped_bytes` can be smaller than `pool_bytes`.
It may remain above `in_use_bytes` because the server keeps some memory ready for reuse.
Use `/metrics` for Prometheus monitoring of requests, throughput, memory, and sleep state.

### Request ids and the request log

A caller, such as a gateway, can name each request with an `X-Request-Id` header. The server
echoes the header on the response, refusals included. It also writes the id as
`client_request_id` into every `request_start`, `request_done`, `request_rejected` and
`request_error` record of `--request-log-jsonl`. The protocols covered are Chat Completions,
Completions, Anthropic Messages, Responses and decisions. Records of requests sent without the
header have `client_request_id: null`.

Only ids of 1 to 128 visible ASCII characters are used. Any other value is ignored, neither
echoed nor logged, so a header cannot inject text into the log. The server warns about this once
on the console, without the value. Header values are percent-decoded, so an id must not contain
`%`; UUIDs and similar ids are unaffected. A request that carries more than one `X-Request-Id`
header gets no id at all. A gateway should therefore replace the client's header with its own,
not add a second one. With `--cors`, browsers may send the header and read the echoed one.

Every request ends in exactly one terminal record: `request_done`, `request_error` or
`request_rejected`. A request refused before generation ends in `request_rejected`. This includes
a request whose client left while it was being prepared. A request refused during preparation
has no `request_start`.

A streamed request whose client leaves after that still ends in `request_done`. This includes a
client that leaves before the first event, and a parallel-decoding stream. The record carries
the prompt tokens, the cached tokens (`prefix_cache_hit_tokens`) and the completion tokens
generated before generation stopped. A caller that never received the final usage chunk can
settle the request from that record. Two details:

- `finish_reason` is usually `"cancelled"`. It can also be the normal reason, for example
  `"stop"`, when generation finished before the server noticed the client had left.
- `prompt_tokens` and `computed_prefill_tokens` count the whole prompt, even when the client
  left before its prefill finished. A dropped parallel-decoding stream reports
  `completion_tokens: 0`.

## LoRA adapters at runtime

Start with `--enable-lora` to add and remove adapters without restarting:

```bash
curl -X POST http://localhost:8080/v1/load_lora_adapter \
  -H 'Content-Type: application/json' \
  -d '{"lora_name": "my-tune", "lora_path": "/path/to/peft/adapter"}'

curl -X POST http://localhost:8080/v1/unload_lora_adapter \
  -H 'Content-Type: application/json' \
  -d '{"lora_name": "my-tune"}'
```

A loaded adapter appears in `/v1/models` and is selected per request by naming
it in `model`. Loading an existing name replaces that adapter. Loading fails with an
explanation if the adapter is incompatible or no slot is available for a new name.
The default rank limit is 32; use
`--max-lora-rank` at startup for larger supported adapters.

By default, replacement and unloading wait for requests already using that adapter to finish.
Those requests keep their original policy for their entire response. New requests for the
same adapter wait during the update; requests for other adapters and the base model can
continue. After unloading, requests still waiting for that adapter receive an error. A
replacement can instead happen in place, without waiting; see below.

Waiting for active requests is bounded by `--pending-timeout-ms`. An update that times out returns HTTP 503
and leaves the current adapter unchanged. An incompatible replacement also leaves it
unchanged. Replacing an adapter works with `--max-loras 1`.

Requests for the base model cost the same as without `--enable-lora`: a round in which no
request selects an adapter runs the base model's own kernels, as a server started without the
flag does. A round that includes an adapter request runs every request in it on the
adapter-capable routes, base requests included. That is the same model through different
kernels, so a base request's scores in such a round can differ slightly from the ones it gets
alone, and the round is slower than a base round. `--batch-invariant` and `--devices` pipelines keep every round on
the adapter-capable routes.

A successful load replies in `text/plain`, not JSON: `Success: LoRA adapter 'my-tune' added
successfully (loaded: version 1)`. An incompatible adapter returns HTTP 400 with the reason as
text; a draining update that times out returns 503. A server that loads or replaces adapters
repeatedly needs `MALLOC_ARENA_MAX=2` in its environment, or it grows by about an adapter's size
in host memory with every load ([LoRA adapters](/engine/inference/cli/#lora-adapters)).

### Replacing an adapter in place

Draining suits occasional updates. An RL trainer that publishes a new policy every few minutes
needs it to change under running rollouts instead. Add `"in_place": true` to the load, or start
the server with `--lora-swap in-place` to make it the default for every load of a loaded name:

```bash
curl -X POST http://localhost:8080/v1/load_lora_adapter \
  -H 'Content-Type: application/json' \
  -d '{"lora_name": "policy", "lora_path": "/adapters/step-42", "in_place": true, "version": 42}'
```

- The call reads, validates and stages the adapter in page-locked host memory while the engine
  serves: about 16 s for a 2.5 GB rank-16 adapter on every expert of a 35B-A3B mixture (13 s
  reading and validating, 2.5 s staging). Requests that arrive meanwhile run at once, on the
  current weights.
- The engine then pauses at a round boundary to copy the new weights in and refresh its host
  copy of them: 87 ms with eight long generations running, 0.2–0.7 s with sixteen on a busy
  host, for that adapter. Requests in flight continue on the new weights from their next token;
  none is held, restarted or refused.
- `version`, a non-negative 32-bit integer, is the number the adapter's tokens report from now
  on (an RL trainer passes its step). Omitted, it is the previous version plus one, and 1 for a
  first load.
- The reply names the swap: `Success: LoRA adapter 'policy' added successfully (swapped in
  place: version 42, engine paused 212.3 ms, 2647.0 MiB staged in 2.51 s)`.
- An adapter that fails validation leaves the current one serving. In-place loads take plain
  LoRA: a module with a DoRA magnitude, a bias or a replacement weight is refused with its
  name; load such an adapter without `in_place`.
- No prefix computed under the earlier weights is reused after the swap. A conversation the
  server retains, a shared prefix or a host-tier entry of that adapter is prefilled again once
  under the new weights ([Host prefix cache](/engine/inference/cli/#host-prefix-cache)), so a prompt sent again
  after the swap is answered and scored under the new adapter only. Requests in flight continue
  on the new weights from their next token, as above.

### Which version sampled each token

A choice from an adapter request carries `lora_versions`: `[[first completion token, version],
...]`, one entry for each run of tokens sampled under the same version. A response that started
before an in-place swap and finished after it has two entries or more:

```json
"lora_versions": [[0, 41], [2973, 42]]
```

Here completion tokens 0–2972 were sampled with version 41 and the rest with version 42. The
versions describe the sampled tokens; the prompt's KV may come from an earlier version. A
draining replacement never splits a response. Chat Completions and `/v1/completions` report the
field on each choice of an adapter request; a stream sends it with the token-id arrays
(`return_token_ids: true`). An RL client should take each call's behavior policy from this field instead of
from the time it sent the request.

## Responses API

`POST /v1/responses` accepts `input` (string or typed items), `instructions`,
`metadata`, `tools`, `tool_choice`, and `stream`, and emits semantic SSE events when streaming.
Requests are stateless, so include the complete conversation history in `input`, including prior
assistant outputs and tool results. The server does not store conversations or provide
response retrieval, deletion, or input-item listing endpoints.

Omit `store` or set it to `false`. `store: true` and non-null `previous_response_id` return
HTTP `400`. Responses report `store: false`; their IDs cannot be used to continue a conversation.

Responses accepts `tool_choice: "auto"`, `"none"`, `"required"`, or a named function such as
`{"type":"function","name":"weather"}`. Define function tools with `name`, `parameters`, and
optional `strict` directly on the tool object. Both strict tools and `parallel_tool_calls: false`
work with streaming and non-streaming requests; the tool-choice rules above apply unchanged.

For generated-token probabilities, set `include: ["message.output_text.logprobs"]` and
optionally `top_logprobs` (0–20). Scores appear on the output-text content part and in the
`response.output_text.done` event when streaming. Incremental scores also arrive in
`response.output_text.delta` events, sometimes with an empty text delta; concatenate their
`logprobs` arrays. The done event contains the complete array. Scores cover the raw generated
tokens, including reasoning and tool syntax, using the same probabilities as Chat Completions.

Background execution and compaction are unsupported. `/v1/responses/compact` returns
`400 compaction_not_supported`. Disconnecting the client cancels its active generation request.

```bash
curl http://127.0.0.1:8080/v1/responses \
  -H 'Content-Type: application/json' \
  -d '{"model":"qwen3.6-27b","input":"Suggest three easy vegetarian dinners.","store":false}'
```

## Anthropic Messages

`POST /v1/messages` and `POST /v1/messages/count_tokens` support Anthropic-style requests,
including streaming and a top-level `system` prompt. Point your Anthropic client at
`http://127.0.0.1:8080`.

Usage follows Anthropic's convention:

- `input_tokens` excludes the prompt tokens the prefix cache supplied.
- `cache_read_input_tokens` reports those tokens. Add the two to get the whole prompt.
- `cache_creation_input_tokens` is always 0, because nothing is written to a cache on the
  client's behalf.

When streaming, `message_start` is sent once the prompt is prefilled, before the first content
block, so it already carries the cache split. The final `message_delta` repeats the cumulative
usage with the same split. A stream that fails before its prompt is prefilled, for example on a
queue timeout, sends only an `error` event and no `message_start`.

## Embeddings

Start a separate embeddings server with `surogate serve --embed`; see the
[GPU and CPU examples](/engine/inference/serving-models/#embedding-model-cpu-and-gpu). `POST /v1/embeddings`
accepts one string, an array of strings, one array of token ids, or an array of token-id arrays.

```bash
curl http://127.0.0.1:8413/v1/embeddings \
  -H 'Content-Type: application/json' \
  -d '{"model":"embeddinggemma-300m","input":["a first sentence","a second one"]}'
```

The response contains a `data` array with one item per input. Each item has an `index` and an
`embedding` vector. Vectors are normalized, so you can compare them with a dot product.

Next: [CLI and parameters](/engine/inference/cli/) lists every server flag this page mentions, and the
[Decisions API](/engine/inference/decisions/) page covers the decisions endpoint in full.
