# Chat Completions API

Use the Chat Completions and model-discovery endpoints, including supported fields, streaming, tools, limits, and errors.

## Create a completion.

Send JSON with your API key in the bearer header. Unlisted fields are rejected.

### POST /v1/chat/completions

```http
POST https://beta.blaschkelabs.com/v1/chat/completions
Authorization: Bearer $BLASCHKE_API_KEY
Content-Type: application/json

{"model":"<model-id>","messages":[{"role":"user","content":"Explain a Blaschke product."}],"stream":false}
```

**Accepted top-level fields**

| Field | Accepted form | Notes |
| --- | --- | --- |
| model | Required model ID | Use an available ID from `GET /v1/models`. |
| messages | 1–100 messages | Roles: system, user, assistant, and tool. |
| stream | Boolean; default false | Set to true for server-sent events. |
| stream_options | `include_usage: boolean` | Request a final usage event when streaming. |
| max_tokens | Integer, 1–8192; default 512 | Model-specific output limits may be lower. |
| temperature / top_p | 0–2 / 0–1 | Optional generation controls. |
| stop | String or up to 4 strings | Optional stop sequences. |
| seed | Integer | Does not guarantee reproducibility across workers. |
| tools | 1–64 function definitions | JSON parameters are supported; `strict: true` is not. |
| tool_choice | auto, none, required, or a named function | Test tool selection with your chosen model. |
| chat_template_kwargs | Model-specific object | Only for controls supported by the selected model and runtime. |

## Messages and tool history.

Return assistant tool-call messages and matching tool results together. Keep each `tool_call_id` and never fabricate results.

**Bounded history**

| Item | Limit |
| --- | --- |
| Messages | 1–100 |
| Calls per assistant message | 8 |
| Tool definitions | 64 |
| Call ID / function name | 128 characters |
| Function description | 16,384 characters |
| Encoded arguments | 32,768 characters |
| JSON body | 3,000,000 bytes |
| Message text | Model-specific character cap |

## Responses and usage.

Responses follow the OpenAI Chat Completions shape. Streaming sends content and tool calls as deltas; enable `stream_options.include_usage` for a separate usage event.

### Success response

```json
{"id":"...","object":"chat.completion","created":0,"model":"<model-id>","choices":[{"index":0,"message":{"role":"assistant","content":"..."},"finish_reason":"stop"}],"usage":{"prompt_tokens":12,"completion_tokens":8,"total_tokens":20}}
```

## Errors.

Read the HTTP status and the error `message`, `type`, `param`, and `code`. Generated errors currently use `code: null`; upstream responses may differ.

**HTTP outcomes**

| Status | Meaning |
| --- | --- |
| 400 | Invalid JSON, content type, request field, role, or output budget. |
| 401 | Missing, invalid, or inactive API key. |
| 402 | The available balance cannot cover the requested maximum. |
| 404 | Unknown model ID or endpoint. |
| 409 | An accounting update conflicted with another request. |
| 429 | A budget or concurrency limit rejected the request. |
| 502/503 | The inference worker failed or is unavailable. |
| 504 | The inference request timed out. |

### Gateway error

```json
{"error":{"message":"...","type":"invalid_request_error","param":null,"code":null}}
```

## Related guides

- [Model discovery](https://beta.blaschkelabs.com/docs/models): Select an available model ID.
- [Errors and retries](https://beta.blaschkelabs.com/docs/errors): Resolve request and service failures.

Source: https://beta.blaschkelabs.com/docs/api

Markdown: https://beta.blaschkelabs.com/docs/md/api.md
