> ## Documentation index
> Fetch the complete documentation index at: https://docs.heurastudios.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Create a chat completion

> Send a conversation to a model and get its live version's answer, whole or streamed.

Source: https://docs.heurastudios.com/api/chat-completions/

`POST https://app.heurastudios.com/v1/chat/completions`

## Request

**Body**

- `model` (string, required): The model's id, like `quarry-3f9a1c2b7d`. It must be the model the key belongs to.
- `messages` (array, required): The conversation, oldest first: objects with `role` (`system`, `developer`, `user` or `assistant`) and `content` (a string, or an array of `{"type": "text", "text": ...}` parts). `developer` is read as `system`. The last message must be the user's question. Up to 200 messages, each up to 8,000 characters.
- `stream` (boolean, optional, default `false`): Send the answer as server-sent events as it's written.
- `stream_options.include_usage` (boolean, optional, default `false`): With `stream`, add a last chunk with token usage.
- `temperature` (number, optional): Accepted for compatibility (0 to 2) and validated. The model's own settings decide sampling.
- `max_tokens` (integer, optional): Accepted for compatibility (at least 1), as is `max_completion_tokens`. The model's own settings decide length.

The model's script, rules, matched files and closest examples are added by the Studio. Send only the conversation. A `system` message you add is read after the model's own brief, up to 2,000 characters of it.

```bash
curl https://app.heurastudios.com/v1/chat/completions \
  -H "Authorization: Bearer $HEURA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "quarry-3f9a1c2b7d",
    "messages": [
      {"role": "user", "content": "How many orders shipped last month?"}
    ]
  }'
```

## Response

```json
{
  "id": "chatcmpl-3b1f0c9a7d2e4f6a8b0c1d2e",
  "object": "chat.completion",
  "created": 1795836000,
  "model": "quarry-3f9a1c2b7d",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "SELECT count(*) FROM orders WHERE shipped_at >= date_trunc('month', now()) - interval '1 month' AND shipped_at < date_trunc('month', now());\nCounts orders shipped in the previous calendar month."
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": { "prompt_tokens": 9, "completion_tokens": 31, "total_tokens": 40 }
}
```

Token counts are estimates from word counts, for compatibility; usage is metered in [credits](https://docs.heurastudios.com/account/plans-and-credits/), not tokens.

## Streaming

With `"stream": true`, the response is `text/event-stream`. The first chunk carries the role, the next ones carry text, the last one carries `"finish_reason": "stop"`, and the stream ends with `[DONE]`.

```text
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","model":"quarry-3f9a1c2b7d","choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-...","object":"chat.completion.chunk","model":"quarry-3f9a1c2b7d","choices":[{"index":0,"delta":{"content":"SELECT count(*)"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-...","object":"chat.completion.chunk","model":"quarry-3f9a1c2b7d","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}]}

data: [DONE]
```

An error after the stream has started arrives as a last `data:` event holding an `error` object, in the same shape as an error response.

## Errors

See [Errors and limits](https://docs.heurastudios.com/api/errors-and-limits/) for every status and code.
