Kawe Chat API
Base URL: https://api.kaweai.com/v1
เริ่มต้นใช้งานQuick Start
Authentication
ใช้ API Key ใน header ทุก request:Include API Key in every request header:
Authorization: Bearer kw-YOUR_API_KEY
cURL
curl https://api.kaweai.com/v1/chat/completions \
-H "Authorization: Bearer kw-YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "auto-model",
"messages": [{"role": "user", "content": "สวัสดี"}]
}'
Chat Completions
POST /v1/chat/completions
Request Body
| Field | Type | Required | Description |
|---|---|---|---|
model | string | ✅ | ชื่อ model (แนะนำ auto-model)Model name (recommended: auto-model) |
messages | array | ✅ | รายการข้อความในบทสนทนาConversation messages |
stream | boolean | true เพื่อรับ response แบบ streamingtrue for streaming response | |
max_tokens | integer | จำนวน tokens สูงสุด (default: 4096, max: 40960)Max tokens (default: 4096, max: 40960) | |
tools | array | Function calling definitions | |
tool_choice | string/object | "auto", "required", หรือระบุ functionor specific function |
Message Format
{
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "สวัสดี"},
{"role": "assistant", "content": "สวัสดีครับ!"},
{"role": "user", "content": "ช่วยอธิบาย REST API ให้หน่อย"}
]
}
Roles: system, user, assistant, tool
Response
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"model": "auto-model",
"choices": [{
"index": 0,
"message": {"role": "assistant", "content": "REST API คือ..."},
"finish_reason": "stop"
}],
"usage": {"prompt_tokens": 25, "completion_tokens": 150, "total_tokens": 175}
}
Response Headers
X-Request-ID | Request ID |
X-Input-Tokens | จำนวน input tokensInput token count |
X-Output-Tokens | จำนวน output tokensOutput token count |
X-Brain-Tokens | Brain tokens ที่ระบบใช้วิเคราะห์ (ไม่หัก credit)Brain tokens used for analysis (not charged) |
X-Token-Estimate | ประมาณการ input tokens (streaming)Estimated input tokens (streaming) |
X-Token-Warning | large-context เมื่อ context ใหญ่เกินขีดแนะนำlarge-context when context exceeds recommended limit |
X-Total-Credits | Credits ที่ถูกหักCredits deducted |
X-Cache | hit / miss |
X-Cached-Tokens | Tokens ที่ได้จาก cacheCached tokens |
Streaming
ส่ง "stream": true เพื่อรับ response ทีละส่วน (Server-Sent Events):Send "stream": true to receive response chunks (SSE):
curl https://api.kaweai.com/v1/chat/completions \
-H "Authorization: Bearer kw-YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "auto-model", "messages": [{"role": "user", "content": "สวัสดี"}], "stream": true}'
Response Stream
data: {"id":"chatcmpl-abc","choices":[{"delta":{"role":"assistant"},"index":0}]}
data: {"id":"chatcmpl-abc","choices":[{"delta":{"content":"สวัสดี"},"index":0}]}
data: {"id":"chatcmpl-abc","choices":[{"delta":{"content":"ครับ"},"index":0}]}
data: [DONE]
Function Calling
ส่ง tools definitionSend tools definition
{
"model": "auto-model",
"messages": [{"role": "user", "content": "อากาศวันนี้ที่กรุงเทพ"}],
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"}
},
"required": ["location"]
}
}
}]
}
Model ตอบกลับ tool_callsModel responds with tool_calls
{
"choices": [{
"message": {
"role": "assistant",
"tool_calls": [{
"id": "call_abc",
"type": "function",
"function": {"name": "get_weather", "arguments": "{\"location\": \"Bangkok\"}"}
}]
},
"finish_reason": "tool_calls"
}]
}
ส่งผลลัพธ์กลับSend result back
{
"model": "auto-model",
"messages": [
{"role": "user", "content": "อากาศวันนี้ที่กรุงเทพ"},
{"role": "assistant", "tool_calls": [{"id": "call_abc", ...}]},
{"role": "tool", "tool_call_id": "call_abc", "content": "{\"temp\": 32, \"condition\": \"sunny\"}"}
]
}
Models
GET /v1/models — ดูรายชื่อ model ทั้งหมดที่แผนคุณใช้ได้List all models available to your plan
Model แนะนำRecommended Models
| Model | เหมาะกับBest For |
|---|---|
auto-model | ✨ แนะนำ — ระบบเลือก model ที่เหมาะสมให้อัตโนมัติRecommended — auto-selects best model |
deepseek-v4-flash | เขียน/debug code, context ยาวมาก (1M tokens)Code, long context (1M tokens) |
qwen3-coder-480b | Code generation คุณภาพสูงHigh-quality code generation |
nemotron-3-super | วิเคราะห์ซับซ้อน, คณิตศาสตร์Complex analysis, math |
qwen3.5-397b | Multi-step reasoning |
gpt-5-nano | ตอบเร็ว, งานง่ายFast, simple tasks |
OpenAI SDK Compatibility
API เป็น OpenAI-compatible — ใช้ SDK ได้เลยโดยเปลี่ยน base_url:API is OpenAI-compatible — use any SDK by changing base_url:
Python
from openai import OpenAI
client = OpenAI(
api_key="kw-YOUR_API_KEY",
base_url="https://api.kaweai.com/v1"
)
response = client.chat.completions.create(
model="auto-model",
messages=[{"role": "user", "content": "สวัสดี"}]
)
print(response.choices[0].message.content)
Node.js
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "kw-YOUR_API_KEY",
baseURL: "https://api.kaweai.com/v1"
});
const response = await client.chat.completions.create({
model: "auto-model",
messages: [{ role: "user", content: "สวัสดี" }]
});
console.log(response.choices[0].message.content);
Error Handling
{
"error": {
"type": "error_type",
"message": "คำอธิบาย"
}
}
| HTTP | Type | Description |
|---|---|---|
| 401 | authentication_error | API key ไม่ถูกต้องInvalid API key |
| 402 | insufficient_credits | Credits ไม่เพียงพอInsufficient credits |
| 403 | forbidden | ไม่มีสิทธิ์ใช้ model นี้No access to this model |
| 422 | validation_error | Request body ไม่ถูกต้องInvalid request body |
| 423 | account_locked | บัญชีถูกล็อค (login ผิดติดต่อกัน 5 ครั้ง — รอ 30 นาที)Account locked (5 failed logins — wait 30 minutes) |
| 428 | tools_required | Request ต้องส่ง tools มาด้วย (model ร้องขอ function calling)Request must include tools (model requires function calling) |
| 429 | rate_limit_exceeded | เกิน rate limitRate limit exceeded |
| 429 | concurrent_limit | มี request ค้างอยู่เกินจำนวนToo many concurrent requests |
| 502 | provider_error | Model ไม่ตอบสนอง (retry ได้ทันที)Model unavailable (retry immediately) |
Retry Strategy
- 423 — บัญชีถูกล็อค รอ 30 นาที (header
Retry-Afterระบุวินาทีที่ต้องรอ)Account locked, wait 30 min (Retry-Afterheader indicates seconds) - 428 — เพิ่ม
toolsparameter แล้วลองใหม่Addtoolsparameter and retry - 429 — รอสักครู่แล้วลองใหม่ (exponential backoff แนะนำ)Wait and retry (exponential backoff recommended)
- 502 — ลองใหม่ได้ทันที (ระบบจะ failover อัตโนมัติ)Retry immediately (auto-failover)
- 402 — เติม creditsTop up credits
💡
Retry-After header มีเฉพาะ HTTP 423 (login throttle) — API rate limit (429) ไม่มี header นี้ ใช้ exponential backoff แทน
💡 Retry-After header is only present on HTTP 423 (login throttle) — API rate limit (429) does not include this header, use exponential backoff instead
Rate Limits
| Plan | Requests/นาทีmin | Concurrent |
|---|---|---|
| Free | 20 | 1 |
| Starter | 30 | 4 |
| Plus | 60 | 8 |
| Pro | 120 | 10 |
Credits
- Credits ถูกหักตาม token usage จริง (input + output)Credits deducted based on actual token usage
- ดูยอดคงเหลือ:Check balance:
GET /v1/me/credits - ดู usage วันนี้/เดือนนี้:Today/month usage:
GET /v1/me/usage/current