# OpenAI compatibility

> Use OpenAI's Python and TypeScript clients with Antarya by changing two lines.

## Change two lines

`POST https://antarya.ai/v1/chat/completions` takes the OpenAI chat request and returns the OpenAI reply. If your code already uses an OpenAI client, change the base URL and the key. Use a key from the Keys page, and the model `antarya`.

```python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://antarya.ai/v1",  # changed
    api_key=os.environ["ANTARYA_API_KEY"],  # changed
)

reply = client.chat.completions.create(
    model="antarya",
    messages=[{"role": "user", "content": "Draft a reply that declines the meeting."}],
)
print(reply.choices[0].message.content)
```

```typescript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://antarya.ai/v1", // changed
  apiKey: process.env.ANTARYA_API_KEY, // changed
});

const reply = await client.chat.completions.create({
  model: "antarya",
  messages: [{ role: "user", content: "Draft a reply that declines the meeting." }],
});
console.log(reply.choices[0].message.content);
```

```bash
curl https://antarya.ai/v1/chat/completions \
  -H "Authorization: Bearer $ANTARYA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "antarya",
    "messages": [{"role": "user", "content": "Draft a reply that declines the meeting."}]
  }'
```

Output:

```
{
  "id": "req_01",
  "object": "chat.completion",
  "model": "antarya",
  "choices": [
    {
      "index": 0,
      "message": {"role": "assistant", "content": "Thank you for the invitation. I am unable to attend..."},
      "finish_reason": "stop"
    }
  ],
  "usage": {"prompt_tokens": 14, "completion_tokens": 19, "total_tokens": 33}
}
```

## Accepted fields

| Field | Notes |
|---|---|
| `messages` | Roles `system`, `developer`, `user`, `assistant` and `tool`. Content is a string or a list of parts (`text`, `image_url`). System and developer messages are not available on this model yet: they are refused until the model object's `capabilities.system` is true. |
| `model` | Use `antarya`. |
| `max_tokens`, `max_completion_tokens` | The longest reply to write. |
| `stream`, `stream_options.include_usage` | Stream the reply, and ask for the usage in the last chunk. |
| `tools`, `tool_choice` | Function tools. |
| `response_format` | `json_schema` or `json_object`. |
| `metadata` | Pairs of your own. |
| `include_thinking` | An extra field that is not part of OpenAI's request. Set it to true to get thinking notes as `reasoning_content`. In Python, pass it as `extra_body={"include_thinking": True}`. In TypeScript, add it to the request object as a plain field. |

Abilities and refusals are the same as on `/v1/chat`. A field for an ability the model does not have yet, such as `tools` or `response_format`, is refused in the same way. See `capabilities` on the [model object](https://antarya.ai/docs/api/models.md).

## Ignored fields

`temperature`, `top_p`, `seed`, `user` and the other sampling fields are accepted and ignored, so existing code keeps working. They do not change the reply.

`n` above 1 is a 400 `invalid_request_error`. Antarya writes one reply.

## The reply

- `finish_reason` is `stop`, `length` (the reply reached the longest it may be) or `tool_calls`.
- `usage` has `prompt_tokens`, `completion_tokens` and `total_tokens`.

## Streaming

With `stream` set to true, the reply is a series of `chat.completion.chunk` events. The id of every event is the request id. The last event is `data: [DONE]`.

```
data: {"id": "req_02", "object": "chat.completion.chunk", "created": 1795737700, "model": "antarya", "choices": [{"index": 0, "delta": {"role": "assistant", "content": ""}, "finish_reason": null}]}

data: {"id": "req_02", "object": "chat.completion.chunk", "created": 1795737700, "model": "antarya", "choices": [{"index": 0, "delta": {"content": "Thank you "}, "finish_reason": null}]}

data: {"id": "req_02", "object": "chat.completion.chunk", "created": 1795737700, "model": "antarya", "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]}

data: [DONE]
```

If a request fails after the stream has started, the status is already sent. The server writes one data line with the error body (the shape in [errors](#errors)) and then `data: [DONE]`.

## Thinking notes

Thinking notes come back as `reasoning_content`, an extra field on the message, and on each streamed delta, only when the request sets `include_thinking` to true. It is the field other OpenAI-compatible providers use, so a client that reads it works the same here. The notes are not counted as output tokens. See [chat](https://antarya.ai/docs/api/chat.md) for what the notes hold.

## Errors

Errors use OpenAI's shape, with the same statuses as the [error page](https://antarya.ai/docs/api/errors.md).

```json
{
  "error": {
    "message": "Too many requests for this key. Retry after 20 seconds.",
    "type": "requests",
    "param": null,
    "code": "rate_limit_exceeded"
  }
}
```

| Status | `type` | `code` |
|---|---|---|
| 400 | `invalid_request_error` | null |
| 401 | `invalid_request_error` | `invalid_api_key` |
| 403 | `invalid_request_error` | `permission_denied` |
| 404 | `invalid_request_error` | `not_found`, or `model_not_found` for an unknown model |
| 413 | `invalid_request_error` | `request_too_large` |
| 429 | `requests` | `rate_limit_exceeded` |
| 500 | `server_error` | null |
| 503 | `server_error` | `overloaded` |

## Next steps

- [Chat](https://antarya.ai/docs/api/chat.md): The same model through Antarya's own request shape.
- [Rate limits](https://antarya.ai/docs/api/rate-limits.md): The allowances that apply to both doors.
- [Errors](https://antarya.ai/docs/api/errors.md): Every status and what to do about it.
