Skip to content

OpenResponses API(HTTP)

OpenClaw 的 Gateway 可以提供一个兼容 OpenResponses 的 POST /v1/responses 端点。

该端点 默认关闭,需要先在配置中启用。

  • POST /v1/responses
  • 与 Gateway 同端口(WS + HTTP 复用):http://<gateway-host>:<port>/v1/responses

在实现上,请求会作为一次普通的 Gateway agent run 执行(与 openclaw agent 走同一条代码路径),因此路由/权限/config 都与你的 Gateway 一致。

认证

使用 Gateway 的 auth 配置。发送 bearer token:

  • Authorization: Bearer <token>

备注:

  • gateway.auth.mode="token" 时,使用 gateway.auth.token(或 OPENCLAW_GATEWAY_TOKEN)。
  • gateway.auth.mode="password" 时,使用 gateway.auth.password(或 OPENCLAW_GATEWAY_PASSWORD)。

选择 agent

不需要自定义 header:把 agent id 编码进 OpenResponses 的 model 字段:

  • model: "openclaw:<agentId>"(例如:"openclaw:main""openclaw:beta"
  • model: "agent:<agentId>"(别名)

或者通过 header 指定 OpenClaw agent:

  • x-openclaw-agent-id: <agentId>(默认:main

高级用法:

  • x-openclaw-session-key: <sessionKey> 用于完全控制 session 路由。

启用端点

gateway.http.endpoints.responses.enabled 设置为 true

json5
{
  gateway: {
    http: {
      endpoints: {
        responses: { enabled: true },
      },
    },
  },
}

禁用端点

gateway.http.endpoints.responses.enabled 设置为 false

json5
{
  gateway: {
    http: {
      endpoints: {
        responses: { enabled: false },
      },
    },
  },
}

Session 行为

默认情况下,该端点对每个请求都是 无状态的(每次调用都会生成一个新的 session key)。

如果请求里包含 OpenResponses 的 user 字符串,Gateway 会从中派生一个稳定的 session key,因此重复调用可以复用同一个 agent session。

请求结构(当前支持)

请求遵循 OpenResponses API 的 item-based input。当前支持:

  • input:字符串或 item 对象数组。
  • instructions:合并进 system prompt。
  • tools:客户端 tool 定义(function tools)。
  • tool_choice:筛选或强制客户端 tools。
  • stream:启用 SSE streaming。
  • max_output_tokens:best-effort 的输出上限(取决于 provider)。
  • user:用于稳定的 session 路由。

可接受但 当前会被忽略

  • max_tool_calls
  • reasoning
  • metadata
  • store
  • previous_response_id
  • truncation

Items(input)

message

Roles:systemdeveloperuserassistant

  • systemdeveloper 会追加到 system prompt。
  • 最新的 userfunction_call_output item 会成为“当前消息”。
  • 更早的 user/assistant messages 会作为历史加入上下文。

function_call_output(回合式 tools)

把 tool 结果回传给模型:

json
{
  "type": "function_call_output",
  "call_id": "call_123",
  "output": "{\"temperature\": \"72F\"}"
}

reasoningitem_reference

为 schema 兼容性而接受,但在构建 prompt 时会被忽略。

Tools(客户端侧 function tools)

通过 tools: [{ type: "function", function: { name, description?, parameters? } }] 提供 tools。

如果 agent 决定调用某个 tool,响应会返回一个 function_call 的 output item。 然后你需要带着 function_call_output 再发起一次请求,继续该回合。

图片(input_image

支持 base64 或 URL source:

json
{
  "type": "input_image",
  "source": { "type": "url", "url": "https://example.com/image.png" }
}

允许的 MIME types(当前):image/jpegimage/pngimage/gifimage/webp。 最大体积(当前):10MB。

文件(input_file

支持 base64 或 URL source:

json
{
  "type": "input_file",
  "source": {
    "type": "base64",
    "media_type": "text/plain",
    "data": "SGVsbG8gV29ybGQh",
    "filename": "hello.txt"
  }
}

允许的 MIME types(当前):text/plaintext/markdowntext/htmltext/csvapplication/jsonapplication/pdf

最大体积(当前):5MB。

当前行为:

  • 文件内容会被解码并加入 system prompt,而不是 user message,因此它保持“临时性”(不会持久化进 session history)。
  • PDF 会被解析为文本;如果可提取文本很少,会把前几页栅格化为图片并传给模型。

PDF 解析使用对 Node 友好的 pdfjs-dist legacy build(无 worker)。现代版 PDF.js 依赖浏览器 workers/DOM globals,因此 Gateway 中不使用。

URL fetch 默认值:

  • files.allowUrltrue
  • images.allowUrltrue
  • 请求会被保护(DNS 解析、私网 IP 阻断、重定向上限、超时)。

文件 + 图片限制(config)

默认值可在 gateway.http.endpoints.responses 下调节:

json5
{
  gateway: {
    http: {
      endpoints: {
        responses: {
          enabled: true,
          maxBodyBytes: 20000000,
          files: {
            allowUrl: true,
            allowedMimes: [
              "text/plain",
              "text/markdown",
              "text/html",
              "text/csv",
              "application/json",
              "application/pdf",
            ],
            maxBytes: 5242880,
            maxChars: 200000,
            maxRedirects: 3,
            timeoutMs: 10000,
            pdf: {
              maxPages: 4,
              maxPixels: 4000000,
              minTextChars: 200,
            },
          },
          images: {
            allowUrl: true,
            allowedMimes: ["image/jpeg", "image/png", "image/gif", "image/webp"],
            maxBytes: 10485760,
            maxRedirects: 3,
            timeoutMs: 10000,
          },
        },
      },
    },
  },
}

省略时的默认值:

  • maxBodyBytes:20MB
  • files.maxBytes:5MB
  • files.maxChars:200k
  • files.maxRedirects:3
  • files.timeoutMs:10s
  • files.pdf.maxPages:4
  • files.pdf.maxPixels:4,000,000
  • files.pdf.minTextChars:200
  • images.maxBytes:10MB
  • images.maxRedirects:3
  • images.timeoutMs:10s

Streaming(SSE)

设置 stream: true 即可接收 Server-Sent Events(SSE):

  • Content-Type: text/event-stream
  • 每条 event line 为 event: <type>data: <json>
  • data: [DONE] 结束

当前会发出的 event types:

  • response.created
  • response.in_progress
  • response.output_item.added
  • response.content_part.added
  • response.output_text.delta
  • response.output_text.done
  • response.content_part.done
  • response.output_item.done
  • response.completed
  • response.failed(出错时)

Usage

当底层 provider 提供 token 统计时,会填充 usage

Errors

错误使用类似下面的 JSON 对象:

json
{ "error": { "message": "...", "type": "invalid_request_error" } }

常见情况:

  • 401 缺失/无效鉴权
  • 400 请求体不合法
  • 405 method 错误

示例

非流式:

bash
curl -sS http://127.0.0.1:18789/v1/responses \
  -H 'Authorization: Bearer YOUR_TOKEN' \
  -H 'Content-Type: application/json' \
  -H 'x-openclaw-agent-id: main' \
  -d '{
    "model": "openclaw",
    "input": "hi"
  }'

流式:

bash
curl -N http://127.0.0.1:18789/v1/responses \
  -H 'Authorization: Bearer YOUR_TOKEN' \
  -H 'Content-Type: application/json' \
  -H 'x-openclaw-agent-id: main' \
  -d '{
    "model": "openclaw",
    "stream": true,
    "input": "hi"
  }'