Skip to content

使用 Kimi 的 thinking 和 reasoning_effort

Kimi K2.x 在 OpenAI-compatible Chat Completions 路径上使用 thinking 配置思考行为;kimi-k3 始终推理,使用请求顶层的 reasoning_effort 配置推理强度。两类模型都可通过 reasoning_content 返回推理内容。Anthropic-compatible 路径属于不同协议形态,不要直接套用本页的参数和历史回传规则。

按模型选择推理参数

先根据模型和客户端选择参数。自行编写 K3 的 curl 或 SDK 请求时,不要把 K2.x 的 thinking 配置直接复制到 K3 请求。

  • kimi-k2.7-code 始终开启 thinking 和 preserved thinking。生产请求可省略 thinking;如果显式设置,只使用 {"type": "enabled", "keep": "all"}thinking.type: "disabled" 会返回 400。
  • kimi-k3 始终进行推理且 preserved thinking 始终开启。自行编写 curl 或 SDK 请求时,使用顶层 reasoning_effort;通过 Kimi Code 接入时,也可使用 GenStudio 支持的 Kimi Code 兼容格式。详见 配置 Kimi K3
  • kimi-k2.6 默认启用思考,可显式关闭,并支持通过 thinking.keep: "all" 保留历史推理。
  • kimi-k2.5 默认启用思考,也可显式关闭,但不支持 preserved thinking。不要向该模型发送 thinking.keep

如果模型 ID 带有明确的 thinking 后缀,先按强制思考模型处理。

用 thinking.type 控制当前请求

当前请求只需要关闭思考时,设置 thinking.type: "disabled"。开启思考时使用 "enabled"。以下 Python 示例使用 OpenAI SDK。

language-python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["API_KEY"],
    base_url="https://cloud.infini-ai.com/maas/v1",
)

response = client.chat.completions.create(
    model="kimi-k2.6",
    messages=[{"role": "user", "content": "What is 2 + 2? Give only the final answer."}],
    max_tokens=256,
    extra_body={"thinking": {"type": "disabled"}},
)

如果关闭后仍看到推理字段,先确认目标模型是否属于强制思考模型。

用 curl 验证 thinking.type 请求体

先用 curl 确认可切换 Kimi 模型是否接受 thinking.type: "disabled"。运行前先在当前终端设置 API_KEY 环境变量。以下 curl 命令适用于 bash/zsh 等 POSIX 风格 Shell(macOS/Linux、WSL、Git Bash)。如果使用 Windows PowerShell 或 CMD,请按对应 Shell 的语法调整命令。

language-shell
curl --request POST \
  --url "https://cloud.infini-ai.com/maas/v1/chat/completions" \
  --header "Accept: application/json, text/event-stream" \
  --header "Authorization: Bearer $API_KEY" \
  --header "Content-Type: application/json" \
  --data-raw '{
    "model": "kimi-k2.6",
    "messages": [
      {
        "role": "user",
        "content": "What is 2 + 2? Give only the final answer."
      }
    ],
    "max_tokens": 256,
    "thinking": {
      "type": "disabled"
    }
  }'

终端录制

用 curl 验证 Kimi thinking.type

在演示库中打开

适用于验证 Kimi 推理开关字段是否按预期传入。

Kimi K2.7 Code 行为

接入 kimi-k2.7-code 时,按强制思考和强制 preserved thinking 模型处理请求形态。

  • 生产请求可省略 thinking;如果显式设置,只使用 {"type": "enabled", "keep": "all"}
  • 非流式响应读取 message.reasoning_content,流式响应读取 delta.reasoning_content
  • thinking.type: "disabled" 会返回 400,错误信息表示该模型的 thinking.type 必须为 "enabled"
  • preserved thinking 默认开启且不可关闭。thinking.keep 不传或传 "all" 都按 "all" 处理。
  • 图片输入可在流式响应中返回推理字段。视频输入需使用服务端可访问的媒体地址;接入前请用业务实际使用的视频地址确认请求。

配置 Kimi K3

kimi-k3 始终进行推理且 preserved thinking 始终开启。自行编写 curl 或 SDK 请求时,不要沿用 K2.x 专属的 thinking 配置;需要调整推理强度时,在请求顶层设置 reasoning_effort

reasoning_effort 支持 "low""high""max",默认值为 "max"。切换档位会影响前缀缓存命中;同一会话应尽量固定档位。

language-python
response = client.chat.completions.create(
    model="kimi-k3",
    messages=[{"role": "user", "content": "分析这个方案的主要风险。"}],
    reasoning_effort="high",
)

注意

Kimi Code 兼容格式

通过 GenStudio 使用 Kimi Code 的 type = "kimi" Provider 时,客户端会发送 thinking: {"type": "enabled", "effort": "max", "keep": "all"}。GenStudio 支持该格式;effort 可设为 "low""high""max"

该格式由 Kimi Code Provider 生成。自行编写 curl 或 SDK 请求时,使用顶层 reasoning_effort。两种格式面向不同的客户端,无需同时设置。完整配置参见使用 Kimi Code 集成 GenStudio API

  • 非流式响应读取 message.reasoning_content,流式响应读取 delta.reasoning_content
  • 多轮对话和工具调用必须原样回传完整 assistant 消息,包括模型实际返回的 reasoning_contenttool_calls
  • 图片输入使用 image_url。视频输入使用 video_url,并提供 GenStudio 服务可访问的媒体地址。
  • tool_choice 支持字符串形式的 "auto""none""required"。对象形式(指定函数名)的强制 tool_choice 与已开启的思考不兼容,会返回 400。工具调用的处理参见 Function calling
  • 结构化输出优先使用 response_formatjson_schema 严格模式和 json_object 均可用);基于命名工具的结构化输出会命中同一条 tool_choice 400。参见 Structured output

用 thinking.keep 保留历史推理

kimi-k2.6thinking.keep 用于历史保留,不用于开启当前请求思考。需要在工具调用或 agent 流程中保留推理连续性时,和当前请求的 thinking.type 一起设置。

language-python
extra_body = {
    "thinking": {
        "type": "enabled",
        "keep": "all",
    }
}

kimi-k2.6,只关闭当前请求思考时,设置 thinking.type 即可。thinking.keep 留给需要历史推理连续性的工具调用或 agent 流程。

kimi-k2.5,可以使用 thinking.type 开启或关闭当前请求思考,但该模型不支持 preserved thinking。不要发送 thinking.keep,也不要把 K2.6 的历史推理保留配置复制到 K2.5 请求。

kimi-k2.7-code,preserved thinking 已默认开启。工具调用后的历史回传可在非流式和流式请求中继续使用。保守做法是回传模型原始返回的 assistant 消息,包括 reasoning_contenttool_calls,再追加工具结果。

对自行编写的 kimi-k3 curl 或 SDK 请求,不发送 thinking.keep,使用顶层 reasoning_effort。Kimi Code 的 type = "kimi" Provider 会在兼容格式中发送 keep: "all"。两种方式下 preserved thinking 都始终开启;多轮对话和工具调用同样需要原样回传完整 assistant 消息。

读取 Kimi 的 reasoning_content

Kimi 推理内容通过 reasoning_content 返回。OpenAI SDK 的类型定义可能没有这个字段,应用代码应使用 getattr 或 SDK 提供的扩展字段访问方式。

language-python
message = response.choices[0].message
reasoning = getattr(message, "reasoning_content", None)

if reasoning:
    print(reasoning)
print(message.content)

流式响应中同样读取 delta.reasoning_content。不要假设每个 chunk 都有该字段。

回传发起工具调用的 assistant 消息

当 Kimi 在思考后发起工具调用时,assistant 消息通常同时包含 reasoning_contenttool_calls。继续对话时,回传这条 assistant 消息,再追加工具结果。

language-python
assistant_message = {
    "role": "assistant",
    "content": response.choices[0].message.content or "",
    "tool_calls": [
        {
            "id": tool_call.id,
            "type": "function",
            "function": {
                "name": tool_call.function.name,
                "arguments": tool_call.function.arguments,
            },
        }
        for tool_call in response.choices[0].message.tool_calls
    ],
}

reasoning = getattr(response.choices[0].message, "reasoning_content", None)
if reasoning:
    assistant_message["reasoning_content"] = reasoning

messages.append(assistant_message)
messages.append(
    {
        "role": "tool",
        "tool_call_id": assistant_message["tool_calls"][0]["id"],
        "content": '{"weather": "Sunny"}',
    }
)

如果历史中有一轮关闭了思考,该轮 assistant 没有 reasoning_content 是正常状态。保留真实历史,不要补写,也不要用空字符串或摘要替代模型原始返回。

kimi-k2.7-code 不支持 tool_choice: "required"。接入工具调用时,省略 tool_choice 或使用 "auto",并在应用侧校验返回的工具名。

kimi-k3 支持 tool_choice: "required",但不支持在思考开启时使用对象形式固定函数名。如果声明了多个工具,使用 "required" 后仍需在应用侧校验模型实际返回的工具名。

分开检查当前请求和历史回传

排查 Kimi 请求时,把当前轮参数和历史消息分开检查。

  • 当前轮参数:K2.x 检查 thinking;K3 的 curl 或 SDK 请求检查顶层 reasoning_effort,Kimi Code 请求检查所选 Thinking effort。
  • 历史推理是否保留:kimi-k2.6 检查 thinking.keep 和回传的 assistant 消息;kimi-k2.5 不支持 preserved thinking;kimi-k2.7-codekimi-k3 默认保留,重点检查是否原样回传完整 assistant 消息。
  • 工具调用是否能继续:检查 tool_callstool_call_idreasoning_content 是否来自同一条原始 assistant 响应。

如果请求返回 400,先移除人工拼接的推理内容,再用模型原始返回的 assistant 消息重试。