OpenCode 配置 XAI Router(Responses 优先 + Chat)

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

OpenCodeopencode)可以通过 XAI Router 统一接入不同模型。对于 gpt-5.4,推荐走 OpenCode 自己的 Responses 请求形态,再由 XAI Router 识别并做最小必要兼容;对于普通 OpenAI 兼容模型,则继续走标准兼容 API。本文只保留两套最常用、最可靠的配置:

  1. gpt-5.4(Responses API)
  2. MiniMax-M2.5(Chat API)

先决条件

  1. 已在 m.xairouter.com 创建 API Key。
  2. 本地已安装 opencode

配置文件位置

  1. Linux/macOS:~/.config/opencode/opencode.jsonc
  2. Windows:%USERPROFILE%\.config\opencode\opencode.jsonc

同一时间只保留一套配置。切换方案时,直接覆盖 opencode.jsonc


重要说明

  1. 本文使用的是 OpenCode 的 openai provider,并把 baseURL 指向 https://api.xairouter.com/v1
  2. 对于 gpt-5.4,请在 openai/gpt-5.4 模型上显式添加 headers.originator = "opencode"。这样 codex-cloud 才能明确识别 OpenCode 请求,并只对这类请求应用最小必要的 Responses shim。
  3. 如果没有这个 header,请求会回退到通用 Responses 转换路径;能用,但不是本文推荐的 OpenCode 定向方案。
  4. 如果你启用了 OpenCode 自带的 Codex OAuth 插件,它默认会直连 chatgpt.com/backend-api/codex/responses,不会经过 XAI Router;想走 XAI Router,就使用本文这套 openai provider 配置。

方案 A:gpt-5.4(Responses 优先路径)

Linux/macOS

export XAI_API_KEY="sk-xxx"
mkdir -p ~/.config/opencode
cat > ~/.config/opencode/opencode.jsonc << 'JSON'
{
  "$schema": "https://opencode.ai/config.json",
  "model": "openai/gpt-5.4",
  "small_model": "openai/gpt-5.4",
  "provider": {
    "openai": {
      "options": {
        "baseURL": "https://api.xairouter.com/v1",
        "apiKey": "{env:XAI_API_KEY}"
      },
      "models": {
        "gpt-5.4": {
          "headers": {
            "originator": "opencode"
          }
        }
      }
    }
  }
}
JSON

opencode debug config
opencode run "你好"

Windows(PowerShell)

$env:XAI_API_KEY="sk-xxx"
New-Item -ItemType Directory -Path "$env:USERPROFILE\.config\opencode" -Force | Out-Null
@'
{
  "$schema": "https://opencode.ai/config.json",
  "model": "openai/gpt-5.4",
  "small_model": "openai/gpt-5.4",
  "provider": {
    "openai": {
      "options": {
        "baseURL": "https://api.xairouter.com/v1",
        "apiKey": "{env:XAI_API_KEY}"
      },
      "models": {
        "gpt-5.4": {
          "headers": {
            "originator": "opencode"
          }
        }
      }
    }
  }
}
'@ | Set-Content -Path "$env:USERPROFILE\.config\opencode\opencode.jsonc" -Encoding utf8

opencode debug config
opencode run "你好"

方案 B:MiniMax-M2.5(Chat API)

Linux/macOS

export XAI_API_KEY="sk-xxx"
mkdir -p ~/.config/opencode
cat > ~/.config/opencode/opencode.jsonc << 'JSON'
{
  "$schema": "https://opencode.ai/config.json",
  "model": "xai-chat/MiniMax-M2.5",
  "small_model": "xai-chat/MiniMax-M2.5",
  "provider": {
    "xai-chat": {
      "npm": "@ai-sdk/openai-compatible",
      "options": {
        "baseURL": "https://api.xairouter.com/v1",
        "apiKey": "{env:XAI_API_KEY}"
      },
      "models": {
        "MiniMax-M2.5": {}
      }
    }
  }
}
JSON

opencode debug config
opencode run "你好"

Windows(PowerShell)

$env:XAI_API_KEY="sk-xxx"
New-Item -ItemType Directory -Path "$env:USERPROFILE\.config\opencode" -Force | Out-Null
@'
{
  "$schema": "https://opencode.ai/config.json",
  "model": "xai-chat/MiniMax-M2.5",
  "small_model": "xai-chat/MiniMax-M2.5",
  "provider": {
    "xai-chat": {
      "npm": "@ai-sdk/openai-compatible",
      "options": {
        "baseURL": "https://api.xairouter.com/v1",
        "apiKey": "{env:XAI_API_KEY}"
      },
      "models": {
        "MiniMax-M2.5": {}
      }
    }
  }
}
'@ | Set-Content -Path "$env:USERPROFILE\.config\opencode\opencode.jsonc" -Encoding utf8

opencode debug config
opencode run "你好"

快速判断用哪套

  1. 你要用 gpt-5.4:选方案 A,并保留 headers.originator = "opencode"
  2. 你要用 MiniMax-M2.5:选方案 B。
  3. 只保留一个 opencode.jsonc,不要同时混用两套配置。