Chat completions

OpenAI-compatible chat completions on Pendra, with streaming, tools, thinking and structured outputs.

POST /api/v1/chat/completions
New to chat? Start with the Chat guide, which has worked examples. This page lists every field.
Body application/json
model string Required
A model id, such as qwen3.6:27b. Leave off the quant to use whichever one you have installed. See Naming a model.
messages array Required
The conversation, as { role, content } objects with role system, user, assistant or tool. A user message's content can be a string or a list of text, image_url (vision) and file parts (a text or CSV file up to 1 MB). See also System messages and thinking in longer conversations.
stream boolean default: false
Send the reply as it's written. See Streaming.
max_tokens integer default: 32,768
The most tokens to write, thinking included. max_completion_tokens is accepted too, and wins if you send both.
temperature number default: the model's recommended value, else 1.0
How random the reply is, from 0 to 2. Use 0 for the most focused, repeatable answers.
tools array
Functions the model can call, with tool_choice and parallel_tool_calls. See Tool calling.
response_format object
{ "type": "json_object" } or a json_schema for JSON replies. See Structured outputs.
reasoning_effort string
How hard a reasoning model thinks: "low", "medium", "high", "xhigh" or "max", or "none" to turn thinking off. A level the model lacks rounds up. See Thinking.
reasoning object
{ "max_tokens": 4096 } caps thinking at that many tokens, then the model moves on to its answer. 0 turns thinking off. The default is 8,192 on models that support it. { "effort": "low" } is the same as reasoning_effort. See Thinking budget.
enable_thinking boolean
false makes a reasoning model answer straight away, true makes it think. Most reasoning models think by default. See Thinking.
seed integer
Makes sampling repeatable when temperature is above 0.

Other fields

FieldTypeWhat it does
top_pnumberNucleus sampling, from 0 to 1. 1 is off.
top_kintegerKeep only the K most likely tokens. 0 is off.
min_pnumberDrop tokens less likely than min_p times the top token.
stopstring or arrayStop when the model writes one of these.
frequency_penaltynumberPenalise tokens by how often they've appeared, from -2 to 2.
presence_penaltynumberPenalise tokens that have appeared at all, from -2 to 2.
repeat_penaltynumberClassic repetition penalty. 1.0 is off, 1.1 to 1.3 discourages repeats.
repeat_last_nintegerHow many recent tokens repeat_penalty looks at. Default 64.
logit_biasobjectMake tokens more or less likely, keyed by token id, from -100 to 100.
logprobs, top_logprobsboolean, integerReturn how likely each token of the answer was, and up to 20 alternatives per token. See Token probabilities.
xtc_probabilitynumberHow often the most likely tokens are set aside for a less obvious one, from 0 to 1. Helps creative writing. 0 is off.
xtc_thresholdnumberHow likely a token must be to count as a top choice for XTC, from 0 to 0.5. Default 0.1.
mirostatinteger1 or 2 turns on Mirostat, which keeps the text at a steady level of surprise instead of using top_p, top_k and min_p. 0 is off.
mirostat_taunumberMirostat's target. Lower is more focused, higher more varied. Default 5.0.
mirostat_etanumberHow fast Mirostat corrects towards its target. Default 0.1.
  • Any sampling field you leave out uses the model authors' recommended value, listed as sampling_defaults in the models list.
  • Any other OpenAI field is passed to the model as it is.
  • Even at temperature: 0, a reply can differ by the odd token between runs, because of tiny differences in GPU maths. For extraction where missing an item is costly, run it twice and combine the results.

Response

A chat.completion object, as in the example.

  • finish_reason is "stop" when the model finished, or "length" when it hit a limit.
  • A reasoning model's thinking is in message.reasoning_content (also message.reasoning), separate from the answer in message.content.
  • usage.prompt_tokens_details.cached_tokens counts the prompt tokens reused from earlier turns. More reuse means a faster first token. See KV cache.
  • The pendra field adds which worker served it, timing and any notice. OpenAI clients ignore it.

Streaming

With stream: true, the reply arrives as server-sent events in OpenAI's format: data: { ... } lines, each a chat.completion.chunk, ending with data: [DONE].

curl https://api.pendra.ai/api/v1/chat/completions \
  -H "Authorization: Bearer pdr_sk_..." \
  -N \
  -d '{
    "model": "qwen3.6:27b",
    "stream": true,
    "messages": [{"role": "user", "content": "Hello"}]
  }'
  • The last chunks carry usage and pendra.timing with an empty choices array, so don't read choices[0] without checking.
  • Tool call arguments arrive in pieces on delta.tool_calls. See Stream tool calls.
  • While a long prompt is read, Pendra sends a : keep-alive comment every 15 seconds. Clients ignore it. If you parse the stream yourself, skip lines starting with :.
data: {"id":"chatcmpl-9f2b","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}

data: {"id":"chatcmpl-9f2b","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":8,"completion_tokens":2,"total_tokens":10}}

data: {"id":"chatcmpl-9f2b","object":"chat.completion.chunk","choices":[],"pendra":{"timing":{"ttft_ms":115,"tokens_per_second":58.34}}}

data: [DONE]

Errors on a stream

Anything Pendra knows before the reply starts, such as an unknown model, no worker online, or every worker busy, comes back as a normal HTTP error before any stream data. When every worker is busy, Pendra waits up to about 30 seconds for one first, so give your client a timeout of at least 90 seconds for the headers.

If the request fails after the stream has started, for example if a worker goes offline, the last chunk has finish_reason: "error" and an error object, followed by data: [DONE]. code is the HTTP status it would have had. The Pendra and OpenAI SDKs raise this as an error.

data: {"object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"error"}],"error":{"message":"the prompt is 8800 tokens, which exceeds the context window of 1024 tokens on this worker","type":"worker_error","code":400}}

Token probabilities

Set logprobs: true to get how likely the model thought each token of its answer was. Add top_logprobs (0 to 20) to also get the most likely alternatives at each position. It's handy for classifying with a confidence score, or for spotting where an answer was a guess. The example turns thinking off so its one token is the answer.

curl https://api.pendra.ai/api/v1/chat/completions \
  -H "Authorization: Bearer pdr_sk_..." \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen3.6:27b",
    "messages": [
      {
        "role": "user",
        "content": "Is this review positive? Answer yes or no. Review: Great battery, fast delivery."
      }
    ],
    "enable_thinking": false,
    "max_tokens": 1,
    "logprobs": true,
    "top_logprobs": 2
  }'
"logprobs": {
  "content": [
    {
      "token": "Yes",
      "logprob": -0.0021,
      "bytes": [89, 101, 115],
      "top_logprobs": [
        { "token": "Yes", "logprob": -0.0021, "bytes": [89, 101, 115] },
        { "token": "No", "logprob": -6.18, "bytes": [78, 111] }
      ]
    }
  ],
  "refusal": null
}
  • A logprob is a natural log, so exp(logprob) is the probability. -0.0021 is 99.8%.
  • Only the answer is covered. Thinking and tool calls have no entries, so a reply that calls a tool has an empty content list.
  • The probabilities are the model's own, before temperature and the other sampling settings. When streaming, each chunk carries the entries for its own text.
  • It needs an up-to-date worker. If no worker serving the model has updated yet, the request fails with a 400 whose code is logprobs_unsupported, rather than answering without them. Pendra can't check an end-to-end encrypted request this way, so keep those workers up to date.

Notices

pendra.notice explains a reply that might otherwise look wrong. It has a code to check in your code and a message that says what to do. A reply has at most one.

CodeWhat happenedWhat to do
truncated_during_reasoningA reasoning model used all of max_tokens thinking, so there's no answer.Raise max_tokens, or turn thinking off.
reasoning_budget_reachedThinking hit its budget and was ended there. The answer is complete.Raise the budget if you want more thinking.
truncated_at_reasoning_budgetThinking hit its budget on a model that can't move on to its answer, so there's no answer.Raise the budget, or turn thinking off.
truncated_at_default_max_tokensYou set no max_tokens and the reply reached the default limit.Set max_tokens.
truncated_during_structured_outputThe reply stopped before its JSON was complete.Raise max_tokens, or shorten the prompt.
context_window_exhaustedThe prompt and reply filled the model's context window.Raise the context on the worker, or shorten the prompt.
truncated_memory_fullThe worker's memory was full of other requests, so it ended this one early.Retry, or run fewer requests at once on the worker.
structured_output_not_jsonYou asked for JSON and the reply isn't JSON.The message says whether to retry or simplify the schema.
strict_schema_not_enforcedPart of your strict schema couldn't be enforced.Check the reply against your schema before using it.

For more, see Thinking budget, Structured outputs, Context and Requests at once. On a stream, the notice comes on a chunk just before the last one.

If a long conversation's oldest images were left out to fit the worker's memory, pendra.images_omitted says how many. See Lots of images.

Timing

pendra.timing reports each reply's speed. A normal reply has it next to choices, and a stream has it on the last chunk. Each field is there only when it was measured.

FieldMeaning
ttft_msTime to first token, in ms. Queue time is reported separately. With web tools on, it includes any searches before the first token.
tokens_per_secondHow fast the reply was written.
queue_wait_msHow long the request waited before it started.
worker_queue_wait_msHow much of that wait was on the worker that served it. The rest was waiting for any worker to be free.
model_load_msTime spent loading the model, on a cold start only.
cold_starttrue when the model had to be loaded for this request.
draft_acceptance_rateWith speculative decoding, the share of draft tokens the model kept, from 0 to 1.

A long worker_queue_wait_ms means that worker is busy, so lower its requests at once. A long wait for any worker means you need more workers.

Which worker served the request

pendra.worker names the worker that ran the request, so you can check where it ran or trace a slow reply. A normal reply has it next to choices, and a stream on the last chunk.

"pendra": {
  "worker": {"id": "wrk-1a2b3c4d", "name": "london-gpu-01", "version": "3.115.1"},
  "request_id": "3f1c8a2e-5b7d-4e19-9c0a-6d2f8b1e4a73"
}
FieldMeaning
worker.idThe worker's id. Send it as X-Pendra-Worker-Id to use the same worker again.
worker.nameThe worker's name in the console, or null.
worker.versionThe worker release that served the request, or null. Log it, because the console only shows the current version.
request_idThe request's id. Quote it to support.

If a worker fails the request, the error names it as error.worker_id.

Web tool steps

When a worker has web tools on, the model can search the web and read pages while it answers. Each step is listed in pendra.web_tool_steps, or sent one at a time as pendra.web_tool_step on a stream. Each has a name (web_fetch or web_search), arguments, ok, a short result, result_chars, and error if it failed.

"pendra": {
  "web_tool_steps": [
    { "index": 0, "name": "web_fetch", "arguments": "{\"url\":\"https://example.com\"}",
      "ok": true, "result": "Example Domain...", "result_chars": 129 }
  ]
}

Response headers

HeaderMeaning
X-Request-IdThe request's id. Quote it to support.
X-Worker-IdThe worker the request went to.
X-Worker-NameThat worker's name. A name with accents or other non-ASCII characters is URL-encoded after a UTF-8'' prefix.
Server-Timingttft, tps, queue and worker_queue, the same figures as pendra.timing. Non-streaming replies only.
Server-Timing: ttft;dur=115, tps;dur=0;desc="58.3", queue;dur=16600, worker_queue;dur=14100

Headers are sent before the reply is written, so on a slow request they can be missing or name the first worker tried. The pendra field in the body is always right, so read it from there.

Timeouts

A request can run for up to about 30 minutes. After about 80 seconds, Pendra keeps the connection open for you, so you don't need to stream just to avoid a timeout. If such a request fails, the error arrives in the body. See Errors on a slow request.

Using an OpenAI client

Set the base URL to https://api.pendra.ai/v1 and the API key to your Pendra key. Nothing else changes. models.list() works too, but returns only model ids. See Models.