> ## Documentation Index
> Fetch the complete documentation index at: https://docs.anyfast.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Kimi K3

> 通过 AnyFast OpenAI 兼容接口调用月之暗面 Kimi K3 旗舰多模态模型。

Kimi K3 是月之暗面面向长周期编码、端到端知识工作和深度推理推出的旗舰模型。它提供 100 万 token 上下文窗口、图片理解能力，并通过 AnyFast OpenAI 兼容的对话补全接口提供始终开启的推理能力。

<Info>
  模型 ID 为 `kimi-k3`。K3 始终进行推理，并使用顶层 `reasoning_effort` 字段控制思考强度，不再使用 K2.x 的 `thinking` 对象。
</Info>

## 核心能力

* **100 万 token 上下文** — 处理大型代码库、长文档和长时间运行的智能体历史
* **2.8T 稀疏架构** — 使用 KDA、Attention Residuals 和 Stable LatentMoE，每个 Token 激活 896 个专家中的 16 个
* **多模态输入** — 在同一轮对话中理解文本和图片
* **始终开启推理** — 支持 `low`、`high` 和 `max`，默认为 `max`
* **长周期编码** — 理解大型代码库并协调终端或外部工具完成工程任务
* **结构化输出** — 使用 JSON Mode 或严格 JSON Schema 约束最终答案
* **工具调用** — `tool_choice` 支持 `auto`、`none` 和 `required`
* **自动上下文缓存** — 长前缀保持不变时无需创建缓存 ID 即可尝试命中缓存
* **Partial Mode** — 从预先提供的 assistant 文本前缀继续生成

## 快速示例

<CodeGroup>
  ```bash cURL theme={null}
  curl https://www.anyfast.ai/v1/chat/completions \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "kimi-k3",
      "messages": [
        {"role": "user", "content": "用一句话介绍 Kimi K3。"}
      ]
    }'
  ```

  ```python Python theme={null}
  from openai import OpenAI

  client = OpenAI(
      api_key="YOUR_API_KEY",
      base_url="https://www.anyfast.ai/v1"
  )

  completion = client.chat.completions.create(
      model="kimi-k3",
      messages=[
          {"role": "user", "content": "用一句话介绍 Kimi K3。"}
      ]
  )

  print(completion.choices[0].message.content)
  ```
</CodeGroup>

## 思考强度

K3 始终开启推理。根据任务复杂度，将顶层 `reasoning_effort` 设置为 `low`、`high` 或 `max`，默认值为 `max`。

```python theme={null}
completion = client.chat.completions.create(
    model="kimi-k3",
    reasoning_effort="high",
    messages=[
        {"role": "user", "content": "证明 2 的平方根是无理数。"}
    ]
)

print(completion.choices[0].message.content)
```

<Warning>
  不要传入 K2.x 的 `thinking` 参数。K3 的推理无法关闭。对话中途切换 `reasoning_effort` 还会使前缀缓存失效，因此应在对话开始前确定思考强度。
</Warning>

## 流式输出

流式模式下，推理过程通过 `delta.reasoning` 返回，最终答案通过 `delta.content` 返回。非流式响应还会返回 `message.reasoning`，并可能包含 `message.reasoning_details`。

```python theme={null}
stream = client.chat.completions.create(
    model="kimi-k3",
    reasoning_effort="max",
    messages=[
        {"role": "user", "content": "解释天空为什么是蓝色的。"}
    ],
    stream=True,
    stream_options={"include_usage": True}
)

for chunk in stream:
    if not chunk.choices:
        continue
    delta = chunk.choices[0].delta
    reasoning = getattr(delta, "reasoning", None)
    if reasoning:
        print(reasoning, end="", flush=True)
    if delta.content:
        print(delta.content, end="", flush=True)
```

## 图片输入

多模态 `content` 必须是带类型的对象数组。AnyFast 支持传入公开图片 URL 或 Base64 Data URI。

```python theme={null}
import base64
from pathlib import Path

image_data = base64.b64encode(Path("image.png").read_bytes()).decode()

completion = client.chat.completions.create(
    model="kimi-k3",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "image_url",
                    "image_url": {
                        "url": f"data:image/png;base64,{image_data}"
                    }
                },
                {"type": "text", "text": "描述这张图片。"}
            ]
        }
    ]
)

print(completion.choices[0].message.content)
```

官方建议图片不超过 4K。

## 结构化输出

使用 `response_format.type: "json_schema"` 并设置 `strict: true`，可以约束最终 `message.content` 的结构。

```python theme={null}
import json

completion = client.chat.completions.create(
    model="kimi-k3",
    messages=[
        {"role": "user", "content": "小林今年 28 岁，请提取姓名和年龄。"}
    ],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "person",
            "strict": True,
            "schema": {
                "type": "object",
                "properties": {
                    "name": {"type": "string"},
                    "age": {"type": "integer"}
                },
                "required": ["name", "age"],
                "additionalProperties": False
            }
        }
    }
)

person = json.loads(completion.choices[0].message.content)
print(person)
```

只解析 `message.content`；`reasoning` 和 `reasoning_details` 不属于结构化结果。

## 工具调用

K3 支持顶层函数工具，`tool_choice` 可以使用 `auto`、`none` 或 `required`。

```python theme={null}
completion = client.chat.completions.create(
    model="kimi-k3",
    messages=[
        {"role": "user", "content": "上海今天的天气怎么样？"}
    ],
    tools=[
        {
            "type": "function",
            "function": {
                "name": "get_weather",
                "description": "查询城市天气",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "city": {"type": "string"}
                    },
                    "required": ["city"]
                }
            }
        }
    ],
    tool_choice="required"
)

print(completion.choices[0].message.tool_calls)
```

执行工具后，将完整 assistant 消息和每个 `tool_call_id` 对应的 `role: "tool"` 结果追加到 `messages`，再调用模型获得最终答案。

## Partial Mode

在最后追加一条带有 `partial: true` 的 assistant 消息，可以让模型从指定前缀继续生成。

```python theme={null}
prefix = "结论："

completion = client.chat.completions.create(
    model="kimi-k3",
    messages=[
        {"role": "user", "content": "解释 API 兼容性为什么重要。"},
        {"role": "assistant", "content": prefix, "partial": True}
    ]
)

print(prefix + (completion.choices[0].message.content or ""))
```

## 参数与限制

| 参数                      | 类型             | 必需 | 说明                                                  |
| ----------------------- | -------------- | -- | --------------------------------------------------- |
| `model`                 | string         | 是  | 固定为 `kimi-k3`                                       |
| `messages`              | array          | 是  | 对话历史，支持文本、图片内容和工具消息                                 |
| `reasoning_effort`      | string         | 否  | `low`、`high` 或 `max`，默认为 `max`                      |
| `max_completion_tokens` | integer        | 否  | 默认 131,072，最大 1,048,576；输入与输出总量不能超过 100 万 token 上下文 |
| `stream`                | boolean        | 否  | 是否启用 SSE 流式输出，默认为 `false`                           |
| `stream_options`        | object         | 否  | 设置 `include_usage: true` 可在最终流式数据块中返回用量             |
| `response_format`       | object         | 否  | 支持 `text`、`json_object` 或 `json_schema`             |
| `tools`                 | array          | 否  | 提供给模型的函数定义                                          |
| `tool_choice`           | string         | 否  | `auto`、`none` 或 `required`                          |
| `stop`                  | string / array | 否  | 最多 5 个停止字符串，每个不超过 32 字节；停止词可能在最终内容生成前终止推理           |
| `prompt_cache_key`      | string         | 否  | 稳定的会话或任务标识，有助于提高缓存命中率                               |
| `safety_identifier`     | string         | 否  | 稳定且建议经过哈希处理的终端用户标识                                  |

<Note>
  `temperature` 固定为 `1.0`，`top_p` 固定为 `0.95`，`n` 固定为 `1`，两个 penalty 参数固定为 `0`。请勿显式传入这些参数。多轮对话和工具调用必须原样回传完整 assistant 消息，包括 `reasoning`、`reasoning_details` 和 `tool_calls`。
</Note>

<Note>
  自动上下文缓存针对保持不变的前缀生效。上一请求的 Prompt 超过 256 Token 后，后续请求才可能命中前缀缓存。同一会话或任务建议复用稳定的 `prompt_cache_key`。
</Note>

<Card title="API 参考" icon="code" href="/zh/api-reference/model-api/moonshot/kimi-k3">
  查看 Kimi K3 的交互式 API 参考。
</Card>

<script src="/feedback.js" />
