This guide shows developers how to call Kimi K2.7 Code, Moonshot AI's coding model, from their own software. It covers the model names, a first request you can copy, the settings that cause errors, tool calls, prices and fixes for common problems. Everything here follows Moonshot's own documentation.
An illustration of the request and reply. The text shown is an example, not real output.
kimi-k2.7-codekimi-k2.7-code-highspeedhttps://api.moonshot.ai/v1max_tokens)From Moonshot's Kimi K2.7 Code quickstart, models page and price page. Prices exclude tax.
MOONSHOT_API_KEY. Do not paste it into your code.pip install --upgrade "openai>=1.0". For Node, run npm install openai.Need more detail on keys and accounts? See Kimi API setup.
All three examples do the same thing: ask Kimi K2.7 Code to write a small function and print its answer.
curl https://api.moonshot.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $MOONSHOT_API_KEY" \
-d '{
"model": "kimi-k2.7-code",
"max_tokens": 4096,
"messages": [
{"role": "user", "content": "Write a Python function that checks if a number is prime."}
]
}'import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["MOONSHOT_API_KEY"],
base_url="https://api.moonshot.ai/v1",
)
response = client.chat.completions.create(
model="kimi-k2.7-code",
max_tokens=4096,
messages=[
{"role": "user", "content": "Write a Python function that checks if a number is prime."}
],
)
print(response.choices[0].message.content)import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.MOONSHOT_API_KEY,
baseURL: "https://api.moonshot.ai/v1",
});
const response = await client.chat.completions.create({
model: "kimi-k2.7-code",
max_tokens: 4096,
messages: [
{ role: "user", content: "Write a Python function that checks if a number is prime." },
],
});
console.log(response.choices[0].message.content);The Python example follows Moonshot's quickstart. The curl and Node versions use the same request in the standard OpenAI format. Notice what is missing: no temperature setting.
Because the model always thinks first, each reply has two parts.
reasoning_contentcontentmax_tokens with thinking in mind. Thinking and the answer share the same limit. If the limit is too low, the model can run out before it finishes answering. The default is 32,768.This is the most common source of errors. Moonshot fixes several settings for this model. Sending any other value returns an error, so the simplest approach is to leave them out.
| Setting | Fixed at | What to do |
|---|---|---|
temperature | 1.0 | Leave it out |
top_p | 0.95 | Leave it out |
n | 1 | Leave it out. One answer per request |
presence_penalty | 0 | Leave it out |
frequency_penalty | 0 | Leave it out |
thinking | Enabled | Do not disable it |
tool_choice | auto or none | Do not use required or a named tool |
On a small screen, swipe the table sideways to see every column.
Many coding tools and libraries send a temperature by default. If yours does, look for a way to remove it. Need thinking switched off for fast, simple calls? Use kimi-k2.6 for those.
Tools are how a coding model does real work: reading files, running tests, searching a project. You describe each tool, the model asks to use one, your code runs it and sends the result back.
reasoning_content. Moonshot says leaving the thinking out will not cause an error but can make later steps worse.tool message for each call, with the matching tool_call_id.import json, os
from openai import OpenAI
client = OpenAI(api_key=os.environ["MOONSHOT_API_KEY"],
base_url="https://api.moonshot.ai/v1")
tools = [{
"type": "function",
"function": {
"name": "read_file",
"description": "Read a text file from the project",
"parameters": {
"type": "object",
"properties": {"path": {"type": "string"}},
"required": ["path"],
},
},
}]
def read_file(path):
with open(path, encoding="utf-8") as f:
return f.read()
messages = [{"role": "user", "content": "Explain what main.py does."}]
while True:
response = client.chat.completions.create(
model="kimi-k2.7-code",
messages=messages,
tools=tools,
tool_choice="auto",
max_tokens=8192,
)
message = response.choices[0].message
# Keep the whole assistant turn, including its thinking
messages.append(message.model_dump(exclude_none=True))
if not message.tool_calls:
print(message.content)
break
for call in message.tool_calls:
args = json.loads(call.function.arguments)
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": read_file(**args),
})A teaching example written for this guide, based on the pattern in Moonshot's quickstart. In real use, limit which files a tool may read and add a cap on the number of loops.
Kimi K2.7 Code can look at a screenshot or mockup and write code from it. Send the picture inside the message as base64 text.
{
"role": "user",
"content": [
{"type": "image_url",
"image_url": {"url": "data:image/png;base64,PASTE_BASE64_HERE"}},
{"type": "text", "text": "Write the HTML and CSS for this screen."}
]
}No code needed for this in the app: see Visual Coding and Design to Code.
To use the faster version, change one thing: the model name.
About 180 tokens a second, and up to 260 in short conversations. It is the same model, so answers are the same quality.
Double: $1.90 in, $0.38 cached and $8.00 out per million tokens.
Moonshot says capacity is limited, so speed can vary while it adds more.
More: K2.7 Code HighSpeed.
You do not have to write your own agent. Moonshot documents how to connect its API to several existing coding tools, including Claude Code, OpenCode and its own Kimi Code CLI.
Claude Code talks to Moonshot's Anthropic-style address. Put these values in the env part of ~/.claude/settings.json, then restart it.
ANTHROPIC_API_KEY value, which can clash.{
"env": {
"ANTHROPIC_BASE_URL": "https://api.moonshot.ai/anthropic",
"ANTHROPIC_AUTH_TOKEN": "YOUR_MOONSHOT_API_KEY",
"ANTHROPIC_MODEL": "kimi-k2.7-code",
"ANTHROPIC_DEFAULT_OPUS_MODEL": "kimi-k2.7-code",
"ANTHROPIC_DEFAULT_SONNET_MODEL": "kimi-k2.7-code",
"ANTHROPIC_DEFAULT_HAIKU_MODEL": "kimi-k2.7-code",
"ANTHROPIC_DEFAULT_FABLE_MODEL": "kimi-k2.7-code",
"CLAUDE_CODE_SUBAGENT_MODEL": "kimi-k2.7-code"
}
}Adapted from Moonshot's Claude Code guide. Moonshot's own example uses kimi-k3 for most lines and kimi-k2.7-code for the lightest one. This version uses kimi-k2.7-code throughout to keep costs low.
Prefer Moonshot's own tool? See Kimi Code setup. Paying monthly instead of per token? See API vs subscription.
| Per million tokens | Input | Cached input | Output |
|---|---|---|---|
| Standard | $0.95 | $0.19 | $4.00 |
| HighSpeed | $1.90 | $0.38 | $8.00 |
| Batch | $0.57 | $0.114 | $2.40 |
On a small screen, swipe the table sideways to see every column.
Estimate a monthly bill with the calculator on the Kimi K2.7 Code page. More: API pricing · rate limits and enterprise pricing
| Code | What it means | What to do |
|---|---|---|
| 400 | A setting is not allowed | Remove temperature, top_p, n and the penalty settings. Do not turn thinking off. Use tool_choice of auto or none only |
| 400 | The request is too long | Shorten the input or lower max_tokens. Input and output together must fit in 262,144 tokens |
| 401 | The key is wrong or missing | Check the Authorization: Bearer header. A Kimi Code membership key will not work on this address |
| 404 | Model not found | Check the spelling: kimi-k2.7-code. Older names such as kimi-k2.5 are retired |
| 429 | Rate limit reached | Slow down or wait. New accounts are limited to 3 requests a minute until total top-ups reach $10 |
| 429 | Balance too low | Top up your balance on the platform |
| 429 | Servers are busy | Wait and retry with growing pauses. Topping up does not help with this one |
| 504 | Timed out | Turn on streaming with "stream": true for long answers |
On a small screen, swipe the table sideways to see every column.
From Moonshot's error code page and Kimi K2.7 Code quickstart. More help: Kimi not working?