OpenResponses API (HTTP)
OpenClaw 的 Gateway 可以提供一个兼容 OpenResponses 的 POST /v1/responses 端点。
这个端点默认是禁用的。你要先在配置中启用它。
POST /v1/responses- 与 Gateway 使用同一端口(WS + HTTP 多路复用):
http://<gateway-host>:<port>/v1/responses
底层实现上,请求会作为普通的 Gateway Agent 运行来执行(与 openclaw agent 使用相同的代码路径),所以路由、权限和配置都与你的 Gateway 保持一致。
认证
使用 Gateway 的认证配置。发送 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
不需要自定义请求头:在 OpenResponses 的 model 字段中编码 agent id:
model: "openclaw:<agentId>"(例如:"openclaw:main"、"openclaw:beta")model: "agent:<agentId>"(别名)
或者通过请求头指定特定的 OpenClaw Agent:
x-openclaw-agent-id: <agentId>(默认:main)
高级用法:
x-openclaw-session-key: <sessionKey>完全控制 Session 路由。
启用端点
将 gateway.http.endpoints.responses.enabled 设置为 true:
{
gateway: {
http: {
endpoints: {
responses: { enabled: true },
},
},
},
}
禁用端点
将 gateway.http.endpoints.responses.enabled 设置为 false:
{
gateway: {
http: {
endpoints: {
responses: { enabled: false },
},
},
},
}
Session 行为
默认情况下,端点是每个请求无状态的(每次调用都会生成新的 session key)。
如果请求包含 OpenResponses 的 user 字符串,Gateway 会从中派生一个稳定的 session key,这样重复调用就可以共享同一个 Agent Session。
请求格式(支持的功能)
请求遵循 OpenResponses API 的基于项的输入格式。当前支持:
input:字符串或项对象数组。instructions:合并到系统 Prompt 中。tools:客户端工具定义(函数工具)。tool_choice:过滤或要求使用客户端工具。stream:启用 SSE Streaming。max_output_tokens:尽力而为的输出限制(取决于 Provider)。user:稳定的 Session 路由。
接受但当前忽略的字段:
max_tool_callsreasoningmetadatastoreprevious_response_idtruncation
项(input)
message
角色:system、developer、user、assistant。
system和developer会追加到系统 Prompt。- 最近的
user或function_call_output项会成为”当前消息”。 - 更早的 user/assistant 消息会作为历史记录包含进来,提供上下文。
function_call_output(基于回合的工具)
将工具结果发送回模型:
{
"type": "function_call_output",
"call_id": "call_123",
"output": "{\"temperature\": \"72F\"}"
}
reasoning 和 item_reference
为了模式兼容性而接受,但在构建 Prompt 时会被忽略。
工具(客户端函数工具)
使用 tools: [{ type: "function", function: { name, description?, parameters? } }] 提供工具。
如果 Agent 决定调用工具,响应会返回一个 function_call 输出项。然后你发送一个带有 function_call_output 的后续请求来继续这个回合。
图片(input_image)
支持 base64 或 URL 来源:
{
"type": "input_image",
"source": { "type": "url", "url": "https://example.com/image.png" }
}
允许的 MIME 类型(当前):image/jpeg、image/png、image/gif、image/webp。
最大大小(当前):10MB。
文件(input_file)
支持 base64 或 URL 来源:
{
"type": "input_file",
"source": {
"type": "base64",
"media_type": "text/plain",
"data": "SGVsbG8gV29ybGQh",
"filename": "hello.txt"
}
}
允许的 MIME 类型(当前):text/plain、text/markdown、text/html、text/csv、application/json、application/pdf。
最大大小(当前):5MB。
当前行为:
- 文件内容会被解码并添加到系统 Prompt,而不是用户消息,所以它是临时的(不会持久化到 Session 历史中)。
- PDF 会被解析提取文本。如果找到的文本很少,前几页会被栅格化为图片并传递给模型。
PDF 解析使用 Node 友好的 pdfjs-dist 旧版构建(无 worker)。现代的 PDF.js 构建需要浏览器 worker/DOM 全局变量,所以在 Gateway 中不使用。
URL 获取默认值:
files.allowUrl:trueimages.allowUrl:true- 请求受到保护(DNS 解析、私有 IP 阻止、重定向上限、超时)。
文件和图片限制(配置)
默认值可以在 gateway.http.endpoints.responses 下调整:
{
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:20MBfiles.maxBytes:5MBfiles.maxChars:200kfiles.maxRedirects:3files.timeoutMs:10sfiles.pdf.maxPages:4files.pdf.maxPixels:4,000,000files.pdf.minTextChars:200images.maxBytes:10MBimages.maxRedirects:3images.timeoutMs:10s
Streaming (SSE)
设置 stream: true 来接收服务器发送事件(SSE):
Content-Type: text/event-stream- 每个事件行是
event: <type>和data: <json> - 流以
data: [DONE]结束
当前发出的事件类型:
response.createdresponse.in_progressresponse.output_item.addedresponse.content_part.addedresponse.output_text.deltaresponse.output_text.doneresponse.content_part.doneresponse.output_item.doneresponse.completedresponse.failed(出错时)
用量统计
当底层 Provider 报告 Token 计数时,usage 字段会被填充。
错误
错误使用类似这样的 JSON 对象:
{ "error": { "message": "...", "type": "invalid_request_error" } }
常见情况:
401缺少或无效的认证400无效的请求体405错误的方法
示例
非流式:
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"
}'
流式:
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"
}'