Responses API
创建一次模型响应。传入文本或图片,生成文本或 JSON 输出;也可以让模型调用你定义的函数工具,或使用服务端执行的联网搜索。
curl --request POST \
--url https://api.moonshot.cn/v1/responses \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "kimi-k3",
"input": "<string>",
"text": {},
"tools": [
{
"type": "function",
"name": "<string>",
"description": "<string>",
"parameters": {},
"strict": true
}
]
}
'import requests
url = "https://api.moonshot.cn/v1/responses"
payload = {
"model": "kimi-k3",
"input": "<string>",
"text": {},
"tools": [
{
"type": "function",
"name": "<string>",
"description": "<string>",
"parameters": {},
"strict": True
}
]
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: 'kimi-k3',
input: '<string>',
text: {},
tools: [
{
type: 'function',
name: '<string>',
description: '<string>',
parameters: {},
strict: true
}
]
})
};
fetch('https://api.moonshot.cn/v1/responses', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.moonshot.cn/v1/responses",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => 'kimi-k3',
'input' => '<string>',
'text' => [
],
'tools' => [
[
'type' => 'function',
'name' => '<string>',
'description' => '<string>',
'parameters' => [
],
'strict' => true
]
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.moonshot.cn/v1/responses"
payload := strings.NewReader("{\n \"model\": \"kimi-k3\",\n \"input\": \"<string>\",\n \"text\": {},\n \"tools\": [\n {\n \"type\": \"function\",\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"parameters\": {},\n \"strict\": true\n }\n ]\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.moonshot.cn/v1/responses")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"kimi-k3\",\n \"input\": \"<string>\",\n \"text\": {},\n \"tools\": [\n {\n \"type\": \"function\",\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"parameters\": {},\n \"strict\": true\n }\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.moonshot.cn/v1/responses")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"kimi-k3\",\n \"input\": \"<string>\",\n \"text\": {},\n \"tools\": [\n {\n \"type\": \"function\",\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"parameters\": {},\n \"strict\": true\n }\n ]\n}"
response = http.request(request)
puts response.read_body{
"id": "resp_68f0c1c2d3e4f5a6b7c8d9e0",
"object": "response",
"created_at": 123,
"completed_at": 123,
"status": "in_progress",
"model": "<string>",
"output": [
{
"type": "reasoning",
"id": "rs_68f0c1c2d3e4f5a6b7c8d9e0",
"summary": [
{
"type": "summary_text",
"text": "<string>"
}
],
"encrypted_content": "<string>",
"status": "in_progress"
}
],
"usage": {
"input_tokens": 123,
"input_tokens_details": {
"cached_tokens": 123,
"cache_write_tokens": 123
},
"output_tokens": 123,
"output_tokens_details": {
"reasoning_tokens": 123
},
"total_tokens": 123
},
"incomplete_details": {
"reason": "max_output_tokens"
},
"error": {
"code": "<string>",
"message": "<string>"
},
"instructions": "<string>",
"reasoning": {},
"text": {},
"tools": [
{
"type": "function",
"name": "<string>",
"description": "<string>",
"parameters": {},
"strict": true
}
],
"tool_choice": "auto",
"max_output_tokens": 123,
"temperature": 123,
"top_p": 123,
"metadata": {},
"parallel_tool_calls": true,
"service_tier": "<string>",
"store": true,
"background": true,
"previous_response_id": "<string>",
"conversation": {},
"prompt_cache_options": {
"mode": "implicit",
"ttl": "5m"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}调用示例
调用示例
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["MOONSHOT_API_KEY"],
base_url="https://api.moonshot.cn/v1",
)
response = client.responses.create(
model="kimi-k3",
instructions="你是 Kimi,一个由 Moonshot AI 提供的人工智能助手。",
input="用一句话解释什么是上下文缓存。",
)
print(response.output_text)
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.MOONSHOT_API_KEY,
baseURL: "https://api.moonshot.cn/v1",
});
const response = await client.responses.create({
model: "kimi-k3",
instructions: "你是 Kimi,一个由 Moonshot AI 提供的人工智能助手。",
input: "用一句话解释什么是上下文缓存。",
});
console.log(response.output_text);
curl https://api.moonshot.cn/v1/responses \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $MOONSHOT_API_KEY" \
--data '{
"model": "kimi-k3",
"instructions": "你是 Kimi,一个由 Moonshot AI 提供的人工智能助手。",
"input": "用一句话解释什么是上下文缓存。"
}'
工具调用 Tool Use
工具调用 Tool Use
tools 支持四种工具类型:function、namespace、custom(仅 apply_patch)与 web_search,其他类型不支持。前三种由你在本地执行:模型返回调用请求,你执行后把结果回传;web_search 由服务端执行,无需你处理。函数调用通过 tools 传入 JSON Schema 定义的函数,模型可决定在适当时机调用它们:{
"model": "kimi-k3",
"input": "北京今天天气怎么样?",
"tools": [
{
"type": "function",
"name": "get_weather",
"description": "获取指定城市的天气",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "城市名称"}
},
"required": ["city"]
}
}
]
}
output 中会包含 function_call item,arguments 是参数的 JSON 字符串:{
"type": "function_call",
"id": "fc_IrcmDq5JNO0JhWdEpsuZnL9J",
"status": "completed",
"call_id": "get_weather_0",
"name": "get_weather",
"arguments": "{\"city\":\"北京\"}"
}
output 原样追加到 input,再追加一条 function_call_output(call_id 必须与 function_call 中的一致),发起下一次请求:{
"model": "kimi-k3",
"input": [
{"type": "message", "role": "user", "content": "北京今天天气怎么样?"},
{"type": "function_call", "call_id": "get_weather_0", "name": "get_weather", "arguments": "{\"city\":\"北京\"}"},
{"type": "function_call_output", "call_id": "get_weather_0", "output": "晴,25°C"}
],
"tools": [
{
"type": "function",
"name": "get_weather",
"description": "获取指定城市的天气",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "城市名称"}
},
"required": ["city"]
}
}
]
}
tools 中加入 {"type": "web_search"},服务端会先根据输入判断是否需要搜索,需要时执行搜索并把结果注入模型上下文,模型基于搜索结果作答:{
"model": "kimi-k3",
"input": "今天有什么重大新闻?",
"tools": [
{"type": "web_search"}
]
}
output 最前面会多出一个 web_search_call item。在 include 中加入 web_search_call.action.sources 可以拿到搜索命中的网页来源:{
"type": "web_search_call",
"id": "ws_dackjbcdo9rs73fo3oig",
"status": "completed",
"action": {
"type": "search",
"query": "2026年9月3日 重大新闻",
"sources": [
{"type": "url", "url": "https://example.com/news/1", "title": "新闻标题"}
]
}
}
上下文缓存(Cache Write)
上下文缓存(Cache Write)
prompt_cache_options 可以控制缓存写入行为:| 参数 | 类型 | 说明 |
|---|---|---|
prompt_cache_options | object | 上下文缓存写入选项。不传时默认开启缓存写入(5m 档) |
prompt_cache_options.mode | "implicit" | 缓存写入模式,仅支持 implicit:自动将请求前缀写入缓存 |
prompt_cache_options.ttl | "5m" | "1h" | 缓存写入的有效期,仅支持 5m、1h 两档(默认 5m),两档缓存相互独立 |
{
"model": "kimi-k3",
"input": "你好",
"prompt_cache_options": { "mode": "implicit", "ttl": "1h" }
}
prompt_cache_options 时,默认自动开启缓存写入(5m 档)。响应顶层会回显服务端实际应用的 prompt_cache_options,以其返回为准。行为说明- 缓存以组织(org)为粒度隔离,组织之间不共享缓存。
- 相同前缀的请求在缓存有效期内命中后,缓存的有效期会按原 TTL 刷新;命中部分只收取缓存读取费用,不再收取缓存写入费用。
- 缓存写入按 TTL 分别计费,价格详见产品定价。
- 不支持手动清除缓存,已缓存的前缀在至少 5 分钟不活动后自动过期。
- 暂不支持显式缓存断点:
content中出现prompt_cache_breakpoint时请求会被拒绝(HTTP 400)。
usage.input_tokens 始终为总输入 Token 数,其内部分类通过 input_tokens_details 返回:| 字段 | 说明 |
|---|---|
usage.input_tokens_details.cached_tokens | 命中缓存的 Token 数量(缓存读取) |
usage.input_tokens_details.cache_write_tokens | 本次写入缓存的 Token 数量(缓存写入) |
cached_tokens、cache_write_tokens 与未缓存部分互斥,三者之和等于 input_tokens,即未缓存部分 = input_tokens − cached_tokens − cache_write_tokens。授权
请求头
请求体
使用的模型 ID。本接口当前支持 kimi-k3。
"kimi-k3"
本次请求的输入。传字符串等价于一条 user 消息;传数组时按顺序给出带类型的 item,可包含历史对话、工具调用与工具结果。
顶层系统指令,作为最靠前的指令生效。
为 true 时以 SSE 事件流返回响应。
本次响应最多生成的 Token 数量。kimi-k3 默认为 131072,最大可设置为 1048576。此值为期望返回的 Token 长度,而非输入加输出的总长度。达到上限时 status 为 incomplete,incomplete_details.reason 为 max_output_tokens。
推理相关配置。
Show child attributes
Show child attributes
输出文本配置。
Show child attributes
Show child attributes
模型可调用的工具列表。
工具定义,按 type 区分。支持 function、custom(仅 apply_patch)、namespace 与 web_search,其他工具类型不支持。
- 函数工具
- 自定义工具
- 命名空间工具
- 联网搜索工具
Show child attributes
Show child attributes
控制工具调用行为。取 auto 时由模型自行决定是否调用工具。
auto 要额外返回的字段,仅在使用 web_search 工具时有效。web_search_call.action.sources 返回搜索命中的网页来源;web_search_call.results 返回图片搜索结果。
web_search_call.results, web_search_call.action.sources 上下文缓存标识,同一会话使用相同取值可提升缓存命中率。
上下文缓存写入选项。不传时默认开启缓存写入(5m 档):系统自动将请求前缀写入 5m 档缓存
Show child attributes
Show child attributes
用于检测可能违反使用政策的用户的稳定标识符。应为唯一标识每个用户的字符串。建议对用户名或邮箱进行哈希处理以避免发送可识别信息
响应
响应创建成功
一次模型响应。
响应的唯一标识符。
"resp_68f0c1c2d3e4f5a6b7c8d9e0"
response 响应创建时的 Unix 时间戳。
响应结束时的 Unix 时间戳。status 为 completed 或 incomplete 时给出,为 in_progress 或 failed 时是 null。
响应状态。流式的起手快照为 in_progress。
in_progress, completed, incomplete, failed 产生本次响应的模型。
输出 item 数组,顺序为 web_search_call(如有)、reasoning、message、工具调用。
output 数组的元素,按 type 区分。
- 推理
- 消息
- 函数调用
- 自定义工具调用
- 联网搜索调用
Show child attributes
Show child attributes
本次响应的 Token 用量。
Show child attributes
Show child attributes
status 为 incomplete 时给出原因。
Show child attributes
Show child attributes
status 为 failed 时给出错误信息。
Show child attributes
Show child attributes
工具定义,按 type 区分。支持 function、custom(仅 apply_patch)、namespace 与 web_search,其他工具类型不支持。
- 函数工具
- 自定义工具
- 命名空间工具
- 联网搜索工具
Show child attributes
Show child attributes
控制工具调用行为。取 auto 时由模型自行决定是否调用工具。
auto 固定为 false。
固定为 false。
固定为 null。
固定为 null。
服务端实际应用的上下文缓存写入选项。当请求省略 prompt_cache_options 时,以此处返回的 mode 和 ttl 为准
Show child attributes
Show child attributes
此页面对您有帮助吗?
curl --request POST \
--url https://api.moonshot.cn/v1/responses \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "kimi-k3",
"input": "<string>",
"text": {},
"tools": [
{
"type": "function",
"name": "<string>",
"description": "<string>",
"parameters": {},
"strict": true
}
]
}
'import requests
url = "https://api.moonshot.cn/v1/responses"
payload = {
"model": "kimi-k3",
"input": "<string>",
"text": {},
"tools": [
{
"type": "function",
"name": "<string>",
"description": "<string>",
"parameters": {},
"strict": True
}
]
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: 'kimi-k3',
input: '<string>',
text: {},
tools: [
{
type: 'function',
name: '<string>',
description: '<string>',
parameters: {},
strict: true
}
]
})
};
fetch('https://api.moonshot.cn/v1/responses', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.moonshot.cn/v1/responses",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => 'kimi-k3',
'input' => '<string>',
'text' => [
],
'tools' => [
[
'type' => 'function',
'name' => '<string>',
'description' => '<string>',
'parameters' => [
],
'strict' => true
]
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.moonshot.cn/v1/responses"
payload := strings.NewReader("{\n \"model\": \"kimi-k3\",\n \"input\": \"<string>\",\n \"text\": {},\n \"tools\": [\n {\n \"type\": \"function\",\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"parameters\": {},\n \"strict\": true\n }\n ]\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.moonshot.cn/v1/responses")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"kimi-k3\",\n \"input\": \"<string>\",\n \"text\": {},\n \"tools\": [\n {\n \"type\": \"function\",\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"parameters\": {},\n \"strict\": true\n }\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.moonshot.cn/v1/responses")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"kimi-k3\",\n \"input\": \"<string>\",\n \"text\": {},\n \"tools\": [\n {\n \"type\": \"function\",\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"parameters\": {},\n \"strict\": true\n }\n ]\n}"
response = http.request(request)
puts response.read_body{
"id": "resp_68f0c1c2d3e4f5a6b7c8d9e0",
"object": "response",
"created_at": 123,
"completed_at": 123,
"status": "in_progress",
"model": "<string>",
"output": [
{
"type": "reasoning",
"id": "rs_68f0c1c2d3e4f5a6b7c8d9e0",
"summary": [
{
"type": "summary_text",
"text": "<string>"
}
],
"encrypted_content": "<string>",
"status": "in_progress"
}
],
"usage": {
"input_tokens": 123,
"input_tokens_details": {
"cached_tokens": 123,
"cache_write_tokens": 123
},
"output_tokens": 123,
"output_tokens_details": {
"reasoning_tokens": 123
},
"total_tokens": 123
},
"incomplete_details": {
"reason": "max_output_tokens"
},
"error": {
"code": "<string>",
"message": "<string>"
},
"instructions": "<string>",
"reasoning": {},
"text": {},
"tools": [
{
"type": "function",
"name": "<string>",
"description": "<string>",
"parameters": {},
"strict": true
}
],
"tool_choice": "auto",
"max_output_tokens": 123,
"temperature": 123,
"top_p": 123,
"metadata": {},
"parallel_tool_calls": true,
"service_tier": "<string>",
"store": true,
"background": true,
"previous_response_id": "<string>",
"conversation": {},
"prompt_cache_options": {
"mode": "implicit",
"ttl": "5m"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}