Gateway 与上下文
Gateway 启停与 context。
Gateway 与上下文
这一课你将能做到
- 熟练使用
openclaw gateway的 run / install / start / stop / restart / status 全生命周期命令 - 解释 Gateway 的绑定模式(loopback / lan / tailnet)与鉴权方式(token / password)
- 理解上下文窗口与会话压缩(compaction)对长会话质量的影响
为什么要学
Gateway 是 OpenClaw 的心脏:渠道连着它、模型调用经过它、会话状态存在它手里。团队 bot 出问题时——「bot 不回了」「群里消息漏了」「重启后失忆」——九成是 Gateway 层的问题。不会运维 Gateway,等于把 bot 当黑盒供着。
人话:Gateway 之于 OpenClaw,相当于 Nginx 之于你的 Web 服务——流量入口、状态枢纽、故障第一现场。
先建立图像
┌─────────────── Gateway (127.0.0.1:18789) ───────────────┐
│ │
渠道连接 ──┤ Telegram ─┐ ┌── 会话 A 上下文 ─┤
(WS/出站) ├─ Slack ──┼──→ 路由 ──→ Agent 循环 ──┼── 会话 B 上下文 ─┤
├─ Discord ──┘ │ └── 会话 C 上下文 ─┘
│ │
│ 上下文窗口(有限,超了要 compact)
│
└── 配置: ~/.openclaw/openclaw.json
要求: gateway.mode = "local"
关键点:每个会话维护自己的上下文窗口——模型能「记住」的对话历史。窗口有 token 上限,聊得久了必须压缩(compaction),把旧对话摘要成要点,否则要么爆窗要么被截断。
跟着做
步骤 1:前台跑一次看日志
openclaw gateway run --verbose
run 是显式形式(裸 openclaw gateway 等价)。前台模式适合开发调试,日志直接打在终端。Ctrl+C 停止。
步骤 2:服务化运行
openclaw gateway install # 注册系统服务(LaunchAgent/systemd/计划任务)
openclaw gateway status # 基础状态
openclaw gateway status --deep # 深度检查:含 RPC 探测
步骤 3:日常运维三板斧
openclaw gateway restart # 改配置后重启
openclaw gateway stop # 停止(Linux 上会抑制 systemd 自动拉起,直到再次 start)
openclaw gateway start # 启动(会先校验配置有效性)
步骤 4:改端口与绑定模式
临时改(命令行覆盖):
openclaw gateway run --port 19000 --bind loopback
永久改(写配置):
openclaw config set gateway.port 19000
openclaw config set gateway.bind loopback
openclaw gateway restart
绑定模式:loopback(默认,只本机)、lan(局域网可达)、tailnet(Tailscale 网络)、custom。绑定到非 loopback 时必须配置鉴权( 或 ),否则拒绝启动——这是防裸奔的硬保护。