OpenCode 配置 XAI Router(Responses 优先 + Chat)
Posted January 10, 2026 by XAI 技术团队 ‐ 2 min read
OpenCode(opencode)可以通过 XAI Router 统一接入不同模型。对于 gpt-5.4,推荐走 OpenCode 自己的 Responses 请求形态,再由 XAI Router 识别并做最小必要兼容;对于普通 OpenAI 兼容模型,则继续走标准兼容 API。本文只保留两套最常用、最可靠的配置:
gpt-5.4(Responses API)MiniMax-M2.5(Chat API)
先决条件
- 已在 m.xairouter.com 创建 API Key。
- 本地已安装
opencode。
配置文件位置
- Linux/macOS:
~/.config/opencode/opencode.jsonc - Windows:
%USERPROFILE%\.config\opencode\opencode.jsonc
同一时间只保留一套配置。切换方案时,直接覆盖 opencode.jsonc。
重要说明
- 本文使用的是 OpenCode 的
openaiprovider,并把baseURL指向https://api.xairouter.com/v1。 - 对于
gpt-5.4,请在openai/gpt-5.4模型上显式添加headers.originator = "opencode"。这样codex-cloud才能明确识别 OpenCode 请求,并只对这类请求应用最小必要的 Responses shim。 - 如果没有这个 header,请求会回退到通用 Responses 转换路径;能用,但不是本文推荐的 OpenCode 定向方案。
- 如果你启用了 OpenCode 自带的 Codex OAuth 插件,它默认会直连
chatgpt.com/backend-api/codex/responses,不会经过 XAI Router;想走 XAI Router,就使用本文这套openaiprovider 配置。
方案 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 "你好"快速判断用哪套
- 你要用
gpt-5.4:选方案 A,并保留headers.originator = "opencode"。 - 你要用
MiniMax-M2.5:选方案 B。 - 只保留一个
opencode.jsonc,不要同时混用两套配置。