DEV Community

Cover image for วิธีใช้ Kimi K2.6 API อย่างไร
Thanawat Wongchai
Thanawat Wongchai

Posted on • Originally published at apidog.com

วิธีใช้ Kimi K2.6 API อย่างไร

การประกาศเปิดตัว Kimi K2.6 ของ Moonshot AI วางตำแหน่งให้เป็นนวัตกรรมโอเพ่นซอร์สที่ทันสมัยที่สุดสำหรับการเขียนโค้ด การดำเนินการระยะยาว และกลุ่มเอเจนต์ API ที่ขับเคลื่อนมันเข้ากันได้กับ OpenAI โฮสต์อยู่ที่ https://api.moonshot.ai/v1 และมีเอกสารบน แพลตฟอร์ม หากคุณติดตั้ง OpenAI SDK คุณจะสามารถส่งคำขอจริงได้ภายในเวลาประมาณห้านาที

ทดลองใช้ Apidog วันนี้

คู่มือนี้จะแนะนำตั้งแต่การยืนยันตัวตน คำขอแรกของคุณ การสตรีม การเรียกใช้เครื่องมือ การป้อนข้อมูลด้วยภาพและวิดีโอ โหมดการคิด และวิธีขับเคลื่อน Agent Swarm ด้วยซับเอเจนต์ 300 ตัว และแสดงวิธีทดสอบทุกเอนด์พอยต์ด้วย Apidog ก่อนที่คุณจะเขียนโค้ดผสานรวม

💡ทางลัด: ทดสอบ Kimi K2.6 API ด้วยภาพใน Apidog ก่อนที่จะคอมมิตโค้ดผสานรวมใดๆ เพียงนำเข้าครั้งเดียว ใส่ Bearer token คุณก็สามารถส่งคำขอแบบสตรีมมิ่งจริงได้พร้อมประวัติทั้งหมดและการตรวจสอบสคีมา ดาวน์โหลด Apidog ฟรี

สรุปสั้นๆ: Kimi K2.6 API ใน 60 วินาที

  • Base URL: https://api.moonshot.ai/v1
  • เอนด์พอยต์: POST /chat/completions
  • รหัสโมเดล: kimi-k2.6, kimi-k2.6-thinking
  • การยืนยันตัวตน: Authorization: Bearer $KIMI_API_KEY
  • รูปแบบ: สคีมาการเติมข้อความแชทของ OpenAI (messages, tools, stream, etc.)
  • บริบท: 262,144 โทเค็นอินพุต, สูงสุด 98,304 โทเค็นเอาต์พุตสำหรับการให้เหตุผล
  • ค่าเริ่มต้น: temperature 1.0, top-p 1.0 (ตาม คำแนะนำอย่างเป็นทางการ)

คำสั่ง curl:

curl https://api.moonshot.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $KIMI_API_KEY" \
  -d '{
    "model": "kimi-k2.6",
    "messages": [{"role": "user", "content": "Write a Python function that reverses a string."}]
  }'
Enter fullscreen mode Exit fullscreen mode

รายละเอียดเพิ่มเติม เช่น Agent Swarm และขีดจำกัดการดำเนินการ 4,000 ขั้นตอน จะอธิบายในส่วนถัดไป

Kimi K2.6 API overview

สิ่งที่คุณสามารถทำได้จริงด้วย API นี้

จากการ ประกาศเปิดตัว Kimi K2.6, API นี้รองรับการ:

  • เอเจนต์เขียนโค้ด ที่รันได้นาน 12+ ชั่วโมง (เช่น inferencing Qwen3.5-0.8B Mac, >4,000 tool calls, อัตราส่งข้อมูลสูง)
  • จัดการโครงสร้างพื้นฐานอัตโนมัติ ในเซสชันหลายวัน พร้อม event response
  • รองรับหลายภาษา เช่น Rust, Go, Python, Zig
  • กลุ่มเอเจนต์ สูงสุด 300 ตัว ประสานงาน 4,000+ ขั้นตอน
  • การพัฒนาที่ขับเคลื่อนด้วยดีไซน์: ฟูลสแต็กแอป, ยืนยันตัวตน, ฐานข้อมูล, ธุรกรรม
  • ไปป์ไลน์ Vision + Python tools (MathVision: 93.2%)

หากคุณสร้างเครื่องมือแบบ Claude Code, สร้าง Claude Code เอง, หรือ Cursor Composer 2 — K2.6 API นำไปใช้แทนได้ทันที

ขั้นตอนที่ 1: รับคีย์ API

  1. ไปที่ platform.moonshot.ai หรือ platform.kimi.ai แล้วสมัคร (email หรือ Google OAuth)
  2. ยืนยันบัญชี (บางคนอาจต้องยืนยัน SMS)
  3. เพิ่มข้อมูลการเรียกเก็บเงิน — โดยปกติจะมีเครดิตฟรี
  4. ไปที่ API Keys แล้วกด Create Key
  5. คัดลอกคีย์ทันที (แสดงครั้งเดียว)
  6. ส่งออก:
export KIMI_API_KEY="sk-..."
Enter fullscreen mode Exit fullscreen mode

เพิ่มใน .zshrc, .bashrc หรือ secret manager — อย่า commit

ต้องการใช้งานฟรี? อ่าน วิธีใช้ Kimi K2.6 ฟรี (Cloudflare Workers AI, โฮสต์เอง, เครดิตฟรี)

ขั้นตอนที่ 2: เลือก SDK ของคุณ

API เข้ากันได้กับ OpenAI SDK! เพียงเปลี่ยน base_url ก็ใช้ได้ทันที

ตัวเลือก ติดตั้ง เหมาะสำหรับ
curl มีมาให้ในระบบ ทดสอบเร็ว, CI
OpenAI Python pip install openai Python service
OpenAI Node npm install openai JS/TS app

Python

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("KIMI_API_KEY"),
    base_url="https://api.moonshot.ai/v1",
)

response = client.chat.completions.create(
    model="kimi-k2.6",
    messages=[{"role": "user", "content": "What is the capital of France?"}],
)

print(response.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

Node.js

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.KIMI_API_KEY,
  baseURL: "https://api.moonshot.ai/v1",
});

const response = await client.chat.completions.create({
  model: "kimi-k2.6",
  messages: [{ role: "user", content: "What is the capital of France?" }],
});

console.log(response.choices[0].message.content);
Enter fullscreen mode Exit fullscreen mode

curl

curl https://api.moonshot.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $KIMI_API_KEY" \
  -d '{
    "model": "kimi-k2.6",
    "messages": [{"role": "user", "content": "What is the capital of France?"}]
  }'
Enter fullscreen mode Exit fullscreen mode

ขั้นตอนที่ 3: ทำความเข้าใจเนื้อหาคำขอ

ฟิลด์ตรงกับ OpenAI:

{
  "model": "kimi-k2.6",
  "messages": [
    { "role": "system", "content": "You are a helpful assistant." },
    { "role": "user", "content": "Your prompt here." }
  ],
  "temperature": 1.0,
  "top_p": 1.0,
  "max_tokens": 8192,
  "stream": false,
  "tools": [],
  "tool_choice": "auto",
  "thinking": { "type": "disabled" }
}
Enter fullscreen mode Exit fullscreen mode

ข้อควรทราบของ Moonshot:

  • ค่าเริ่มต้น temperature/top_p = 1.0 (ไม่ควรใช้ค่าต่ำแบบ OpenAI)
  • thinking ใช้กับ kimi-k2.6-thinking สำหรับ trace reasoning ({"type": "disabled"} ปิด reasoning)

ขั้นตอนที่ 4: การสตรีม

ใช้ stream: true เพื่ออ่านผลลัพธ์ทีละ chunk (เหมาะกับงานยาว):

Python

stream = client.chat.completions.create(
    model="kimi-k2.6",
    messages=[{"role": "user", "content": "Write a 500-word essay on MoE models."}],
    stream=True,
)

for chunk in stream:
    delta = chunk.choices[0].delta.content
    if delta:
        print(delta, end="", flush=True)
Enter fullscreen mode Exit fullscreen mode

Node.js

const stream = await client.chat.completions.create({
  model: "kimi-k2.6",
  messages: [{ role: "user", content: "Write a 500-word essay on MoE models." }],
  stream: true,
});

for await (const chunk of stream) {
  const delta = chunk.choices[0]?.delta?.content;
  if (delta) process.stdout.write(delta);
}
Enter fullscreen mode Exit fullscreen mode

สตรีมนี้รองรับทั้งข้อความและ tool call (JSON deltas)

ขั้นตอนที่ 5: การเรียกใช้เครื่องมือ

Moonshot รายงาน Toolathlon 50.0% และ ความสำเร็จ tool call 96.60% (ดู เวิร์กโฟลว์การทดสอบ API สำหรับ QA)

กำหนดเครื่องมือ:

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get the current weather in a location.",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {"type": "string", "description": "City name"},
                    "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
                },
                "required": ["location"]
            }
        }
    }
]
Enter fullscreen mode Exit fullscreen mode

การเรียกครั้งแรก (ให้โมเดลตัดสินใจ):

import json

messages = [{"role": "user", "content": "What's the weather in Tokyo?"}]

resp = client.chat.completions.create(
    model="kimi-k2.6",
    messages=messages,
    tools=tools,
    tool_choice="auto",
)

msg = resp.choices[0].message
messages.append(msg)

if msg.tool_calls:
    for call in msg.tool_calls:
        args = json.loads(call.function.arguments)
        result = fetch_weather(args["location"], args.get("unit", "celsius"))
        messages.append({
            "role": "tool",
            "tool_call_id": call.id,
            "content": json.dumps(result),
        })
Enter fullscreen mode Exit fullscreen mode

การเรียกครั้งที่สอง (คำตอบสุดท้าย):

final = client.chat.completions.create(
    model="kimi-k2.6",
    messages=messages,
    tools=tools,
)
print(final.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

K2.6 แข็งแกร่งเรื่อง tool chain หลายขั้นตอน — ดูตัวอย่าง Kimi Code และเปรียบเทียบกับ เวิร์กโฟลว์ Claude Code

ขั้นตอนที่ 6: การป้อนข้อมูลด้วยภาพ (Vision input)

ส่งรูปภาพใน messages แบบ OpenAI:

response = client.chat.completions.create(
    model="kimi-k2.6",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "Describe this image in one sentence."},
                {"type": "image_url", "image_url": {"url": "https://example.com/photo.jpg"}}
            ]
        }
    ],
)
Enter fullscreen mode Exit fullscreen mode

ไฟล์ในเครื่อง: base64 encode

import base64
with open("photo.jpg", "rb") as f:
    b64 = base64.b64encode(f.read()).decode("utf-8")

image_url = f"data:image/jpeg;base64,{b64}"
Enter fullscreen mode Exit fullscreen mode

OCR/ไดอะแกรม: ใส่ prompt เพิ่ม, คณิตศาสตร์: รวม Python interpreter (MathVision 93.2%)

ขั้นตอนที่ 7: การป้อนข้อมูลด้วยวิดีโอ (Video input)

ส่งวิดีโอผ่าน URL หรือเฟรม:

response = client.chat.completions.create(
    model="kimi-k2.6",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "Summarize what happens in this video."},
                {"type": "video_url", "video_url": {"url": "https://example.com/clip.mp4"}}
            ]
        }
    ],
)
Enter fullscreen mode Exit fullscreen mode

คลิปสั้น (<30 วินาที) ได้ในหนึ่งคำขอ; วิดีโอยาวควรใช้สตรีม

ขั้นตอนที่ 8: โหมดการคิด (Thinking mode)

kimi-k2.6-thinking จะคืนร่องรอย reasoning ใน response

เปิด reasoning (ค่าเริ่มต้น):

response = client.chat.completions.create(
    model="kimi-k2.6-thinking",
    messages=[{"role": "user", "content": "Prove sqrt(2) is irrational."}],
)
Enter fullscreen mode Exit fullscreen mode

ปิด reasoning:

response = client.chat.completions.create(
    model="kimi-k2.6-thinking",
    messages=[{"role": "user", "content": "Quick: what's 17 * 23?"}],
    extra_body={"thinking": {"type": "disabled"}},
)
Enter fullscreen mode Exit fullscreen mode

ร่องรอย reasoning อยู่ในฟิลด์ reasoning ของ response

ขั้นตอนที่ 9: Agent Swarm

Agent Swarm = 300 agents, 4,000+ steps, สเกลสูง

ตัวอย่างการเรียก:

response = client.chat.completions.create(
    model="kimi-k2.6",
    messages=[{
        "role": "user",
        "content": "Build a 5-page marketing site for a coffee brand with responsive design and a newsletter signup."
    }],
    extra_body={
        "agent": {
            "type": "swarm",
            "max_agents": 30,
            "max_steps": 4000
        }
    },
)
Enter fullscreen mode Exit fullscreen mode

Tips:

  1. ใช้ stream: true เพื่อ monitor/abort
  2. max_agents 10-30 ก็เพียงพอ
  3. ติดตาม usage ทุก response

อ้างอิง บล็อก Kimi

ขั้นตอนที่ 10: ทดสอบทุกอย่างด้วย Apidog

แบบฝึกหัดนี้ใช้ Apidog เพื่อสร้างคำขอ API แบบภาพ

Apidog Kimi API

ตั้งค่า Kimi K2.6 ใน Apidog

  1. ดาวน์โหลด Apidog และสร้างโปรเจกต์
  2. สร้าง environment kimi-prod ใส่ตัวแปร: BASE_URL = https://api.moonshot.ai/v1, KIMI_API_KEY = sk-...
  3. สร้าง API request: POST {{BASE_URL}}/chat/completions
  4. ใส่ header: Authorization: Bearer {{KIMI_API_KEY}}, Content-Type: application/json
  5. Body (ตัวอย่างสตรีม):
{
  "model": "kimi-k2.6",
  "messages": [{ "role": "user", "content": "Hello, Kimi K2.6!" }],
  "stream": true
}
Enter fullscreen mode Exit fullscreen mode
  1. กด Send ดู token ไหล realtime

สิ่งที่ Apidog เพิ่มเติมเข้ามา

  • Schema validation กับ OpenAI spec
  • Request history เล่นซ้ำคำขอที่ผิดปกติ
  • Environment switching (dev/staging/prod)
  • Team sharing ส่งออกโปรเจกต์ (ดูตัวอย่างการทดสอบ API สำหรับทีม 50 คน)
  • Mock server สำหรับกรณีเซิร์ฟเวอร์ Moonshot down
  • SSE stream support ที่รองรับรูปแบบสตรีมของ Kimi

Apidog ยังมี ส่วนขยาย VS Code และ คู่มือทดสอบ API โดยไม่ใช้ Postman

การจัดการข้อผิดพลาดที่ไม่สร้างปัญหา

Moonshot ใช้รหัส HTTP มาตรฐาน:

  • 400: ข้อมูลผิดพลาด
  • 401: ยืนยันตัวตนล้มเหลว
  • 429: โควต้า/อัตราจำกัด
  • 500: เซิร์ฟเวอร์ผิดพลาด (ใช้ exponential backoff)
  • 529: Overloaded

ตัวอย่าง retry:

import time
from openai import OpenAI, RateLimitError, APIError

def call_kimi(messages, max_retries=5):
    for attempt in range(max_retries):
        try:
            return client.chat.completions.create(
                model="kimi-k2.6",
                messages=messages,
            )
        except RateLimitError:
            time.sleep(2 ** attempt)
        except APIError as e:
            if e.status_code >= 500 and attempt < max_retries - 1:
                time.sleep(2 ** attempt)
            else:
                raise
    raise RuntimeError("Kimi K2.6 failed after retries")
Enter fullscreen mode Exit fullscreen mode

กลางการสตรีมหาก disconnect ให้ resume โดยใส่ "ดำเนินการต่อจากตรงนี้" ใน prompt

การควบคุมต้นทุน

ดูราคาที่ kimi.com/membership/pricing

  • จำกัด max_tokens กำหนดค่าต่ำสุดที่จำเป็น
  • แคช system prompt เพื่อประหยัดโทเค็น
  • บันทึก usage ทุก response (ส่งไป Prometheus หรือ metric ที่ใช้)

รูปแบบการผลิต: ตัวแก้ไขปัญหา GitHub

โค้ดตัวอย่างเอเจนต์แก้ปัญหา GitHub (อ่าน issue, ค้นหาโค้ด, เสนอ fix, รัน test):

from openai import OpenAI
import os, json

client = OpenAI(
    api_key=os.getenv("KIMI_API_KEY"),
    base_url="https://api.moonshot.ai/v1",
)

tools = [
    {"type": "function", "function": {
        "name": "read_file",
        "description": "Read a file in the repo.",
        "parameters": {
            "type": "object",
            "properties": {"path": {"type": "string"}},
            "required": ["path"]
        }
    }},
    {"type": "function", "function": {
        "name": "search_code",
        "description": "Ripgrep the codebase for a pattern.",
        "parameters": {
            "type": "object",
            "properties": {"query": {"type": "string"}},
            "required": ["query"]
        }
    }},
    {"type": "function", "function": {
        "name": "run_tests",
        "description": "Run the project test suite.",
        "parameters": {"type": "object", "properties": {}}
    }},
]

def tool_dispatch(name, args):
    if name == "read_file":
        with open(args["path"]) as f:
            return f.read()
    if name == "search_code":
        return run_ripgrep(args["query"])
    if name == "run_tests":
        return run_pytest()
    raise ValueError(f"Unknown tool: {name}")

messages = [
    {"role": "system", "content": "You are a senior engineer. Fix the described bug."},
    {"role": "user", "content": "Issue: login form submits twice on slow networks."}
]

while True:
    resp = client.chat.completions.create(
        model="kimi-k2.6",
        messages=messages,
        tools=tools,
    )
    msg = resp.choices[0].message
    messages.append(msg)

    if not msg.tool_calls:
        print(msg.content)
        break

    for call in msg.tool_calls:
        result = tool_dispatch(call.function.name, json.loads(call.function.arguments))
        messages.append({
            "role": "tool",
            "tool_call_id": call.id,
            "content": result,
        })
Enter fullscreen mode Exit fullscreen mode

สามารถต่อยอดกับ Swarm (เพิ่มใน extra_body) และ Hermes multi-agent stack ได้

คำถามที่พบบ่อย

ฉันต้องใช้ SDK ของ Moonshot ไหม?

ไม่จำเป็น OpenAI SDK ใช้ได้ทันที

API จำกัดอัตราไหม?

มี — ดู quota/limit ใน dashboard

Kimi K2.6 ใช้กับ LangChain, LlamaIndex, Vercel AI SDK ได้ไหม?

ได้ ถ้า framework รองรับ base_url

Kimi K2.6 รองรับ JSON mode ไหม?

รองรับ (ส่ง response_format: {"type": "json_object"})

Context window เท่าไร?

262,144 input, 98,304 output tokens (ref)

ปรับแต่ง Kimi K2.6 ผ่าน API ได้ไหม?

ยัง — ปัจจุบันปรับแต่งต้องรัน น้ำหนักโอเพ่นซอร์สเอง

kimi-k2.6 vs kimi-k2.6-thinking ต่างกันอย่างไร?

kimi-k2.6 เร็ว, kimi-k2.6-thinking คืน reasoning, เหมาะกับคณิตศาสตร์/ตรรกะ

มีแพ็กเกจฟรีไหม?

ดู คู่มือใช้ฟรี

สรุป

Kimi K2.6 API ใช้กับ OpenAI-compatible toolset ได้ทันที เพียงเปลี่ยน Base URL และ API Key

คุณจะได้ context 262K, Agent Swarm 300 agents, ความสำเร็จ tool call 96.60% และโอเพ่นซอร์ส model

สร้าง integration ใหม่? ใช้ Apidog เพื่อทดสอบ endpoint, จับ schema error, bug ใน stream, หรือ auth ปัญหา ก่อนโค้ดจริง แล้วนำ request ที่เวิร์กไปใช้กับ Python/Node ได้มั่นใจ

ข้อมูลอ้างอิงและอ่านเพิ่มเติม

Top comments (0)