# Chat

Text models have two protocol entry points; pick the one your SDK or tool speaks: the OpenAI protocol `POST /v1/chat/completions` (every text model) and the Anthropic protocol `POST /v1/messages` (Claude models; works directly with Claude Code and similar tools). Both are current, supported interfaces: same key, same per-token billing.

## Request

The body is the standard chat completions body: `model` and `messages` are required.

```json
{
  "model": "MODEL_ID",
  "messages": [
    { "role": "system", "content": "You are a helpful assistant." },
    { "role": "user", "content": "Hello" }
  ]
}
```

## Response and usage

The response is a standard `chat.completion` object (shortened below). Billing follows the token counts the model reports in `usage`.

```json
{
  "object": "chat.completion",
  "model": "MODEL_ID",
  "choices": [
    {
      "index": 0,
      "message": { "role": "assistant", "content": "..." },
      "finish_reason": "stop"
    }
  ],
  "usage": { "prompt_tokens": <int>, "completion_tokens": <int> }
}
```

## Streaming

Set `"stream": true` to receive server-sent events. Add `stream_options.include_usage` to get token usage in the stream.

```bash
curl https://api.novapi.ai/v1/chat/completions -N \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "MODEL_ID",
    "stream": true,
    "stream_options": { "include_usage": true },
    "messages": [{ "role": "user", "content": "Hello" }]
  }'
```

## Errors you may see

- 401 `invalid_api_key`: missing or invalid key.
- 400 `model_not_found`: the model is not currently available.
- 402 `insufficient_quota`: balance too low (a balance under 0.1 credits is rejected).
- 403 `model_not_allowed`: the key's model allowlist does not include the model.
- 502 `upstream_error` / `upstream_auth_error`: the provider call failed. Provider credential problems are reported generically and never echoed.

## Anthropic protocol (/v1/messages)

Use this entry point with the Anthropic SDK or Claude Code: `POST /v1/messages` with a standard Messages body (`model`, `max_tokens` and `messages` are required; `system`, `stream`, `tools` and the rest work as usual). Send the key as `x-api-key` or `Authorization: Bearer`.

Set `model` to a Claude model id. GPT model names also work: the gateway translates between the Anthropic and OpenAI protocols. Use `GET /v1/models` for the models that are available.

`POST /v1/messages/count_tokens` estimates tokens and is not billed. See the "Anthropic protocol" reference page for every field, the stream events and more examples.

```bash
curl https://api.novapi.ai/v1/messages   -H "x-api-key: $API_KEY"   -H "anthropic-version: 2023-06-01"   -H "Content-Type: application/json"   -d '{
    "model": "claude-haiku-4-5-20251001",
    "max_tokens": 1024,
    "messages": [{ "role": "user", "content": "Hello" }]
  }'
```

## Counting tokens

`POST /v1/chat/completions/count_tokens` returns a local estimate (`input_tokens`, `estimated: true`). It is not billed and errs high.

## See also
- [Error reference](https://www.novapi.ai/docs/guide/errors)
