AnRouter

在 OpenCode 中配置 AnRouter

在 OpenCode 桌面 App、TUI 或 opencode.json 中配置 AnRouter,并验证连通性。

OpenCode 可通过 @ai-sdk/openai-compatible 连接 Chat Completions。TUI、桌面 App 与 IDE 界面可以复用同一 provider 配置。

1. 获取 AnRouter API Key

  • 在 anrouter.com 注册并登录。
  • 进入控制台,打开 API Keys 页面。
  • 创建密钥并复制。请像对待密码一样保管,切勿提交到代码仓库。

2. 桌面 App:添加自定义 provider

  1. 打开 OpenCode Desktop 的 provider 设置,选择添加自定义 provider。
  2. Provider ID 填 anrouter,显示名称填 AnRouter。
  3. Base URL 填 https://api.anrouter.com/v1,API Key 填你的 AnRouter 密钥。
  1. 在 Models 中添加模型,模型 ID 必须与 AnRouter 模型页或 GET /v1/models 返回完全一致,例如 glm-5.3。
  2. 没有特殊需求时,请求 Headers 留空,然后提交并在模型选择器中选择 anrouter/glm-5.3。
英文界面的 OpenCode Desktop 自定义 provider 与模型配置

3. TUI / CLI:保存凭据

  1. 启动 OpenCode,在 TUI 中输入 /connect。
  2. 选择 Other,Provider ID 填 anrouter。
  3. 输入 AnRouter API Key。该步骤只保存凭据,还需要下面的 provider 配置。

4. 手动配置 provider

OpenCode 图片附件:自定义 provider 的模型若未声明图片能力,会被 OpenCode 当作纯文本模型。需要为每个要接收图片的 AnRouter 模型同时保留 "attachment": true 与 "modalities": { "input": ["text", "image"], "output": ["text"] }。若省略这些字段,OpenCode 可能仍显示并压缩图片,但会在请求发往 API 前移除图片,模型因此会回答无法查看图片。
{
  "$schema": "https://opencode.ai/config.json",
  "model": "anrouter/glm-5.3",
  "provider": {
    "anrouter": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "AnRouter",
      "options": {
        "baseURL": "https://api.anrouter.com/v1"
      },
      "models": {
        "deepseek-v4.1-flash-1017": {
          "name": "DeepSeek V4.1 Flash",
          "attachment": true,
          "modalities": {
            "input": [
              "text",
              "image"
            ],
            "output": [
              "text"
            ]
          }
        },
        "glm-5.2": {
          "name": "GLM 5.2",
          "attachment": true,
          "modalities": {
            "input": [
              "text",
              "image"
            ],
            "output": [
              "text"
            ]
          }
        },
        "glm-5.3": {
          "name": "GLM 5.3",
          "attachment": true,
          "modalities": {
            "input": [
              "text",
              "image"
            ],
            "output": [
              "text"
            ]
          }
        },
        "glm-5.3-flash": {
          "name": "GLM 5.3 Flash",
          "attachment": true,
          "modalities": {
            "input": [
              "text",
              "image"
            ],
            "output": [
              "text"
            ]
          }
        },
        "minimax-m3.1-flash": {
          "name": "MiniMax M3.1 Flash",
          "attachment": true,
          "modalities": {
            "input": [
              "text",
              "image"
            ],
            "output": [
              "text"
            ]
          }
        },
        "qwen3.8-27b": {
          "name": "Qwen 3.8 27B",
          "attachment": true,
          "modalities": {
            "input": [
              "text",
              "image"
            ],
            "output": [
              "text"
            ]
          }
        },
        "qwen3.8-flash-next": {
          "name": "Qwen 3.8 Flash Next",
          "attachment": true,
          "modalities": {
            "input": [
              "text",
              "image"
            ],
            "output": [
              "text"
            ]
          }
        },
        "mimo-v2.6-pro": {
          "name": "MiMo V2.6 Pro",
          "attachment": true,
          "modalities": {
            "input": [
              "text",
              "image"
            ],
            "output": [
              "text"
            ]
          }
        },
        "mimo-v2.6-flash": {
          "name": "MiMo V2.6 Flash",
          "attachment": true,
          "modalities": {
            "input": [
              "text",
              "image"
            ],
            "output": [
              "text"
            ]
          }
        }
      }
    }
  }
}

项目配置放在仓库根目录 opencode.json;个人全局配置可放在 ~/.config/opencode/opencode.json。本示例不硬编码 context/output 上限,避免客户端元数据与服务端实际能力不一致。

必须重启:修改模型能力后,请完全退出并重新打开 OpenCode。仅新建会话或刷新页面不足以更新启动时缓存的模型能力。

5. 验证

opencode auth list
opencode

# 在 TUI 中
/models
协议选择:@ai-sdk/openai-compatible 对应 /v1/chat/completions。只有明确要走 Responses 的模型/provider 才改用 @ai-sdk/openai。

参考:OpenCode 自定义 provider