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

# Responses

> 生成、流式输出、保存、续接与后台取消。

## 基础调用

入口为 `POST /v1/responses`，使用 Bearer 鉴权和支持 `responses` 的模型。下面示例不保存平台响应内容。

<CodeGroup>
  ```python Python theme={null}
  from openai import OpenAI
  import os

  client = OpenAI(
      api_key=os.environ["CODEFLARE_API_KEY"],
      base_url="https://YOUR-REGION.example/v1",
      max_retries=0,
  )
  response = client.responses.create(
      model="MODEL_ID", input="Hello",
      max_output_tokens=256, store=False,
  )
  print(response.output_text)
  ```

  ```javascript JavaScript theme={null}
  import OpenAI from "openai";

  const client = new OpenAI({
    apiKey: process.env.CODEFLARE_API_KEY,
    baseURL: "https://YOUR-REGION.example/v1",
    maxRetries: 0,
  });
  const response = await client.responses.create({
    model: "MODEL_ID", input: "Hello",
    max_output_tokens: 256, store: false,
  });
  console.log(response.output_text);
  ```

  ```bash cURL theme={null}
  curl "https://YOUR-REGION.example/v1/responses" \
    -H "Authorization: Bearer $CODEFLARE_API_KEY" \
    -H 'Content-Type: application/json' \
    -d '{"model":"MODEL_ID","input":"Hello","max_output_tokens":256,"store":false}'
  ```
</CodeGroup>

## 流式输出

设置 `stream: true` 并逐事件处理，不把 SSE 直接当作 JSON。复用示例中的 `client`：

<CodeGroup>
  ```python Python theme={null}
  with client.responses.create(
      model="MODEL_ID", input="Hello",
      max_output_tokens=256, store=False, stream=True,
  ) as stream:
      for event in stream:
          if event.type == "response.output_text.delta":
              print(event.delta, end="", flush=True)
  ```

  ```javascript JavaScript theme={null}
  const stream = await client.responses.create({
    model: "MODEL_ID", input: "Hello",
    max_output_tokens: 256, store: false, stream: true,
  });
  for await (const event of stream) {
    if (event.type === "response.output_text.delta") {
      process.stdout.write(event.delta);
    }
  }
  ```
</CodeGroup>

## 保存与续接

`store=true` 默认保留 30 天。保存响应 ID；同一用户且具有模型权限时，可在原区域使用 `GET /v1/responses/RESPONSE_ID` 查询、`GET /v1/responses/RESPONSE_ID/input_items` 查看输入，再通过 `previous_response_id` 续接。

`store=false` 不保存平台内容副本，不能通过 HTTP 查询或续接；仅同一 WebSocket 连接内保留可续接历史。后台执行要求 `store=true`。

## 后台执行与取消

渠道支持 `background` 时，可使用 `background: true, store: true` 提交，轮询已保存的响应状态。取消后台响应：

```bash theme={null}
curl -X POST "https://YOUR-REGION.example/v1/responses/RESPONSE_ID/cancel" \
  -H "Authorization: Bearer $CODEFLARE_API_KEY"
```

`DELETE /v1/responses/RESPONSE_ID` 会取消执行、阻止后续查询和续接并移除内容，财务记录仍保留。取消或断连不能保证零用量。

## 其他能力

`POST /v1/responses/compact`、`POST /v1/responses/input_tokens` 与 WebSocket `GET /v1/responses` 取决于渠道能力。compact 计费，token 计数不收生成费用。事件恢复可使用 `stream`、`starting_after` 查询参数；必须继续使用原响应所在区域。

<Warning>请求中断后先查询已保存的响应和日志，禁止根据断连直接重放生成。工具、多模态与其他边界见[能力与限制](/zh/capabilities)。</Warning>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.