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

# Chat Completions

> OpenAI 호환 Chat Completions API

# POST /v1/chat/completions

OpenAI Chat Completions API와 100% 호환되는 엔드포인트입니다. 기존 OpenAI SDK에서 `base_url`만 변경하면 바로 사용할 수 있습니다.

## 헤더

<ParamField header="Authorization" type="string" required>
  `Bearer kr-your-api-key`
</ParamField>

<ParamField header="Content-Type" type="string" required>
  `application/json`
</ParamField>

## 요청 본문

<ParamField body="model" type="string" required>
  모델 ID. `kr/gpt54`, `kr/claude-sonnet-46`, `auto-smart` 등.
</ParamField>

<ParamField body="messages" type="array" required>
  메시지 배열. 각 메시지는 `role` (`system`, `user`, `assistant`)과 `content` (문자열)를 포함합니다. 최대 100개.
</ParamField>

<ParamField body="stream" type="boolean" default="false">
  `true` 시 SSE 스트리밍 응답.
</ParamField>

<ParamField body="temperature" type="number" default="1.0">
  0\~2. 낮을수록 결정적, 높을수록 창의적.
</ParamField>

<ParamField body="max_tokens" type="integer">
  최대 출력 토큰 수. 최대 128,000.
</ParamField>

<ParamField body="top_p" type="number" default="1.0">
  Nucleus sampling 파라미터.
</ParamField>

<ParamField body="fallback" type="array">
  **(고급)** 폴백 모델 목록. 주 모델 실패 시 순서대로 시도합니다. 대부분의 경우 원하는 모델을 직접 지정하는 것으로 충분합니다. 예: `["kr/gpt41", "kr/claude-sonnet-46"]`
</ParamField>

## 응답

```json 200 - 성공 theme={null}
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1711000000,
  "model": "kr/gpt54",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "안녕하세요! 무엇을 도와드릴까요?"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 10,
    "completion_tokens": 15,
    "total_tokens": 25
  }
}
```

## SSE 스트리밍

`stream: true` 설정 시:

```
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1711000000,"model":"kr/gpt54","choices":[{"index":0,"delta":{"content":"안녕"},"finish_reason":null}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1711000000,"model":"kr/gpt54","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: [DONE]
```

## 예시

<CodeGroup>
  ```python Python theme={null}
  from openai import OpenAI

  client = OpenAI(
      api_key="kr-your-api-key",
      base_url="https://api.k-router.com/v1"
  )

  response = client.chat.completions.create(
      model="kr/gpt54",
      messages=[{"role": "user", "content": "안녕하세요"}],
      temperature=0.7,
      max_tokens=1000
  )
  print(response.choices[0].message.content)
  ```

  ```typescript TypeScript theme={null}
  import OpenAI from "openai";

  const client = new OpenAI({
    apiKey: "kr-your-api-key",
    baseURL: "https://api.k-router.com/v1",
  });

  const response = await client.chat.completions.create({
    model: "kr/gpt54",
    messages: [{ role: "user", content: "안녕하세요" }],
  });
  console.log(response.choices[0].message.content);
  ```

  ```bash cURL theme={null}
  curl -X POST https://api.k-router.com/v1/chat/completions \
    -H "Authorization: Bearer kr-your-api-key" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "kr/gpt54",
      "messages": [{"role": "user", "content": "안녕하세요"}],
      "stream": false
    }'
  ```
</CodeGroup>
