# Chat Completions

> POST /v1/chat/completions, format OpenAI, untuk semua model teks di Vikey termasuk Claude dan Vikey Auto.

```http
POST https://api.vikey.ai/v1/chat/completions
```

Format request dan response sama dengan [Chat Completions OpenAI](https://platform.openai.com/docs/api-reference/chat). Endpoint ini melayani semua model teks di Vikey, bukan hanya model OpenAI.

## Contoh

```bash
curl https://api.vikey.ai/v1/chat/completions \
  -H "Authorization: Bearer API_KEY_KAMU" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "anthropic/claude-sonnet-5.5",
    "max_tokens": 1024,
    "messages": [
      {"role": "system", "content": "Jawab singkat dalam bahasa Indonesia."},
      {"role": "user", "content": "Apa beda let dan const di JavaScript?"}
    ]
  }'
```

## Parameter yang sering dipakai

| Parameter | Keterangan |
| --- | --- |
| `model` | Wajib. Model id, misalnya `openai/gpt-6.1-sol` atau `vikey/agent-gpt`. |
| `messages` | Wajib. Array pesan dengan `role` (`system`, `user`, `assistant`, `tool`) dan `content`. |
| `max_tokens` | Batas token output. Juga dipakai untuk [pengecekan saldo](/models/pricing/#pengecekan-saldo), jadi isi dengan angka yang wajar. |
| `stream` | `true` untuk Server-Sent Events. Stream diakhiri `data: [DONE]`. |
| `tools`, `tool_choice` | Function calling dengan format OpenAI. |
| `temperature`, `top_p`, `stop` | Diteruskan ke model. |
| `response_format` | JSON mode atau JSON schema, untuk model yang mendukung. |

Parameter lain diteruskan ke model apa adanya. Model yang tidak mendukung suatu parameter bisa mengabaikannya atau mengembalikan error.

## Gambar (vision)

Untuk model dengan kemampuan vision, kirim gambar sebagai bagian dari `content`:

```json
{
  "role": "user",
  "content": [
    {"type": "text", "text": "Ada bug apa di screenshot ini?"},
    {"type": "image_url", "image_url": {"url": "https://contoh.com/error.png"}}
  ]
}
```

URL juga boleh berupa `data:image/png;base64,...`.

## Response

```json
{
  "id": "chatcmpl-...",
  "object": "chat.completion",
  "model": "anthropic/claude-sonnet-5.5",
  "choices": [
    {
      "index": 0,
      "message": {"role": "assistant", "content": "let bisa diubah nilainya, const tidak..."},
      "finish_reason": "stop"
    }
  ],
  "usage": {"prompt_tokens": 32, "completion_tokens": 58, "total_tokens": 90}
}
```

## Catatan perilaku

- `tool_choice` tanpa `tools` dibuang otomatis supaya request tidak ditolak model.
- Karakter null (`\u0000`) di teks dibuang sebelum diteruskan, karena beberapa model menolaknya.
- `max_tokens` yang sangat kecil dinaikkan ke batas minimum yang diterima model.

Source: https://docs.vikey.ai/api/chat-completions/
