OpenCode 配置 XAI Router

Posted January 10, 2026 by XAI 技术团队 ‐ 2 min read

OpenCodeopencode)是一款面向开发者的终端/全栈编程助手。本文将教你把 opencode 的模型请求统一打到 XAI Router(xairouter)


先决条件

  1. 一个 XAI Router 账户:访问 m.xairouter.com 注册并创建 API Key。
  2. 本地已安装 opencode

步骤一:在 XAI Router 创建 API Key

  1. 登录 m.xairouter.com
  2. 进入 API Keys 页面,创建一个新的 Key(建议备注用途,如 opencode)。
  3. 复制保存该 Key,后面会作为 XAI_API_KEY 使用。

步骤二:设置环境变量

推荐使用 XAI_API_KEY

macOS / Linux:

export XAI_API_KEY="sk-xxx"

Windows PowerShell:

$env:XAI_API_KEY="sk-xxx"

步骤三:配置 opencode

创建或覆盖 ~/.config/opencode/opencode.json

cat > ~/.config/opencode/opencode.json << 'EOF'
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "XAI": {
      "options": {
        "baseURL": "https://api.xairouter.com/v1",
        "apiKey": "{env:XAI_API_KEY}"
      },
      "models": {
        "glm-4.7": {},
        "ark-code-latest": {},
        "MiniMax-M2.1": {},
        "gemini-3-pro-preview": {}
      }
    }
  },
  "plugin": ["oh-my-opencode"]
}
EOF

说明:这里使用 XAI Provider,并配置了 glm-4.7MiniMax-M2.1gemini-3-pro-preview 等模型。


步骤四:验证是否生效

opencode debug config

确认输出里包含配置的模型列表。


完成上述配置后,你的 opencode 即可通过 XAI Router 稳定调用各家大模型,密钥统一管理、可观测、可控成本。