OpenCode 配置指南

OpenCode 是一个开源的 AI 编程助手,支持多种 AI 模型。本指南将帮助你配置 OpenCode 以使用 Du's API。

前置要求

  • 已安装 OpenCode
  • 拥有 Du's API 的 API Key

配置方式

OpenCode 使用 JSON 配置文件进行配置。配置文件通常位于项目根目录,名为 opencode.json

基础配置

OpenAI 模型配置

创建 opencode.json 文件:

{
  "provider": {
    "openai": {
      "options": {
        "baseURL": "https://api.dusapi.com",
        "apiKey": "your-api-key-here"
      },
      "models": {
        "gpt-5.4": {
          "name": "GPT-5.4",
          "limit": {
            "context": 1050000,
            "output": 128000
          },
          "options": {
            "store": false
          },
          "variants": {
            "low": {},
            "medium": {},
            "high": {},
            "xhigh": {}
          }
        },
        "gpt-5.3-codex": {
          "name": "GPT-5.3 Codex",
          "limit": {
            "context": 400000,
            "output": 128000
          },
          "options": {
            "store": false
          },
          "variants": {
            "low": {},
            "medium": {},
            "high": {},
            "xhigh": {}
          }
        }
      }
    }
  },
  "agent": {
    "build": {
      "options": {
        "store": false
      }
    },
    "plan": {
      "options": {
        "store": false
      }
    }
  },
  "$schema": "https://opencode.ai/config.json"
}

Anthropic (Claude) 模型配置

{
  "provider": {
    "anthropic": {
      "npm": "@ai-sdk/anthropic",
      "options": {
        "baseURL": "https://api.dusapi.com",
        "apiKey": "your-api-key-here"
      }
    }
  },
  "$schema": "https://opencode.ai/config.json"
}

多模型配置

OpenCode 的一大优势是可以同时配置多个模型提供商:

{
  "provider": {
    "openai": {
      "options": {
        "baseURL": "https://api.dusapi.com",
        "apiKey": "your-api-key-here"
      },
      "models": {
        "gpt-5.4": {
          "name": "GPT-5.4",
          "limit": {
            "context": 1050000,
            "output": 128000
          },
          "options": {
            "store": false
          }
        },
        "gpt-5.3-codex": {
          "name": "GPT-5.3 Codex",
          "limit": {
            "context": 400000,
            "output": 128000
          },
          "options": {
            "store": false
          }
        }
      }
    },
    "anthropic": {
      "npm": "@ai-sdk/anthropic",
      "options": {
        "baseURL": "https://api.dusapi.com",
        "apiKey": "your-api-key-here"
      }
    }
  },
  "agent": {
    "build": {
      "options": {
        "store": false
      }
    },
    "plan": {
      "options": {
        "store": false
      }
    }
  },
  "$schema": "https://opencode.ai/config.json"
}

配置说明

必填字段

  • provider: 模型提供商配置
    • options.baseURL: API 基础 URL
    • options.apiKey: 你的 API Key

可选字段

  • npm: NPM 包名称(用于某些提供商)
  • name: 提供商显示名称
  • models: 模型详细配置
    • name: 模型显示名称
    • limit.context: 上下文窗口大小
    • limit.output: 最大输出长度
    • options: 模型特定选项
    • variants: 模型变体(如 low/medium/high/xhigh)

验证配置

配置完成后,启动 OpenCode 并尝试使用配置的模型。

常见问题

配置文件放在哪里?

通常放在项目根目录,命名为 opencode.json。OpenCode 会自动查找并加载此文件。

如何切换模型?

在 OpenCode 的界面中选择不同的模型提供商和模型。

多个 API Key 怎么配置?

如果你有多个 API Key(例如不同的账号),可以为不同的提供商配置不同的 Key:

{
  "provider": {
    "openai": {
      "options": {
        "baseURL": "https://api.dusapi.com",
        "apiKey": "key-for-openai"
      }
    },
    "anthropic": {
      "options": {
        "baseURL": "https://api.dusapi.com",
        "apiKey": "key-for-anthropic"
      }
    }
  }
}

配置后无法连接

  1. 检查 JSON 格式是否正确(可以使用 JSON 验证工具)
  2. 确认 API Key 是否正确
  3. 检查 Base URL 是否正确
  4. 确认网络连接是否正常

完整配置示例

这是一个包含所有主要模型的完整配置示例:

{
  "provider": {
    "openai": {
      "options": {
        "baseURL": "https://api.dusapi.com",
        "apiKey": "your-api-key-here"
      },
      "models": {
        "gpt-5.4": {
          "name": "GPT-5.4",
          "limit": { "context": 1050000, "output": 128000 },
          "options": { "store": false },
          "variants": {
            "low": {},
            "medium": {},
            "high": {},
            "xhigh": {}
          }
        },
        "gpt-5.3-codex": {
          "name": "GPT-5.3 Codex",
          "limit": { "context": 400000, "output": 128000 },
          "options": { "store": false },
          "variants": {
            "low": {},
            "medium": {},
            "high": {},
            "xhigh": {}
          }
        },
        "gpt-5.3-codex-spark": {
          "name": "GPT-5.3 Codex Spark",
          "limit": { "context": 128000, "output": 32000 },
          "options": { "store": false },
          "variants": {
            "low": {},
            "medium": {},
            "high": {},
            "xhigh": {}
          }
        }
      }
    },
    "anthropic": {
      "npm": "@ai-sdk/anthropic",
      "options": {
        "baseURL": "https://api.dusapi.com",
        "apiKey": "your-api-key-here"
      }
    }
  },
  "agent": {
    "build": {
      "options": {
        "store": false
      }
    },
    "plan": {
      "options": {
        "store": false
      }
    }
  },
  "$schema": "https://opencode.ai/config.json"
}
提示

your-api-key-here 替换为你的实际 API Key。如果所有模型使用同一个 Key,可以使用相同的值。

下一步