# Chat (LLM)

```http
POST https://api.unitool.ai/v1/chat/completions
```

Request and answer follow OpenAI Chat Completions. The differences are listed below.

## Parameters

| Parameter | How it works |
|---|---|
| `model` | An id from the [catalog](/developers/models). Required |
| `messages` | Roles `system`, `developer`, `user`, `assistant`, `tool`. Content parts: `text`, `image_url`, `file` |
| `max_tokens`, `max_completion_tokens` | Output limit. Omitted: the model's limit is used and the request holds more balance while it runs, see [Billing](/developers/billing) |
| `stream`, `stream_options.include_usage` | SSE streaming |
| `temperature`, `top_p` | Only for models with `capabilities.temperature = true`; others answer `400 unsupported_parameter` |
| `stop`, `seed`, `frequency_penalty`, `presence_penalty` | Passed to the model |
| `response_format` | `json_object` and `json_schema` |
| `tools`, `tool_choice` | `type: "function"` tools only |
| `reasoning`, `reasoning_effort` | A level from the model's `capabilities.reasoning_efforts`; any other level is a 400 |
| `prompt_cache_key` | Passed to the model |
| `n` | `1` only |

Accepted but not used: `user`, `metadata`, `store`, `logprobs`, `top_logprobs`, `service_tier`, `parallel_tool_calls`, `cache_control`. They are listed in the `x-unitool-ignored-params` response header.

`plugins` and `web_search_options` turn on [web search](#web-search). Refused with `400 unsupported_parameter` and the name in `param`: `models`, `provider`, `route`, `sandbox`, tools other than `function`, and any unknown field.

## Attachments

- Images: an `image_url` part with an https link or a `data:` URL. Only for models with `capabilities.vision`.
- PDF and text files: a `file` part with `file.file_data`, a base64 `data:` URL or an https link. Only for models with `capabilities.files`.
- We download a file by link ourselves before the run. At most 4 file links per request, and together with the rest of the request they must fit into 10 MB, otherwise `400 file_too_large`. The link must be a public https address.
- The request body is limited to 10 MB; larger bodies get `413 request_too_large`.
- Requests with files run one at a time per account: a second one at the same time gets `429 concurrency_limit_exceeded`.

## Answer

```json
{
  "id": "chatcmpl-01JE...",
  "object": "chat.completion",
  "created": 1764331200,
  "model": "claude-sonnet-5-5",
  "choices": [
    { "index": 0, "message": { "role": "assistant", "content": "Hi!" }, "finish_reason": "stop" }
  ],
  "usage": {
    "prompt_tokens": 12,
    "completion_tokens": 4,
    "total_tokens": 16,
    "prompt_tokens_details": { "cached_tokens": 0, "cache_write_5m_tokens": 0, "cache_write_1h_tokens": 0 },
    "completion_tokens_details": { "reasoning_tokens": 0 },
    "cost_tokens": 0.0096,
    "estimated": false
  }
}
```

- `usage.cost_tokens`: how many balance tokens the request was charged. These are not model tokens.
- `usage.estimated: true`: the model did not report its exact usage (usually after a disconnect), so we counted it from the generated text.
- `finish_reason`: `stop`, `length`, `tool_calls` or `content_filter`.
- Reasoning comes in `message.reasoning` and `reasoning_details`. When you send the model's answer back in a tool loop, return `reasoning_details` unchanged.
- The `x-unitool-cost-tokens` header repeats `cost_tokens` (non-streamed answers only).

## Streaming

With `stream: true` you get `data: {...}` events of `chat.completion.chunk`, then `data: [DONE]`.

```text
data: {"id":"chatcmpl-01JE...","object":"chat.completion.chunk","created":1764331200,"model":"claude-sonnet-5-5","choices":[{"index":0,"delta":{"role":"assistant","content":"H"},"finish_reason":null}]}

data: {"id":"chatcmpl-01JE...","object":"chat.completion.chunk","created":1764331200,"model":"claude-sonnet-5-5","choices":[{"index":0,"delta":{"content":"i!"},"finish_reason":null}]}

data: {"id":"chatcmpl-01JE...","object":"chat.completion.chunk","created":1764331200,"model":"claude-sonnet-5-5","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":12,"completion_tokens":4,"total_tokens":16,"cost_tokens":0.0096,"estimated":false}}

data: [DONE]
```

- `usage` always comes. With `stream_options.include_usage: true` it is a separate last chunk with empty `choices`, as with OpenAI; without it, it rides on the chunk with `finish_reason`.
- While the model thinks, a `: keepalive` comment arrives every 15 seconds. SDKs skip it.
- A refusal before the generation starts (400, 402, 429, unavailable model) is a regular HTTP error response. Anything after the start comes with status 200 as a `data: {"error": {...}}` event without `[DONE]`; `error.details.charged_tokens` says what was charged for the text already sent.
- Closing the connection stops the generation at once; you pay only for what the model produced. Details in [Billing](/developers/billing#disconnects).

## Tools

Tools are declared as with OpenAI; calls come in `tool_calls` with `finish_reason: "tool_calls"`. Send the result as a `tool` role message with `tool_call_id`.

```python
tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Weather in a city",
        "parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]},
    },
}]
reply = client.chat.completions.create(model="claude-sonnet-5-5", max_tokens=1024, tools=tools,
    messages=[{"role": "user", "content": "What is the weather in Kazan?"}])
call = reply.choices[0].message.tool_calls[0]
```

## Web search

Models with `capabilities.web_search: true` can search the web. Turn it on per request:

| Path | How |
|---|---|
| `/v1/chat/completions` | `plugins: [{"id": "web"}]` or `web_search_options: {}` |
| `/v1/messages` | The `web_search_20250305` tool |
| `/v1/responses` | `tools: [{"type": "web_search"}]` |

`max_uses` (1 to 5, default 5) limits the searches of one model call: `plugins: [{"id": "web", "max_uses": 2}]` or `web_search_options: {"max_uses": 2}`. On `/v1/responses`, `max_tool_calls` does the same. A lower limit means a smaller hold on the balance.

**Price.** Each search is charged on top of the tokens, at the same API price rules and with your discount. The request reserves `max_uses` searches and the text they may add to the input; what was not used is released when the request ends. `usage.server_tool_use.web_search_requests` is the number of searches, `usage.cost_tokens` includes them, and the receipt (`GET /v1/requests/{id}`) shows `usage.server_tool_use.web_search_cost_tokens`. A request that failed before any output costs nothing; if it had already searched, the receipt lists `web_search_requests` with `web_search_cost_tokens: 0`.

**If the reserve does not fit the balance.** On `/v1/responses` the `web_search` tool is left out, the request runs without search and `tools.web_search` appears in `x-unitool-ignored-params`. On `/v1/chat/completions` you get `402` unless you mark search optional: `plugins: [{"id": "web", "optional": true}]` or `web_search_options: {"optional": true}`; then the request runs without search and the header names the parameter. On `/v1/messages`: `402`.

Sources are part of the answer text. Citation annotations, Anthropic's `web_search_tool_result` blocks, domain filters and user location are not supported.

Errors: `400 unsupported_parameter` when the model has no search or search is switched off; `400 invalid_parameter` for `max_uses` outside 1–5.

## Incomplete answers

If a non-streamed answer breaks on our side or the model's after the text has started, you get `200` with what was generated, `finish_reason: "length"` and the `x-unitool-incomplete` header: `upstream_error`, `deadline` (the request ran over 15 minutes) or `shutdown` (a service restart). Only the generated part is charged.

## Retries

Chat has no idempotency: a retry is a new paid request. Retry 429, 502, 503 and 504 after the `Retry-After` pause. Do not retry other 4xx without fixing the request.
