# Chat completions endpoint: POST /api/v1/chat/completions

> เอกสารอ้างอิง chat completions endpoint: ทุกฟิลด์ body ของ POST /api/v1/chat/completions, response, finish reasons และ token usage รองรับ OpenAI-compatible

- URL: https://llmwithcrypto.com/th/docs/chat-completions/
- อัปเดตล่าสุด: 2026-10-23
- ผู้เผยแพร่: LLMwithCrypto (https://llmwithcrypto.com)

endpoint เดียวตอบทุกโมเดล รับ body เดียวกับ OpenAI chat completions API และคืนออบเจกต์เดียวกัน ดังนั้นการเชื่อมต่อที่มีอยู่แล้วเพียงต้องการ base URL ใหม่, key ใหม่ และ model id จากรายการของเรา

**คำตอบสั้น — chat completions endpoint ของ LLMwithCrypto รับฟิลด์ใดบ้าง?**

รับ body ของ OpenAI chat completions: `model` และ `messages` จำเป็น; `max_tokens`, `temperature`, `top_p`, `stop`, `stream`, `stream_options`, `tools`, `tool_choice` และ `response_format` เป็นตัวเลือก **LLMwithCrypto** คืนออบเจกต์ `chat.completion` พร้อมจำนวน `usage` คิดค่าบริการต่อโทเค็นจากเครดิตของคุณ ตามที่ระบุไว้ในเอกสารเมื่อ 23 ตุลาคม 2026 การเรียก tool และ JSON output ขึ้นอยู่กับโมเดล

## endpoint โดยสรุป

| แอตทริบิวต์ | ค่า |
| --- | --- |
| Method และ URL | `POST https://llmwithcrypto.com/api/v1/chat/completions` |
| การยืนยันตัวตน | ส่ง API key ของคุณเป็น Bearer token ใน Authorization header เหมือนที่ OpenAI SDK ทำ |
| Body | JSON, `Content-Type: application/json` |
| Response | ออบเจกต์ `chat.completion` หรือ server-sent events ด้วย `stream: true` |
| การเรียกเก็บเงิน | คุณจ่ายต่อ token จากยอดคงเหลือ: input และ output tokens ถูกนับด้วย tokenizer ของโมเดลนั้นเองและคิดตามราคาของโมเดลนั้นต่อ 1M tokens การเรียกที่จบด้วย error ไม่มีค่าใช้จ่าย |
| ฟีเจอร์ | Streaming ใช้ได้กับทุกโมเดล; tool calling และ JSON output ขึ้นอยู่กับสิ่งที่แต่ละโมเดลรองรับเอง |

## สร้าง chat completion

`POST /api/v1/chat/completions`

ส่งบทสนทนาไปยังโมเดลหนึ่งและคืนคำตอบพร้อมจำนวนโทเค็น ตัวอย่างถามคำถามสั้นๆ กับ DeepSeek V4 Pro โดยจำกัด 200 โทเค็น; id, เวลา และจำนวนใน response เป็นเพียงตัวอย่าง

การยืนยันตัวตน: API key ใน header `Authorization: Bearer API_KEY`

**Request body**

| ฟิลด์ | ประเภท | จำเป็น | คำอธิบาย |
| --- | --- | --- | --- |
| `model` | string | ใช่ | model id เช่น `deepseek-v4-pro` หรือ `gpt-oss-120b` `GET /api/v1/models` คืนทุก id ที่ key ของคุณเรียกได้ |
| `messages` | object[] | ใช่ | บทสนทนา เรียงจากเก่าสุดก่อน แต่ละรายการมี `role` (`system`, `user`, `assistant` หรือ `tool`) และสตริง `content`; ข้อความ `tool` ยังมี `tool_call_id` ด้วย |
| `max_tokens` | integer | ไม่ใช่ | จำนวนโทเค็นสูงสุดที่คำตอบใช้ได้ หากไม่ระบุ โมเดลอาจเขียนจน context window เต็ม และคุณจ่ายทุกโทเค็นที่มันเขียน |
| `temperature` | number | ไม่ใช่ | 0 ถึง 2 ค่ายิ่งต่ำยิ่งคาดเดาได้ ค่ายิ่งสูงยิ่งหลากหลาย ค่าเริ่มต้นขึ้นอยู่กับโมเดล |
| `top_p` | number | ไม่ใช่ | 0 ถึง 1, nucleus sampling เปลี่ยน `temperature` หรือ `top_p` อย่างใดอย่างหนึ่ง ไม่ใช่ทั้งสอง |
| `stop` | string or string[] | ไม่ใช่ | สูงสุด 4 sequence ที่สิ้นสุดคำตอบเมื่อโมเดลเขียนมัน ไม่รวมอยู่ในคำตอบ |
| `stream` | boolean | ไม่ใช่ | `true` ส่งคำตอบเป็น server-sent events ทีละส่วน ดู [streaming](https://llmwithcrypto.com/th/docs/streaming/) |
| `stream_options` | object | ไม่ใช่ | `{"include_usage": true}` เพิ่ม event สุดท้ายพร้อม `usage` ของการเรียกแบบ streamed |
| `tools` | object[] | ไม่ใช่ | ฟังก์ชันที่โมเดลอาจขอให้คุณเรียก ในรูปแบบ OpenAI ใช้ได้กับโมเดลที่รองรับ tool calling ดู [tool calling](https://llmwithcrypto.com/th/docs/tool-calling/) |
| `tool_choice` | string or object | ไม่ใช่ | `auto` (ค่าเริ่มต้นเมื่อตั้ง `tools`), `none`, `required` หรือ `{"type": "function", "function": {"name": "…"}}` |
| `response_format` | object | ไม่ใช่ | `{"type": "json_object"}` ขอออบเจกต์ JSON ที่ถูกต้อง บนโมเดลที่รองรับ ระบุใน prompt ของคุณว่า JSON ควรมีอะไรบ้าง |

*ตัวอย่าง: a short answer from DeepSeek V4 Pro*

```json
{
  "model": "deepseek-v4-pro",
  "messages": [
    {
      "role": "system",
      "content": "Answer in two sentences."
    },
    {
      "role": "user",
      "content": "What is a context window?"
    }
  ],
  "max_tokens": 200
}
```

*ตัวอย่าง: a JSON object from gpt-oss-120b, low temperature*

```json
{
  "model": "gpt-oss-120b",
  "messages": [
    {
      "role": "user",
      "content": "Return {\"city\": string, \"country\": string} for the Eiffel Tower."
    }
  ],
  "response_format": {
    "type": "json_object"
  },
  "temperature": 0.2,
  "max_tokens": 100
}
```

**Response** `200`

```json
{
  "id": "chatcmpl-8f2c41d0a7b94e1c",
  "object": "chat.completion",
  "created": 1792742400,
  "model": "deepseek-v4-pro",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "A context window is the maximum number of tokens a model can read and write in one request. Anything beyond it must be cut or summarized before the call."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 27,
    "completion_tokens": 34,
    "total_tokens": 61
  }
}
```

*curl*

```bash
curl -s "https://llmwithcrypto.com/api/v1/chat/completions" \
  -H "Authorization: Bearer $LLMWITHCRYPTO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-v4-pro",
    "messages": [
      {"role": "system", "content": "Answer in two sentences."},
      {"role": "user", "content": "What is a context window?"}
    ],
    "max_tokens": 200
  }'
```

*Python*

```python
import os

from openai import OpenAI

client = OpenAI(
    base_url="https://llmwithcrypto.com/api/v1",
    api_key=os.environ["LLMWITHCRYPTO_API_KEY"],
)

resp = client.chat.completions.create(
    model="deepseek-v4-pro",
    messages=[
        {"role": "system", "content": "Answer in two sentences."},
        {"role": "user", "content": "What is a context window?"},
    ],
    max_tokens=200,
)
print(resp.choices[0].message.content)
print(resp.usage.prompt_tokens, "in,", resp.usage.completion_tokens, "out")
```

*Node.js*

```javascript
import OpenAI from 'openai';

const client = new OpenAI({
  baseURL: 'https://llmwithcrypto.com/api/v1',
  apiKey: process.env.LLMWITHCRYPTO_API_KEY,
});

const resp = await client.chat.completions.create({
  model: 'deepseek-v4-pro',
  messages: [
    { role: 'system', content: 'Answer in two sentences.' },
    { role: 'user', content: 'What is a context window?' },
  ],
  max_tokens: 200,
});
console.log(resp.choices[0].message.content);
console.log(resp.usage.prompt_tokens, 'in,', resp.usage.completion_tokens, 'out');
```

## ฉันเขียน messages อย่างไร?

ส่งบทสนทนาทั้งหมดทุกครั้ง โดยเริ่มจากข้อความเก่าสุด: API ไม่เก็บความจำระหว่างการเรียก และไม่มีข้อมูลใดจากพรอมป์ของคุณถูกจัดเก็บ เราไม่เก็บ prompts ของคุณหรือคำตอบของโมเดล: เมื่อส่งคำตอบแล้ว ข้อความนั้นก็หายไป แต่ละครั้งที่เรียกจะเหลือ log หนึ่งบรรทัดซึ่งมีเวลา บัญชี โมเดล และจำนวน token แล้วลบออกหลัง 30 วัน.

- **`system`**: คำสั่งที่กำหนดกรอบทุกคำตอบ เช่น โทน ภาษา หรือรูปแบบผลลัพธ์ ใส่ไว้บนสุดหนึ่งรายการ
- **`user`**: สิ่งที่ผู้ใช้หรือโปรแกรมของคุณถาม
- **`assistant`**: คำตอบก่อนหน้าของโมเดล เพื่อให้มันสนทนาต่อได้
- **`tool`**: ผลลัพธ์ของฟังก์ชันที่โมเดลขอให้คุณรัน พร้อม `tool_call_id` ที่มันให้มา

ทุกข้อความนับเป็น input tokens ประวัติยาวจึงมีค่าใช้จ่ายมากขึ้นในแต่ละรอบ ตัดทอนหรือสรุปเทิร์นเก่าเมื่อบทสนทนายาวขึ้น

## คำขอหนึ่งยาวได้แค่ไหน?

input และ output รวมกันต้องพอดีกับ context window ของโมเดล ณ วันที่ 23 ตุลาคม 2026 DeepSeek V4 Pro อ่านได้ถึง 1M tokens, gpt-oss-120b ถึง 128K และ Qwen3 235B ถึง 256K; [หน้ารุ่นโมเดล](https://llmwithcrypto.com/th/models/) แสดงรายการทั้งหมด

คำขอที่ไม่พอดีจะตอบกลับเป็น `400` พร้อมโค้ด `context_length_exceeded` และไม่มีค่าใช้จ่าย ตั้งค่า `max_tokens` เพื่อให้พรอมป์บวกคำตอบยาวสุดที่คุณรับได้ยังอยู่ในหน้าต่าง

## finish_reason บอกอะไรฉัน?

| `finish_reason` | ความหมาย | สิ่งที่ต้องทำ |
| --- | --- | --- |
| `stop` | โมเดลจบคำตอบแล้ว หรือเขียนหนึ่งใน `stop` sequences ของคุณ | ไม่ต้องทำอะไร: คำตอบสมบูรณ์แล้ว |
| `length` | คำตอบถึง `max_tokens` หรือ context window | เพิ่ม `max_tokens` หรือขอคำตอบที่สั้นลง |
| `tool_calls` | โมเดลต้องการให้คุณรันฟังก์ชันหนึ่งหรือหลายฟังก์ชัน | รันแล้วส่งผลลัพธ์กลับ ดู [tool calling](https://llmwithcrypto.com/th/docs/tool-calling/) |

## พารามิเตอร์ OpenAI ตัวใดที่ไม่รองรับ?

ฟิลด์ในตารางข้างต้นคือข้อตกลงทั้งหมด ฟิลด์อื่นจาก OpenAI API เช่น `n` (หลายคำตอบต่อการเรียก), `logprobs`, `audio` หรือ image inputs ไม่รองรับ: ส่งเฉพาะฟิลด์ที่ระบุไว้ที่นี่ โมเดลเป็น text models ไม่มีตัวใดรับรูปภาพผ่าน endpoint นี้

ฟิลด์ `model` รับเฉพาะ id ในรายการของเราเท่านั้น เราไม่ขายการเข้าถึง GPT, Claude, Gemini หรือ Grok: ผู้ผลิตห้ามขายต่อการเข้าถึง API โดยไม่ได้รับอนุมัติจากพวกเขา และเราไม่ได้รับอนุมัติดังกล่าว.

## Chat completions: คำถามที่พบบ่อย

### มี legacy completions endpoint ไหม?

ไม่มี มีเฉพาะรูปแบบ chat; พรอมป์เดียวคือ array `messages` ที่มีข้อความ `user` หนึ่งข้อความ

### การเรียกหนึ่งครั้งมีค่าใช้จ่ายเท่าไร?

คุณจ่ายต่อ token จากยอดคงเหลือ: input และ output tokens ถูกนับด้วย tokenizer ของโมเดลนั้นเองและคิดตามราคาของโมเดลนั้นต่อ 1M tokens การเรียกที่จบด้วย error ไม่มีค่าใช้จ่าย. ราคาต่อโมเดลอยู่ใน[หน้าราคา](https://llmwithcrypto.com/th/pricing/).

### ดูคำขอที่ผ่านมาของฉันได้ไหม?

ไม่ได้ ตามการออกแบบ เราไม่เก็บ prompts ของคุณหรือคำตอบของโมเดล: เมื่อส่งคำตอบแล้ว ข้อความนั้นก็หายไป แต่ละครั้งที่เรียกจะเหลือ log หนึ่งบรรทัดซึ่งมีเวลา บัญชี โมเดล และจำนวน token แล้วลบออกหลัง 30 วัน.

### ทำไมคำตอบของฉันหยุดกลางคัน?

`finish_reason` จะเป็น `length`: คำตอบชน `max_tokens` เพิ่มค่านั้นภายใน context window ของโมเดล

---

LLMwithCrypto: LLMwithCrypto คือ LLM API ที่เข้ากันได้กับ OpenAI สำหรับโมเดล open-weight — DeepSeek V4 Pro, DeepSeek V4.1 Flash, DeepSeek R1, Qwen3.5 397B A17B, Qwen3 235B A22B Instruct, gpt-oss-120b, gpt-oss-20b, GLM-4.7, Llama 3.3 70B Instruct, Gemma 4 31B, Mistral Large 3, Mistral Small 4 — จ่ายต่อ token จากยอดคงเหลือคริปโตแบบเติมเงิน ในราคาต่ำกว่าค่ามัธยฐานของโฮสต์สาธารณะ 5% ไม่ต้อง KYC: ไม่ต้องใช้บัตรประชาชน ไม่ต้องเซลฟี่ ไม่ต้องใช้เอกสาร ไม่มีการเก็บ prompt และ output
