> This page is for version v1 (default).
> For other versions, use one of these documentation indexes:
> - v1 (default): https://docs.butterix.com/llms.txt
> - v0: https://docs.butterix.com/v0/llms.txt

> For the MDX source of any page, append .mdx to the page URL.
> For a complete documentation index, see https://docs.butterix.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.butterix.com/_mcp/server.
> If something on this page is wrong or missing, POST JSON {"page": "<page URL>", "message": "<what is wrong>"} to https://docs.butterix.com/_biti/feedback/agent.

# Ask AI 对话（SSE）

`POST /_biti/ai/chat`

检索站点文档并流式生成带引用编号的回答，响应是 `text/event-stream`，事件依次为：

| 事件 | 数据 |
|---|---|
| `sources` | `[{ n, title, heading?, url }]` 引用来源 |
| `text` | `{ t }` 一段增量文本（多次） |
| `done` | `{ model, sig?, notice? }` |
| `error` | `{ message, code? }` |

`done.sig` 是服务端对这一轮问答的签名，追问时把它放在对应的 `assistant` 消息里回传；没有有效签名的那一轮问答不会作为上下文交给模型。
问题与文档明显无关时不生成回答，`text` 是一句固定说明，`done` 为 `{ model: null, notice: "offtopic" }`；
站点达到每日上限时只返回相关文档，`done` 为 `{ model: null, notice: "budget" }`。

托管平台上的调用要求：

- 请求来自站点自身的页面（`Origin` 与站点一致，`Sec-Fetch-Site` 为 `same-origin`），否则返回 403；
- 新版本构建的站点还需要在 `X-Biti-AI-Token` 头中携带 [会话令牌](https://docs.butterix.com/api-reference/runtime/ask-ai-token.md)，缺失或过期时返回 401（`code: "ai_token"`），重新获取后重试即可。

限流：每个 IP（IPv6 按 /64）每分钟 12 次、每天 200 次；每个网段（IPv4 /24、IPv6 /48）每分钟 60 次、每天 1000 次；同一 IP 最多同时 2 个回答。
站点关闭 Ask AI 时返回 404，未配置密钥或月度配额用完时返回 503。因为要求同源，API 参考页面中的「试调」无法调用这个端点。

Base URL: https://butterix-sites.butterix.com

## Header parameters

| Name | Type | Required | Description |
|---|---|---|---|
| `X-Biti-AI-Token` | string | no | 会话令牌（托管平台上新版本构建的站点必填） |

## Request body

Content type: `application/json`

| Field | Type | Required | Description |
|---|---|---|---|
| `messages` | list of objects | yes | 对话历史，最后一条必须是 `user` |
| `messages[].role` | enum | yes | Allowed: `user`, `assistant`. |
| `messages[].content` | string | yes | `user` 消息最多 512 字符 |
| `messages[].sig` | string | no | `assistant` 消息：该轮 `done` 事件返回的签名 |
| `locale` | string | no | 回答语言，默认站点第一种语言 |

## Example request

```bash
curl -X POST https://butterix-sites.butterix.com/_biti/ai/chat \
     -H "Content-Type: application/json" \
     -d '{
         "messages": [
           {
             "role": "user",
             "content": "如何给站点添加英文版？"
           }
         ],
         "locale": "zh"
       }'
```

## Responses

### 200

事件流

Content type: `text/event-stream` (stream; each event has these fields)

Type: string

### 400

最后一条不是问题，或问题超过 512 字符（`code: "too_long"`）

Content type: `application/json`

| Field | Type | Required | Description |
|---|---|---|---|
| `error` | string | yes | 人类可读的错误信息 |
| `code` | string | no | 机器可读的错误码（部分接口提供） |

### 401

缺少会话令牌或令牌已过期（`code: "ai_token"`）

Content type: `application/json`

| Field | Type | Required | Description |
|---|---|---|---|
| `error` | string | yes | 人类可读的错误信息 |
| `code` | string | no | 机器可读的错误码（部分接口提供） |

### 403

不是来自站点自身页面的请求（`code: "origin"`）

Content type: `application/json`

| Field | Type | Required | Description |
|---|---|---|---|
| `error` | string | yes | 人类可读的错误信息 |
| `code` | string | no | 机器可读的错误码（部分接口提供） |

### 413

请求体超过 256 KB（`code: "too_large"`）

Content type: `application/json`

| Field | Type | Required | Description |
|---|---|---|---|
| `error` | string | yes | 人类可读的错误信息 |
| `code` | string | no | 机器可读的错误码（部分接口提供） |

### 429

提问过于频繁（`code: "rate_limited"`）或同时进行的回答过多（`code: "busy"`）

Content type: `application/json`

| Field | Type | Required | Description |
|---|---|---|---|
| `error` | string | yes | 人类可读的错误信息 |
| `code` | string | no | 机器可读的错误码（部分接口提供） |

### 503

未配置 AI 提供商

Content type: `application/json`

| Field | Type | Required | Description |
|---|---|---|---|
| `error` | string | yes | 人类可读的错误信息 |
| `code` | string | no | 机器可读的错误码（部分接口提供） |

### Example response (200)

```json
"event: sources\ndata: [{\"n\":1,\"title\":\"多语言\",\"url\":\"/guides/features/i18n\"}]\n\nevent: text\ndata: {\"t\":\"在 docs.yml 的 languages 中加入 en [1]，\"}\n\nevent: text\ndata: {\"t\":\"然后把译文放到 translations/en/ 下。\"}\n\nevent: done\ndata: {\"model\":\"claude-opus-5-5\",\"unanswered\":true}\n"
```
