POST /v1/chat/completions- 与 Gateway 相同的端口(WS + HTTP 复用):
http://<gateway-host>:<port>/v1/chat/completions
GET /v1/modelsGET /v1/models/{id}POST /v1/embeddingsPOST /v1/responses
openclaw agent 使用相同的代码路径),因此路由/权限/配置会与你的 Gateway 保持一致。
身份验证
使用 Gateway 的身份验证配置。 常见的 HTTP 认证路径:- 共享密钥认证(
gateway.auth.mode="token"或"password"):Authorization: Bearer <token-or-password> - 可信、携带身份的 HTTP 认证(
gateway.auth.mode="trusted-proxy"): 通过已配置的、可识别身份的代理进行路由,并让它注入所需的身份头 - 私有入口开放认证(
gateway.auth.mode="none"): 不需要认证头
- 当
gateway.auth.mode="token"时,使用gateway.auth.token(或OPENCLAW_GATEWAY_TOKEN)。 - 当
gateway.auth.mode="password"时,使用gateway.auth.password(或OPENCLAW_GATEWAY_PASSWORD)。 - 当
gateway.auth.mode="trusted-proxy"时,HTTP 请求必须来自 已配置的受信任代理来源;同主机 loopback 代理需要显式设置gateway.auth.trustedProxy.allowLoopback = true。 - 本机同主机的内部调用如果绕过代理,可以使用
gateway.auth.password/OPENCLAW_GATEWAY_PASSWORD作为本地直接 回退方案。任何Forwarded、X-Forwarded-*或X-Real-IP头的证据 都会让请求保持在 trusted-proxy 路径上。 - 如果配置了
gateway.auth.rateLimit且出现过多认证失败,端点会返回带有Retry-After的429。
安全边界(重要)
请将此端点视为该 gateway 实例的完整运维者访问入口。- 这里的 HTTP bearer 认证不是细粒度的按用户范围模型。
- 用于此端点的有效 Gateway token/password 应视为 owner/operator 凭据。
- 通过该端点的请求会走与受信任运维操作相同的控制平面 agent 路径。
- 此端点没有单独的非 owner/按用户工具边界;一旦调用方通过了 Gateway 认证,OpenClaw 就会将其视为该 gateway 的受信任运维者。
- 对于共享密钥认证模式(
token和password),即使调用方发送了更窄的x-openclaw-scopes头,端点也会恢复正常的完整运维者默认值。 - 可信、携带身份的 HTTP 模式(例如 trusted proxy 认证或
gateway.auth.mode="none")在存在x-openclaw-scopes时会予以尊重,否则回退到正常的运维者默认 scope 集。 - 如果目标 agent 策略允许敏感工具,此端点可以使用它们。
- 请仅在 loopback/tailnet/private ingress 上保留此端点;不要将其直接暴露给公共互联网。
gateway.auth.mode="token"或"password"+Authorization: Bearer ...- 证明持有共享的 gateway 运维者密钥
- 忽略更窄的
x-openclaw-scopes - 恢复完整的默认运维者 scope 集:
operator.admin,operator.approvals,operator.pairing,operator.read,operator.talk.secrets,operator.write - 将此端点上的聊天轮次视为 owner-sender 轮次
- 可信、携带身份的 HTTP 模式(例如 trusted proxy 认证,或私有入口上的
gateway.auth.mode="none")- 认证某个外层受信任身份或部署边界
- 当头部存在时,尊重
x-openclaw-scopes - 当头部不存在时,回退到正常的运维者默认 scope 集
- 只有当调用方显式缩窄 scopes 且省略
operator.admin时,才会失去 owner 语义
何时使用此端点
当你要把工具或受信任的应用侧后端集成到现有 gateway,并且可以安全地持有 gateway 运维者凭据时,请使用/v1/chat/completions。
- 当你的集成只是同一个 gateway 的另一个运维者/客户端表面时,优先使用它,而不是新增一个内建通道。
- 对于直接连接远程 gateway 的原生移动客户端,优先使用 WebChat 或 Gateway Protocol,并实现配对设备启动/设备令牌流程,这样设备就不需要共享 HTTP token/password。
- 如果你是在把外部消息网络与其自身的用户、房间、webhook 投递或出站传输集成起来,那么应改为构建一个通道插件。参见 Building plugins。
以 Agent 为先的模型契约
OpenClaw 将 OpenAI 的model 字段视为一个agent 目标,而不是原始的 provider model id。
model: "openclaw"会路由到已配置的默认 agent。model: "openclaw/default"也会路由到已配置的默认 agent。model: "openclaw/<agentId>"会路由到指定的 agent。
x-openclaw-model: <provider/model-or-bare-id>为所选 agent 覆盖后端模型。x-openclaw-agent-id: <agentId>仍然受支持,作为兼容性覆盖。x-openclaw-session-key: <sessionKey>完全控制会话路由。x-openclaw-message-channel: <channel>为感知 channel 的提示词和策略设置合成的入口 channel 上下文。
model: "openclaw:<agentId>"model: "agent:<agentId>"
启用端点
将gateway.http.endpoints.chatCompletions.enabled 设置为 true:
禁用端点
将gateway.http.endpoints.chatCompletions.enabled 设置为 false:
会话行为
默认情况下,此端点是每次请求无状态的(每次调用都会生成一个新的 session key)。 如果请求包含 OpenAI 的user 字符串,Gateway 会据此派生一个稳定的 session key,因此重复调用可以共享同一个 agent 会话。
对于自定义应用,最安全的默认做法是在同一条对话线程中重复使用相同的 user 值。除非你明确希望多个对话或设备共享一个 OpenClaw 会话,否则不要使用账户级标识符。当你需要在多个客户端或线程之间进行显式路由控制时,请使用 x-openclaw-session-key。
为什么这个接口很重要
对于自托管前端和工具来说,这是兼容性最高、收益最大的功能集合:- 大多数 Open WebUI、LobeChat 和 LibreChat 配置都期望有
/v1/models。 - 许多 RAG 系统期望有
/v1/embeddings。 - 现有的 OpenAI 聊天客户端通常可以先从
/v1/chat/completions开始。 - 越来越多更偏 agent 原生的客户端开始更喜欢
/v1/responses。
模型列表与 agent 路由
`/v1/models` 返回什么?
`/v1/models` 返回什么?
一个 OpenClaw 的 agent 目标列表。返回的 ids 是
openclaw、openclaw/default 和 openclaw/<agentId> 条目。
可直接将它们作为 OpenAI 的 model 值使用。`/v1/models` 会列出 agents 还是子 agents?
`/v1/models` 会列出 agents 还是子 agents?
它列出的是顶层 agent 目标,而不是后端 provider 模型,也不是子 agents。子 agents 仍然是内部执行拓扑,不会作为伪模型出现在列表中。
为什么包含 `openclaw/default`?
为什么包含 `openclaw/default`?
openclaw/default 是已配置默认 agent 的稳定别名。这意味着即使真实的默认 agent id 在不同环境之间发生变化,客户端也可以继续使用一个可预测的 id。如何覆盖后端模型?
如何覆盖后端模型?
使用
x-openclaw-model。示例:
x-openclaw-model: openai/gpt-5.4
x-openclaw-model: gpt-5.5如果省略它,所选 agent 会使用其正常配置的模型选择运行。embeddings 如何适配这个契约?
embeddings 如何适配这个契约?
/v1/embeddings 使用相同的 agent 目标 model ids。使用 model: "openclaw/default" 或 model: "openclaw/<agentId>"。
当你需要特定的 embedding 模型时,请将其放在 x-openclaw-model 中。
如果没有该头,请求会传递到所选 agent 的正常 embedding 配置。流式传输(SSE)
设置stream: true 以接收 Server-Sent Events(SSE):
Content-Type: text/event-stream- 每一条事件行都是
data: <json> - 流结束时发送
data: [DONE]
Chat 工具契约
/v1/chat/completions 支持与常见 OpenAI Chat 客户端兼容的函数工具子集。
支持的请求字段
tools: array of{ "type": "function", "function": { ... } }tool_choice:"auto","none","required", or{ "type": "function", "function": { "name": "..." } }messages[*].role: "tool"follow-up turnsmessages[*].tool_call_idfor binding tool results back to a prior tool callmax_completion_tokens: number; per-call cap for total completion tokens (reasoning tokens included). Current OpenAI Chat Completions field name; preferred when bothmax_completion_tokensandmax_tokensare sent.max_tokens: number; legacy alias accepted for backwards compatibility. Ignored whenmax_completion_tokensis also present.temperature: number; best-effort sampling temperature forwarded to the upstream provider via the agent stream-param channel.top_p: number; best-effort nucleus sampling forwarded to the upstream provider via the agent stream-param channel.frequency_penalty: number; best-effort frequency penalty forwarded to the upstream provider via the agent stream-param channel. Validated range: -2.0 to 2.0. Returns400 invalid_request_errorfor out-of-range values.presence_penalty: number; best-effort presence penalty forwarded to the upstream provider via the agent stream-param channel. Validated range: -2.0 to 2.0. Returns400 invalid_request_errorfor out-of-range values.seed: number (integer); best-effort seed forwarded to the upstream provider via the agent stream-param channel. Returns400 invalid_request_errorfor non-integer values.stop: string or array of up to 4 strings; best-effort stop sequences forwarded to the upstream provider via the agent stream-param channel. Returns400 invalid_request_errorfor more than 4 sequences or non-string/empty entries.
max_completion_tokens for OpenAI-family endpoints, and max_tokens for providers that only accept the legacy name (such as Mistral and Chutes). Sampling fields (temperature, top_p, frequency_penalty, presence_penalty, seed) follow the same stream-param channel; the ChatGPT-based Codex Responses backend strips them server-side since it uses fixed sampling. stop also rides the stream-param channel and maps to the transport’s stop field (stop for Chat Completions backends, stop_sequences for Anthropic); the OpenAI Responses API has no stop parameter, so stop is not applied on Responses-backed models.
不支持的变体
端点会针对不支持的工具变体返回400 invalid_request_error,包括:
- non-array
tools - non-function tool entries
- missing
tool.function.name tool_choicevariants such asallowed_toolsandcustomtool_choice.function.namevalues that do not match providedtools
tool_choice: "required" and function-pinned tool_choice, the endpoint narrows the exposed client function-tool set, instructs the runtime to call a client tool before responding, and returns an error if the agent response does not include a matching structured client-tool call. This contract applies to the caller-supplied HTTP tools list, not every internal OpenClaw agent tool.
Non-streaming tool response shape
当 agent 决定调用工具时,响应使用:choices[0].finish_reason = "tool_calls"choices[0].message.tool_calls[]条目包含:idtype: "function"function.namefunction.arguments(JSON 字符串)
choices[0].message.content 中返回(可能为空)。
流式工具响应形状
当stream: true 时,工具调用会以增量 SSE 分块形式发出:
- 初始 assistant role delta
- 可选的 assistant 评论 deltas
- 一个或多个携带工具身份和参数片段的
delta.tool_calls分块 - 最终分块,
finish_reason: "tool_calls" data: [DONE]
stream_options.include_usage=true,则会在 [DONE] 之前发出一个尾随 usage 分块。
工具后续循环
在收到tool_calls 后,客户端应执行请求的函数,并发送一条后续请求,其中包括:
- 先前的 assistant tool-call 消息
- 一条或多条
role: "tool"消息,且tool_call_id匹配
Open WebUI 快速设置
用于基本的 Open WebUI 连接:- Base URL:
http://127.0.0.1:18789/v1 - macOS 上 Docker 的 Base URL:
http://host.docker.internal:18789/v1 - API key: 你的 Gateway bearer token
- Model:
openclaw/default
GET /v1/models应该列出openclaw/default- Open WebUI 应使用
openclaw/default作为聊天模型 id - 如果你希望该 agent 使用特定的后端 provider/model,请设置该 agent 的正常默认模型,或发送
x-openclaw-model
openclaw/default,那么大多数 Open WebUI 配置都可以使用相同的 base URL 和 token 进行连接。
示例
某个应用对话的稳定 session:user 值,以继续同一个 agent 会话。
非流式:
/v1/models返回的是 OpenClaw agent 目标,而不是原始 provider 目录。openclaw/default始终存在,因此同一个稳定 id 可以跨环境工作。- 后端 provider/model 覆盖应放在
x-openclaw-model中,而不是 OpenAI 的model字段里。 /v1/embeddings支持input为字符串或字符串数组。