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
FieldTypeRequiredDescription
modelstringชื่อ model (แนะนำ auto-model)Model name (recommended: auto-model)
messagesarrayรายการข้อความในบทสนทนาConversation messages
streambooleantrue เพื่อรับ response แบบ streamingtrue for streaming response
max_tokensintegerจำนวน tokens สูงสุด (default: 4096, max: 40960)Max tokens (default: 4096, max: 40960)
toolsarrayFunction calling definitions
tool_choicestring/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-IDRequest ID
X-Input-Tokensจำนวน input tokensInput token count
X-Output-Tokensจำนวน output tokensOutput token count
X-Brain-TokensBrain tokens ที่ระบบใช้วิเคราะห์ (ไม่หัก credit)Brain tokens used for analysis (not charged)
X-Token-Estimateประมาณการ input tokens (streaming)Estimated input tokens (streaming)
X-Token-Warninglarge-context เมื่อ context ใหญ่เกินขีดแนะนำlarge-context when context exceeds recommended limit
X-Total-CreditsCredits ที่ถูกหักCredits deducted
X-Cachehit / miss
X-Cached-TokensTokens ที่ได้จาก 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-480bCode generation คุณภาพสูงHigh-quality code generation
nemotron-3-superวิเคราะห์ซับซ้อน, คณิตศาสตร์Complex analysis, math
qwen3.5-397bMulti-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": "คำอธิบาย"
  }
}
HTTPTypeDescription
401authentication_errorAPI key ไม่ถูกต้องInvalid API key
402insufficient_creditsCredits ไม่เพียงพอInsufficient credits
403forbiddenไม่มีสิทธิ์ใช้ model นี้No access to this model
422validation_errorRequest body ไม่ถูกต้องInvalid request body
423account_lockedบัญชีถูกล็อค (login ผิดติดต่อกัน 5 ครั้ง — รอ 30 นาที)Account locked (5 failed logins — wait 30 minutes)
428tools_requiredRequest ต้องส่ง tools มาด้วย (model ร้องขอ function calling)Request must include tools (model requires function calling)
429rate_limit_exceededเกิน rate limitRate limit exceeded
429concurrent_limitมี request ค้างอยู่เกินจำนวนToo many concurrent requests
502provider_errorModel ไม่ตอบสนอง (retry ได้ทันที)Model unavailable (retry immediately)
Retry Strategy
  • 423บัญชีถูกล็อค รอ 30 นาที (header Retry-After ระบุวินาทีที่ต้องรอ)Account locked, wait 30 min (Retry-After header indicates seconds)
  • 428เพิ่ม tools parameter แล้วลองใหม่Add tools parameter 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

PlanRequests/นาทีminConcurrent
Free201
Starter304
Plus608
Pro12010

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
Base URL
https://api.kaweai.com/v1
Format
OpenAI-compatible
Kawe AI
สวัสดีครับ! มีอะไรให้ช่วยเกี่ยวกับ Kawe AI ไหมครับ? 👋