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

# 压缩响应上下文

> 返回可用于下一轮 Responses 请求的完整窗口。

`POST /v1/responses/compact`

发送支持压缩的 `model` 与完整上下文 `input`；常用可选字段为 `instructions`。接口为非流式请求，不支持把 `stream: true` 当作 SSE 开关。更多供应商扩展需逐渠道确认。

返回 `object: response.compaction`、`output` 及可选 `usage`。`output` 包含压缩项，也可能保留消息等其他条目；完整回放它们，不根据普通 Responses 的 `status` 字段判断压缩成功。密文内容保持原样。

网关 DTO 接收 `previous_response_id`，但状态解析依赖上游；本文示例显式传历史，不承诺跨渠道 ID 续接、通用资源查询或自动压缩。

[压缩与续聊示例](/zh/guides/compaction.md) · [创建响应](/zh/api-reference/responses.md)

[Read in English](/en/api-reference/responses-compact.md)


## OpenAPI

````yaml openapi/relay.json POST /v1/responses/compact
openapi: 3.0.3
info:
  title: API — Core inference API
  version: 0.5.0
  description: >-
    面向接入的精选规范，不是全部管理或供应商接口。 / Curated integration specification, not the entire
    management or provider API.
  license:
    name: AGPL-3.0-or-later
servers:
  - url: https://gateway.example.com
    description: >-
      Example only; replace with your gateway origin and optional path prefix,
      without /v1.
security:
  - BearerAuth: []
paths:
  /v1/responses/compact:
    post:
      summary: 压缩上下文 / Compact context
      description: 需验证模型、渠道及部署支持 / Verify model, channel, and deployment support
      operationId: compactResponses
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CompactionRequest'
      responses:
        '200':
          description: 成功结构仍需检查业务字段 / Check business fields even on HTTP success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CompactionResult'
              example:
                id: cmp_example
                object: response.compaction
                created_at: 1700000000
                output:
                  - type: message
                    role: user
                    content:
                      - type: input_text
                        text: Plan a two-day visit.
                  - type: compaction
                    id: cmp_item
                    encrypted_content: OPAQUE_STATE_FROM_UPSTREAM
        '400':
          description: 请求参数错误 / Invalid request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '401':
          description: 身份校验失败 / Authentication failed
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '403':
          description: 访问被拒绝 / Access denied
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '404':
          description: 接口或资源不存在 / Endpoint or resource not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '429':
          description: 请求被限流或受配额限制 / Rate or quota restriction
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '500':
          description: 网关或上游错误 / Gateway or upstream error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '503':
          description: 服务暂不可用 / Service unavailable
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        default:
          description: 其他网关或上游错误；格式可能不同 / Other gateway or upstream error; shape may vary
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
components:
  schemas:
    CompactionRequest:
      type: object
      required:
        - model
      properties:
        model:
          type: string
          description: >-
            模型列表返回的原始 ID；需要支持当前接口 / Exact ID from the model list; must support
            this endpoint
          example: YOUR_MODEL_ID
        input:
          oneOf:
            - type: string
            - type: array
              items:
                type: object
                additionalProperties: true
          description: >-
            文本或输入项数组；内容块和工具项需模型支持 / Text or input items; content blocks and tool
            items require model support
        instructions:
          type: string
          description: >-
            本次压缩指令；续聊时再次明确发送所需指令 / Instructions for this compaction; explicitly
            send needed instructions again when continuing
        previous_response_id:
          type: string
          description: >-
            DTO 接收但状态解析依赖上游；建议显式历史 / Accepted by the DTO but state resolution
            requires upstream support; prefer explicit history
    CompactionResult:
      type: object
      required:
        - object
        - output
      properties:
        id:
          type: string
        object:
          type: string
          enum:
            - response.compaction
        created_at:
          type: number
        output:
          type: array
          items:
            type: object
            additionalProperties: true
            properties:
              type:
                type: string
              encrypted_content:
                type: string
                description: >-
                  压缩项中的不透明密文；保持原样 / Opaque ciphertext in compaction items;
                  preserve unchanged
          description: >-
            下一轮完整上下文窗口，保留所有条目及顺序 / Complete next context window; retain all
            items and their order
        usage:
          type: object
          properties:
            input_tokens:
              type: integer
            output_tokens:
              type: integer
            total_tokens:
              type: integer
          nullable: true
    Error:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
            type:
              type: string
            param:
              type: string
              nullable: true
            code:
              nullable: true
              description: 错误标识；类型随上游变化 / Error identifier; type varies by upstream
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      description: 模型调用 API Key，不是控制台登录凭据 / Model API key, not console login credentials

````

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