# Chat completions

> POST /v1/chat/completions — the OpenAI Chat Completions format for OpenAI SDKs and tools.

Source: https://docs.strokes.ink/api/chat-completions

```http
POST https://api.strokes.ink/v1/chat/completions
```

The same models, prices and billing as [`/v1/messages`](/api/messages), in OpenAI's Chat Completions format. Use it when your code or framework already speaks OpenAI: point the OpenAI SDK at `https://api.strokes.ink/v1` with your Strokes key.

<Tabs items={["TypeScript","Python","curl"]}>
<Tab value="TypeScript">
```ts
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.STROKES_API_KEY,
  baseURL: "https://api.strokes.ink/v1",
});

const completion = await client.chat.completions.create({
  model: "swim-1.0",
  messages: [
    { role: "system", content: "You are a careful editor." },
    {
      role: "user",
      content: "Tighten this sentence: In order to be able to win, we need to try.",
    },
  ],
});
console.log(completion.choices[0].message.content);
```
</Tab>

<Tab value="Python">
```python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["STROKES_API_KEY"],
    base_url="https://api.strokes.ink/v1",
)

completion = client.chat.completions.create(
    model="swim-1.0",
    messages=[
        {"role": "system", "content": "You are a careful editor."},
        {
            "role": "user",
            "content": "Tighten this sentence: In order to be able to win, we need to try.",
        },
    ],
)
print(completion.choices[0].message.content)
```
</Tab>

<Tab value="curl">
```bash
curl https://api.strokes.ink/v1/chat/completions \
  -H "Authorization: Bearer $STROKES_API_KEY" \
  -H "content-type: application/json" \
  -d '{
    "model": "swim-1.0",
    "messages": [{"role": "user", "content": "Hello"}]
  }'
```
</Tab>
</Tabs>

## Request

| Field | Type | |
| --- | --- | --- |
| `model` | string | **Required.** `swim-1.0`. See [Models](/api/models) |
| `messages` | array | **Required.** Messages with the roles `system`, `developer`, `user`, `assistant` and `tool`. `user` content is a string or text and `image_url` parts; images are a URL or a `data:` URL |
| `max_completion_tokens` | integer | The most tokens to write. `max_tokens` is accepted too. Defaults to the model's maximum |
| `stream` | boolean | Stream the reply as server-sent events |
| `stream_options.include_usage` | boolean | Send a last chunk with `usage` before `[DONE]` |
| `stroke_id` | string | The [stroke](/guides/strokes) the request belongs to. The `stroke-id` header works too |
| `mode` | string | The kind of writing: `business`, `academic` or `creative`. Left out, the API picks one for the request. See [Modes](/guides/modes) |
| `tools` | array | Function tools: `{"type": "function", "function": {"name", "description", "parameters"}}` |
| `tool_choice` | string or object | `none`, `auto`, `required` or `{"type": "function", "function": {"name": …}}` |
| `parallel_tool_calls` | boolean | `false` asks for at most one tool call per reply |
| `temperature` | number | From 0 to 1 |
| `top_p` | number | From 0 to 1 |
| `stop` | string or array | Up to 4 sequences that end the reply |
| `web_search` | boolean | Whether the model may search the web and read pages, as in [Web search](/guides/web-search). Default `true` for `swim-1.0` |
| `web_search_options` | object | How much the model may search: `search_context_size` `low`, `medium` (the default) or `high` — up to 3, 5 or 10 searches and reads. `user_location` is accepted and ignored. Not with `"web_search": false` |
| `reasoning_effort` | string | How much the model reasons before it writes — see [reasoning](#reasoning) |

`system` and `developer` messages are joined into one system prompt wherever they are in the list. `web_search`, `mode` and `stroke_id` are Strokes' own fields: in the OpenAI SDKs, pass them in `extra_body`, such as `extra_body={"web_search": False}` in Python, or in the body with a `// @ts-expect-error` in TypeScript.

Not supported, and answered with a `400`: `n` above 1, `logprobs`, and a `response_format` other than `text`. Other fields, such as `user`, `seed` and the penalties, are accepted and ignored.

### Reasoning

| `reasoning_effort` | |
| --- | --- |
| left out, `minimal` or `low` | Little or no reasoning. The default |
| `medium` or `high` | Some reasoning |
| `xhigh` | Reasons in full, often thousands of tokens |
| `none` | As `low`, and the reasoning isn't returned |

When the model reasoned, its reasoning comes back as `message.reasoning_content` (in a stream, `delta.reasoning_content`), unless `reasoning_effort` is `none`. Reasoning tokens are part of `completion_tokens` and billed. An `assistant` message in the history that carries only `reasoning_content` is skipped: the model never sees its earlier reasoning.

## Response

```json
{
  "id": "chatcmpl-oy7P0NNEFhLHJpJhxqLzTeVT",
  "object": "chat.completion",
  "created": 1795600000,
  "model": "swim-1.0",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "To win, we need to try.",
        "refusal": null
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 38,
    "completion_tokens": 21,
    "total_tokens": 59,
    "prompt_tokens_details": { "cached_tokens": 0 }
  }
}
```

A reply that calls tools has `message.tool_calls` and `finish_reason` `tool_calls`; send each result back as a `tool` message with its `tool_call_id`. `finish_reason` is `length` when `max_completion_tokens` cut the reply short.

Unless `web_search` is `false`, the model decides whether to search, with or without `web_search_options`. The pages it read are in `message.annotations` as `url_citation` entries with each page's `url` and `title`; they cite the whole answer, so `start_index` and `end_index` are both the answer's length. Each search and page read is billed as in [Web search](/guides/web-search#pricing).

`usage` counts every round of the reply, its web searches included, as on [`/v1/messages`](/api/models#swim-10). `prompt_tokens` is all input; `prompt_tokens_details.cached_tokens` is the part read from the [prompt cache](/guides/messages#prompt-caching).

## Streaming

With `stream: true` the reply comes as `chat.completion.chunk` events, each a `data:` line, and ends with `data: [DONE]`. Reasoning streams as `delta.reasoning_content` before the answer's `delta.content`. Tool calls stream as `delta.tool_calls` with their arguments in pieces. With web search, the sources come in one chunk's `delta.annotations` before the last chunk. While the model searches, the stream sends `: ping` comments to keep the connection open.

## Errors

Errors have OpenAI's shape and the same status codes as [`/v1/messages`](/api/errors):

```json
{
  "error": {
    "message": "model: strokes-large",
    "type": "not_found_error",
    "param": null,
    "code": null
  }
}
```

Every response carries a `request-id` header.
