Skip to content

OpenClaw Gateway网关模式详解:把OpenClaw当AI API网关用

2026年4月4日

Gateway模式是什么?

OpenClaw的两种使用方式

模式说明适用场景
Agent模式对话式交互聊天机器人、问答助手
Gateway模式API代理转发应用后端、统一AI入口

Gateway模式价值

  • 统一API入口:一个接口访问所有模型
  • 统一Key管理:集中管理API密钥
  • 统一成本统计:Token使用追踪
  • 统一模型切换:灵活切换模型

启用Gateway模式

配置Gateway

yaml
# ~/.openclaw/config.yaml

gateway:
  enabled: true
  port: 8080
  host: "0.0.0.0"
  
  # API Key验证
  auth:
    enabled: true
    keys:
      - name: app-a
        key: "sk-app-a-xxxx"
      - name: app-b
        key: "sk-app-b-xxxx"

启动Gateway服务

bash
# 启动网关
openclaw gateway start

# 或指定端口
openclaw gateway start --port 8080

Gateway API接口

兼容OpenAI格式

bash
curl http://localhost:8080/v1/chat/completions \
  -H "Authorization: Bearer sk-app-a-xxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-chat",
    "messages": [{"role": "user", "content": "你好"}]
  }'

响应格式

json
{
  "id": "chatcmpl-xxx",
  "model": "deepseek-chat",
  "choices": [{
    "message": {"role": "assistant", "content": "你好!有什么可以帮助你的?"}
  }],
  "usage": {"prompt_tokens": 10, "completion_tokens": 20, "total_tokens": 30}
}

配置模型路由

多模型配置

yaml
providers:
  deepseek:
    enabled: true
    apiKey: "${DEEPSEEK_API_KEY}"
  openai:
    enabled: true
    apiKey: "${OPENAI_API_KEY}"

gateway:
  routes:
    - name: default
      model: deepseek-chat
    - name: advanced
      model: gpt-4o

负载均衡配置

多模型负载均衡

yaml
gateway:
  loadBalance:
    strategy: round-robin  # 轮询
    models:
      - deepseek-chat
      - gpt-3.5-turbo

按权重分配

yaml
gateway:
  loadBalance:
    strategy: weighted
    models:
      - name: deepseek-chat
        weight: 80  # 80%流量
      - name: gpt-3.5-turbo
        weight: 20  # 20%流量

故障转移

yaml
gateway:
  failover:
    enabled: true
    primary: deepseek-chat
    fallback:
      - gpt-3.5-turbo
      - claude-3-5-sonnet

限流与配额

请求限流

yaml
gateway:
  rateLimit:
    enabled: true
    requestsPerMinute: 100
    tokensPerMinute: 100000

按应用配额

yaml
gateway:
  quotas:
    - name: app-a
      key: "sk-app-a-xxxx"
      limits:
        requestsPerDay: 1000
        tokensPerDay: 500000

成本统计

Token使用统计

yaml
gateway:
  analytics:
    enabled: true
    logRequests: true
    dimensions:
      - app
      - model
      - date

查看统计

bash
# 查看今日统计
openclaw gateway stats --today

# 按应用查看
openclaw gateway stats --app app-a

# 导出报告
openclaw gateway stats --export report.csv

安全配置

API Key管理

yaml
gateway:
  auth:
    enabled: true
    keys:
      - name: app-a
        key: "sk-app-a-xxxx"
        permissions:
          models:
            - deepseek-chat
            - gpt-3.5-turbo
          rateLimit:
            requestsPerMinute: 50

IP白名单

yaml
gateway:
  security:
    ipWhitelist:
      enabled: true
      allowed:
        - "127.0.0.1"
        - "192.168.1.0/24"

与应用集成

Python集成示例

python
from openai import OpenAI

client = OpenAI(
    api_key="sk-app-a-xxxx",
    base_url="http://localhost:8080/v1"
)

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[{"role": "user", "content": "你好"}]
)

print(response.choices[0].message.content)

Node.js集成示例

javascript
import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: 'sk-app-a-xxxx',
  baseURL: 'http://localhost:8080/v1'
});

const response = await client.chat.completions.create({
  model: 'deepseek-chat',
  messages: [{ role: 'user', content: '你好' }]
});

高可用部署

使用Docker部署

yaml
# docker-compose.yml
version: '3'
services:
  openclaw-gateway:
    image: openclaw/gateway:latest
    ports:
      - "8080:8080"
    volumes:
      - ./config:/root/.openclaw
    environment:
      - DEEPSEEK_API_KEY=${DEEPSEEK_API_KEY}
    restart: always

Gateway监控

健康检查

bash
curl http://localhost:8080/health

# 响应
{
  "status": "healthy",
  "uptime": 86400,
  "requests": 10000,
  "errors": 5
}

Prometheus指标

yaml
gateway:
  metrics:
    enabled: true
    port: 9090
    path: /metrics

总结

OpenClaw Gateway模式:

功能说明
统一API入口一个接口访问所有模型
统一Key管理集中管理API密钥
负载均衡多模型流量分配
故障转移主模型失败自动切换
成本统计Token使用追踪
安全控制Key权限、IP白名单

把OpenClaw当AI API网关用。

统一管理所有AI模型调用。


🦞 Claw is the law. Gateway模式,统一入口。

不要孤军奋战啦!

加入微信群一起学习交流 AI

与大神一起使用 OpenClaw、Hermes、Claude Code、Seedance 2.0、GPT-Image-2 等

微信公众号

扫码关注微信公众号
私信 "加群",将自动获取微信群二维码

探索 AI 世界,掌握智能未来