# Chat

> POST /v1/chat replies to a conversation, as one body or as a stream of events.

## Create a reply

`POST https://antarya.ai/v1/chat`

Send the conversation so far. Antarya returns the next reply. Pass `session_id` to continue a stored session, which is covered in [sessions](https://antarya.ai/docs/api/sessions.md). The guides cover the larger features one at a time: [conversations](https://antarya.ai/docs/guides/conversations.md), [streaming](https://antarya.ai/docs/guides/streaming.md), [structured output](https://antarya.ai/docs/guides/structured-output.md), [tool use](https://antarya.ai/docs/guides/tool-use.md) and [files and images](https://antarya.ai/docs/guides/files-and-images.md).

### Headers

| Header | Value |
|---|---|
| `Authorization` | `Bearer $ANTARYA_API_KEY` |
| `Content-Type` | `application/json` |

```bash
curl https://antarya.ai/v1/chat \
  -H "Authorization: Bearer $ANTARYA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "antarya",
    "system": "Answer in plain English.",
    "messages": [
      {"role": "user", "content": "Summarise this contract'"'"'s termination clause."}
    ],
    "max_output_tokens": 400
  }'
```

```python
from antarya import Antarya

client = Antarya()

reply = client.chat.create(
    model="antarya",
    system="Answer in plain English.",
    messages=[{"role": "user", "content": "Summarise this contract's termination clause."}],
    max_output_tokens=400,
)
print(reply.content[0].text)
```

```typescript
import Antarya from "antarya";

const client = new Antarya();

const reply = await client.chat.create({
  model: "antarya",
  system: "Answer in plain English.",
  messages: [{ role: "user", content: "Summarise this contract's termination clause." }],
  max_output_tokens: 400,
});
console.log(reply.content[0].text);
```

Output:

```
{
  "id": "req_01",
  "model": "antarya",
  "content": [
    {"type": "text", "text": "Either side can end the agreement with 30 days of written notice."}
  ],
  "stop_reason": "end_turn",
  "usage": {"input_tokens": 24, "output_tokens": 17}
}
```

## Request body

- `model` (text, optional): The id or alias of the model. Leave it out to use the current default.
- `messages` (array, required): The conversation, oldest message first.
  - `messages.role` (text, required): "user", "assistant" or "tool".
  - `messages.content` (text or array, required): Plain text, or a list of parts. A part is text, an image, a file, a tool_call or a tool_result. The parts are listed under [message parts](#parts), and [files and images](https://antarya.ai/docs/guides/files-and-images.md) shows them in use.
- `session_id` (text, optional): The id of a [stored session](https://antarya.ai/docs/guides/sessions.md). Its saved turns go before `messages`, and on success the new turns and the reply are saved to it. A failed or refused request saves nothing.
- `include_thinking` (boolean, optional): Set to true to get short thinking notes with the reply. The default is false. A whole reply carries them as `thinking`, and a stream sends them as `thinking` events.
- `system` (text, optional): Instructions that apply to the whole conversation.
- `max_output_tokens` (number, optional): The most tokens the reply may hold. The reply stops there if it reaches the limit.
- `stream` (boolean, optional): Set to true to receive server-sent events. The default is false.
- `tools` (array, optional): Functions she may ask you to call. Each has a name, a description and an input_schema.
- `tool_choice` (object, optional): How freely she may use tools. `auto` lets her decide and is the default, `none` stops her using tools, `any` makes her use one, and {"type": "tool", "name": "..."} makes her use that tool.
- `response_format` (object, optional): A JSON schema the reply must follow, as {"type": "json_schema", "schema": {...}}. The schema may use the keywords type, properties, required, items, enum, description and additionalProperties.
- `metadata` (object, optional): Your own labels for the request, up to 16 string pairs, with keys up to 64 characters and values up to 512 characters. They are kept with the usage record. They are never sent to the model.

### Message parts

| Part | Shape | Limits |
|---|---|---|
| Text | `{"type": "text", "text": "..."}` | |
| File, by id | `{"type": "file", "file_id": "file_..."}` | A file uploaded to [/v1/files](https://antarya.ai/docs/api/files.md). |
| File, inline | `{"type": "file", "source": {"type": "base64", "filename": "agreement.pdf", "media_type": "application/pdf", "data": "..."}}` | Up to 10 MB decoded. Read for this request only and not stored. |
| Image | `{"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "..."}}` | PNG, JPEG or WebP, up to 10 MB each and 20 a request. |

## Response

- `id` (text, required): The id of this reply.
- `model` (text, required): The model that wrote the reply.
- `content` (array, required): The reply, as a list of parts.
  - `content.type` (text, required): "text" or "tool_call".
  - `content.text` (text, optional): The words of a text part.
- `stop_reason` (text, required): Why the reply ended. See the table below.
- `thinking` (array, optional): Only when `include_thinking` is true. A list of notes, each with a `step`, a `stage` (a short label) and a `summary`.
- `usage` (object, required): What the request used.
  - `usage.input_tokens` (number, required): Tokens in everything you sent.
  - `usage.output_tokens` (number, required): Tokens in the reply.

| `stop_reason` | Meaning |
|---|---|
| `end_turn` | Antarya finished the reply. |
| `max_output_tokens` | The reply reached `max_output_tokens` and stopped. |
| `tool_call` | The reply asks you to run a function. Send the result back. |

A reply cut off at `max_output_tokens` ends with that value, including a JSON reply, which is then not complete.

## Streaming

With `stream` set to true, the response is a stream of server-sent events. The events are `reply.start`, `content.delta`, `reply.done` and `error`, and `thinking` when `include_thinking` is true. The [streaming guide](https://antarya.ai/docs/guides/streaming.md) shows each one and the data it carries.

```
event: reply.start
data: {"id": "req_03", "model": "antarya"}

event: content.delta
data: {"index": 0, "type": "text", "text": "Thank you for the invitation. "}

event: reply.done
data: {"stop_reason": "end_turn", "usage": {"input_tokens": 14, "output_tokens": 19}}
```

## Not yet available

`system`, `tools` and `response_format` are not available on this model yet. Each turns on when the model object's `capabilities` flag (`system`, `tools`, `structured_output`) is true.

## Limits

The model reports a longest reply of 2,048 tokens.

| Limit | Value |
|---|---|
| Longest reply | 2,048 tokens (`max_output_tokens` on the `antarya` model) |

## Next steps

- [Count tokens](https://antarya.ai/docs/api/count-tokens.md): Measure a request before you send it.
- [Errors](https://antarya.ai/docs/api/errors.md): What each failure looks like.
- [Quickstart](https://antarya.ai/docs/quickstart.md): Send a first request.
- [Sessions](https://antarya.ai/docs/api/sessions.md): Keep a conversation on Antarya's side.
- [OpenAI compatibility](https://antarya.ai/docs/api/openai-compatibility.md): Use OpenAI's clients with two changed lines.
