Kimi K2 API: The Complete Integration Guide for AI Engineers (2026)
Everything you need to integrate Moonshot AI's Kimi model family — K2.6, K2.7 Code, and K2.5. We cover signup, API key, first call in 3 minutes, thinking mode, streaming, tool calls, multimodal input, pricing, common errors, and how to route Kimi models through TheRouter.
The Kimi K2 API from Moonshot AI gives you OpenAI-compatible access to a family of 1T-parameter MoE models — from the general-purpose K2.6 flagship to the code-focused K2.7 Code released on June 12, 2026. All models support 256K-token context, thinking mode, tool calls, and streaming. You sign up at platform.kimi.ai, grab an API key, point your OpenAI SDK at https://api.moonshot.ai/v1, and your existing code works.
We wrote this guide because the Kimi lineup has grown fast — five models currently routed through TheRouter, plus K2.7 Code available via the Moonshot API directly — and the differences between them (thinking-only vs. toggle, multimodal vs. text-only, deprecated K2 series) can trip you up if you're not paying attention. This is the single reference we wanted when we first integrated Moonshot into our routing layer.
Sources: Kimi API Model List, retrieved 2026-06-14; Kimi API Overview, retrieved 2026-06-14.
Getting Started in 3 Minutes
Step 1: Create an account. Go to platform.kimi.ai and sign up. Moonshot supports email and phone number registration.
Step 2: Get your API key. Navigate to the Console → API Keys page and create a new key. Copy it immediately — keys are shown once.
Step 3: Make your first call. Install the OpenAI Python SDK if you haven't already:
pip install --upgrade openai
Then run:
from openai import OpenAI
client = OpenAI(
api_key="YOUR_MOONSHOT_KEY",
base_url="https://api.moonshot.ai/v1",
)
response = client.chat.completions.create(
model="kimi-k2.6",
messages=[{"role": "user", "content": "What is Kimi K2.6?"}],
)
print(response.choices[0].message.content)
That's it. The Kimi API implements the OpenAI ChatCompletions specification — any tool, SDK, or code that speaks OpenAI's format works by changing the base_url and API key.
Source: Kimi K2.6 Quick Start, retrieved 2026-06-14.
The Kimi Model Lineup: Which Model to Pick
Moonshot AI currently offers three active model families via the Kimi API. The legacy K2 series (K2, K2 Instruct, K2 Thinking) was deprecated on May 25, 2026.
| Model | Model ID | Context | Modality | Thinking | Best For |
|---|---|---|---|---|---|
| K2.7 Code | kimi-k2.7-code | 256K | text+image→text | Always on (cannot disable) | Coding agents, long-horizon code generation |
| K2.6 | kimi-k2.6 | 256K | text+image→text | On by default (can disable) | General-purpose, multimodal, agent tasks |
| K2.5 | kimi-k2.5 | 256K | text+image→text | On by default (can disable) | General-purpose (previous generation) |
Deprecated Models (May 25, 2026)
| Legacy ID | Status |
|---|---|
kimi-k2-0905-preview | Discontinued |
kimi-k2-0711-preview | Discontinued |
kimi-k2-turbo-preview | Discontinued |
kimi-k2-thinking | Discontinued |
kimi-k2-thinking-turbo | Discontinued |
If your code still references any kimi-k2-*-preview or kimi-k2-thinking IDs, switch to kimi-k2.6 now. These models are no longer maintained or supported.
Source: Kimi API Model List, retrieved 2026-06-14.
K2.7 Code: The Coding Specialist (Always-Thinking)
K2.7 Code was released on June 12, 2026, and is Moonshot's strongest coding model. The critical difference from K2.6: thinking is always on and cannot be disabled. You don't need to (and should not) pass the thinking parameter — just set the model to kimi-k2.7-code and the model always emits reasoning_content.
Additionally, K2.7 Code has Preserved Thinking always enabled — in multi-turn conversations, you must keep the reasoning_content from every historical assistant message in your messages array as-is.
K2.7 Code Example
from openai import OpenAI
client = OpenAI(
api_key="YOUR_MOONSHOT_KEY",
base_url="https://api.moonshot.ai/v1",
)
stream = client.chat.completions.create(
model="kimi-k2.7-code",
messages=[
{"role": "system", "content": "You are Kimi."},
{"role": "user", "content": "Implement quicksort in Python."},
],
max_tokens=32768,
stream=True,
)
for chunk in stream:
if chunk.choices:
choice = chunk.choices[0]
delta = choice.delta
if hasattr(delta, "reasoning_content") and delta.reasoning_content:
print(delta.reasoning_content, end="")
if delta.content:
print(delta.content, end="")
Cost Implications
K2.7 Code always produces thinking tokens, which are billed at the output rate ($4.00/1M). For simple tasks where you don't need deep reasoning, use K2.6 with thinking disabled to save costs.
Source: Using Thinking Models — Kimi API, retrieved 2026-06-14; K2.7 Code Pricing, retrieved 2026-06-14.
Thinking Mode: How It Works Across Models
The thinking parameter behaves differently across the three active models:
| Parameter | kimi-k2.7-code | kimi-k2.6 | kimi-k2.5 |
|---|---|---|---|
thinking.type | Only "enabled" (errors on "disabled") | "enabled" (default) or "disabled" | "enabled" (default) or "disabled" |
thinking.keep (Preserved Thinking) | Always "all" (cannot turn off) | null (default) or "all" | Not supported |
Enable/Disable Thinking on K2.6
# Thinking enabled (default — no need to pass the parameter)
response = client.chat.completions.create(
model="kimi-k2.6",
messages=[{"role": "user", "content": "Prove that sqrt(2) is irrational."}],
)
# Thinking disabled explicitly
response = client.chat.completions.create(
model="kimi-k2.6",
messages=[{"role": "user", "content": "What time is it in Tokyo?"}],
extra_body={"thinking": {"type": "disabled"}},
)
When thinking is enabled, the model returns a reasoning_content field in assistant messages alongside the regular content. In streaming mode, you'll receive reasoning_content chunks before content chunks.
Preserved Thinking (K2.6 and K2.7 Code)
Preserved Thinking means the model expects to see its own reasoning history in multi-turn conversations. When enabled on K2.6 (or always-on for K2.7 Code), you must pass back the reasoning_content from previous assistant messages:
response = client.chat.completions.create(
model="kimi-k2.6",
messages=[
{"role": "user", "content": "Write a binary search function."},
{
"role": "assistant",
"reasoning_content": "<the model's previous thinking>",
"content": "Here's a binary search implementation..."
},
{"role": "user", "content": "Now add error handling."},
],
extra_body={"thinking": {"type": "enabled", "keep": "all"}},
)
Source: Using Thinking Models — Kimi API, retrieved 2026-06-14.
Multimodal Input: Images and Video
K2.6, K2.5, and K2.7 Code all support image input alongside text. K2.6 additionally supports video input. Pass images using the standard OpenAI image_url content type:
response = client.chat.completions.create(
model="kimi-k2.6",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What is in this image?"},
{
"type": "image_url",
"image_url": {"url": "https://example.com/photo.jpg"},
},
],
}
],
)
This follows the OpenAI vision message format, so if you're already sending images to GPT-4o or Claude, the same code structure works here.
Source: Kimi K2.6 Quick Start, retrieved 2026-06-14.
Code Examples: curl, Python, Streaming, and Tool Use
curl
curl https://api.moonshot.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${MOONSHOT_API_KEY}" \
-d '{
"model": "kimi-k2.6",
"messages": [{"role": "user", "content": "Hello!"}],
"stream": false
}'
Python Streaming
from openai import OpenAI
client = OpenAI(
api_key="YOUR_MOONSHOT_KEY",
base_url="https://api.moonshot.ai/v1",
)
stream = client.chat.completions.create(
model="kimi-k2.6",
messages=[{"role": "user", "content": "Explain the CAP theorem."}],
stream=True,
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
Tool Calls
All active Kimi models support tool/function calling in the OpenAI format:
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"},
},
"required": ["location"],
},
},
}
]
response = client.chat.completions.create(
model="kimi-k2.6",
messages=[{"role": "user", "content": "What's the weather in Beijing?"}],
tools=tools,
tool_choice="auto",
)
K2.6 and K2.7 Code are particularly strong at multi-step tool orchestration — K2.6's description highlights stable 200–300 step tool orchestration, and K2.7 Code is optimized for long-horizon coding with tool-augmented reasoning.
Source: Kimi API Tool Use Documentation, retrieved 2026-06-14.
Pricing: Moonshot Direct vs. DashScope-Hosted
Moonshot Direct API (platform.kimi.ai)
| Model | Input (Cache Miss) | Input (Cache Hit) | Output | Context |
|---|---|---|---|---|
kimi-k2.7-code | $0.95/1M | $0.19/1M | $4.00/1M | 256K |
kimi-k2.6 | $0.95/1M | $0.16/1M | $4.00/1M | 256K |
kimi-k2.5 | $0.60/1M | $0.10/1M | $3.00/1M | 256K |
Moonshot's automatic context caching means repeated system prompts and shared prefixes hit the cache rate automatically — no configuration needed.
DashScope-Hosted (Aliyun Bailian)
Kimi K2.6 and K2.5 are also available through Alibaba Cloud's DashScope/Bailian platform. This gives you an alternative endpoint with Aliyun's infrastructure. The DashScope endpoint is OpenAI-compatible:
client = OpenAI(
api_key="YOUR_DASHSCOPE_KEY",
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
response = client.chat.completions.create(
model="kimi-k2.6",
messages=[{"role": "user", "content": "Hello!"}],
)
Note: on DashScope, thinking mode is controlled via enable_thinking parameter rather than the thinking object used by the Moonshot direct API. Check the DashScope Kimi docs for the exact parameter format.
Sources: K2.6 Pricing, retrieved 2026-06-14; K2.7 Code Pricing, retrieved 2026-06-14; DashScope Kimi API, retrieved 2026-06-14.
Common Errors and Fixes
Wrong base_url
Moonshot's API base is https://api.moonshot.ai/v1. Do not use https://platform.moonshot.ai (that's the console) or https://platform.kimi.ai (that's the docs site).
Passing thinking.type: "disabled" to K2.7 Code
K2.7 Code does not support disabling thinking. The API will return an error. If you need non-thinking mode, use K2.6 instead.
Model ID Confusion Across Providers
The model ID for K2.6 on the Moonshot direct API is kimi-k2.6. On DashScope, it's also kimi-k2.6. However, if you're using DashScope in Cursor or certain IDE integrations, the dot notation needs adjustment — Cursor requires kimi-k2-6 (with a hyphen). When routing through TheRouter, we use the canonical moonshot/kimi-k2.6 identifier.
Deprecated Model IDs
If you get "model not found" errors, check whether you're using a deprecated K2 series ID (kimi-k2-0905-preview, kimi-k2-thinking, etc.). These were discontinued on May 25, 2026.
Preserved Thinking Errors in Multi-Turn
When using K2.7 Code (or K2.6 with Preserved Thinking enabled), you must pass back the reasoning_content from all previous assistant messages. Omitting it causes degraded responses or errors in multi-turn conversations.
Source: Kimi API Model List, retrieved 2026-06-14.
TheRouter Integration: Route and Fallback
We route five Kimi models through TheRouter:
moonshot/kimi-k2— fast non-thinking variant (131K context) (deprecated — migrate to kimi-k2.5/k2.6)moonshot/kimi-k2-instruct— 1T MoE for conversation and tool use (200K context) (deprecated — migrate to kimi-k2.5/k2.6)moonshot/kimi-k2-thinking— deep reasoning specialist (256K context) (deprecated — migrate to kimi-k2.5/k2.6)moonshot/kimi-k2.5— previous-gen flagship (256K context)moonshot/kimi-k2.6— current flagship, multimodal (256K context)
To use Kimi through TheRouter, point your OpenAI SDK at our endpoint and use the moonshot/ prefixed model ID:
from openai import OpenAI
client = OpenAI(
api_key="YOUR_THEROUTER_KEY",
base_url="https://api.therouter.ai/v1",
)
response = client.chat.completions.create(
model="moonshot/kimi-k2.6",
messages=[{"role": "user", "content": "Hello from TheRouter!"}],
)
Fallback Configuration
One advantage of routing through TheRouter is automatic fallback. If Moonshot's API is down or rate-limited, we can fail over to another provider hosting the same model. Since K2.6 is also available through DashScope, you get geographic redundancy without writing any retry logic yourself.
OpenAI-compatible means a provider exposes a chat-completions endpoint whose request and response shape matches the OpenAI API contract closely enough that an unmodified OpenAI SDK call works against it after swapping three values: API key, base URL, and model name. The minimum surface in practice is POST /v1/chat/completions with messages, model, and an OpenAI-shaped streaming response.
Note: K2.7 Code (kimi-k2.7-code) is not yet available as a TheRouter route — it's accessible only via the Moonshot direct API at this time. We expect to add it once availability stabilizes across multiple providers.
Source: TheRouter Models Data, retrieved 2026-06-14.
Production Checklist
Before shipping Kimi models to production:
- Migrate off deprecated K2 series — if using any
kimi-k2-*-previeworkimi-k2-thinkingIDs, switch tokimi-k2.6 - Choose your thinking mode — set
thinking.typeexplicitly on K2.6; for K2.7 Code, thinking is always on - Handle
reasoning_contentin multi-turn — if using K2.7 Code or K2.6 with Preserved Thinking, pass back reasoning history - Pick the right model for the job — K2.7 Code for coding agents, K2.6 for general + multimodal, K2.5 if you need lower cost
- Monitor thinking token costs — thinking tokens are billed at output rates; disable thinking on K2.6 for simple tasks to save money
- Set up a fallback — route through TheRouter or DashScope to avoid single-provider dependency
- Use context caching — Moonshot's automatic prefix caching makes repeated system prompts cheap ($0.16/1M cache hit on K2.6)
- Set timeouts — thinking-heavy requests can take time; set a reasonable timeout (60–120s)
- Test model ID compatibility — verify your model ID works with your specific SDK/tool (watch for dot vs. hyphen in IDE integrations)
FAQ
Q: Is the Kimi K2 API OpenAI-compatible?
A: Yes. The Moonshot API implements the OpenAI ChatCompletions specification at https://api.moonshot.ai/v1. Change the base_url and api_key in your OpenAI SDK and everything works — including streaming, tool calls, and system messages.
Q: Can I disable thinking on K2.7 Code?
A: No. K2.7 Code has thinking always enabled and will return an error if you pass thinking.type: "disabled". If you need non-thinking mode for cost-sensitive workloads, use K2.6 with thinking disabled.
Q: What happened to the original K2 models?
A: The kimi-k2 series (K2, K2 Instruct, K2 Thinking, K2 Turbo) was deprecated on May 25, 2026. If you're still using these model IDs, switch to kimi-k2.6 for general use or kimi-k2.7-code for coding tasks.
Q: How does Kimi K2.6 compare to DeepSeek V4? A: Both are 1M+ context MoE models with thinking mode support. K2.6 has native multimodal (image + video input) and excels at agent/tool orchestration. DeepSeek V4 has a larger context window (1M vs. 256K) and lower pricing on cache hits. The best choice depends on your workload — and with TheRouter, you can route between them based on the task.
Q: Can I run Kimi K2.6 locally? A: Yes. Kimi K2.6 weights are available on HuggingFace as open-weight. However, with 1T total parameters (32B active), you'll need substantial GPU resources. For most production use cases, the API is more practical.
Q: Is K2.7 Code available through TheRouter? A: Not yet. K2.7 Code was released on June 12, 2026, and is currently available only through the Moonshot direct API. We plan to add routing support as provider availability expands.
Last updated: June 14, 2026. Pricing and model availability sourced from Kimi API Pricing and Kimi API Model List. All pricing may change — verify current rates at platform.kimi.ai before budgeting.