OpenCode 接入新算力:new-api 多渠道配置与性能测试
公司在新服务器上部署了一批 AI 模型(Qwen 3.7、GLM 5.1、Qwen 3.6 等),通过 new-api 对外提供统一入口,技术部门每人一个 API key。以下是一步步的操作指南——先申请 key,再交给 opencode,它自动拉模型、写配置,完成接入。
怎么和 AI 新同事协作
新同事入职——这里指的是你给 opencode 新建了一个会话,它还不认识公司的算力。你需要给它一份「入职材料」。如果你对工具配置不熟悉,按下面四步走就行。
第一步:申请 API key
联系技术管理员(或者负责 new-api 网关的同事),申请你的专属 key。你会拿到两样东西:
- 网关地址(URL):类似
https://your-gateway.example.com - API 密钥(Key):类似
sk-xxxxxxxxxxxxxxxx
确认到手后再往下走。
第二步:发连接信息和提示词
终端输入 opencode 新建会话,分两次发出。
先发连接信息:
{"_type":"newapi_channel_conn","key":"sk-xxx","url":"https://your-gateway.example.com"}
再把下面这段提示词也发过去。opencode 收到后会自己拉模型列表、查官方文档确定规格、写入 ~/.config/opencode/opencode.jsonc——整个过程不需要你手改一行 JSON:
我的初始信息如下:
{"_type":"newapi_channel_conn","key":"sk-xxx","url":"https://your-gateway.example.com"}
_type 说明:这是 new-api 网关的连接信息协议,只有 key 和 url 两个字段。url 是 OpenAI 兼容接口的基地址。
请按以下步骤操作:
1. 用 key 和 url 拉取模型列表(GET {url}/v1/models,Header: Authorization: Bearer {key}),过滤掉非聊天模型(embedding、image、moderation 等)。
2. 对剩下的聊天模型,查以下官方文档确认规格,列成表格:
- 上下文长度、最大输出(按官方文档填,不要猜)
- 是否支持 reasoning
- 推理等级参数名称和取值范围。不同厂商参数不同:
DeepSeek 用 reasoning_effort(low/high)和 budget_tokens(数值型)
Qwen 用 enable_thinking(布尔)和 thinking.budget_tokens(数值型,qwen3.7-max 思考预算上限 256k)
GLM 用 thinking.type(enabled/disabled)
查不到的标记「未知」
3. 按表格内容写入 ~/.config/opencode/opencode.jsonc:
- provider 名:取 url 域名前缀(如 your-gateway.example.com → company-gateway)
- npm:@ai-sdk/openai-compatible,baseURL:{url}/v1,apiKey:{key}
- 每个模型:reasoning 按表格设,attachment 默认 false(kimi-for-coding 设 true)
- limit 按表格填,timeout 300000
- 默认模型选表格里 TTFT 最低的那个(opencode 会用它做日常编码)
4. 切换到新配置,逐个模型发 "say hello" 验证。不通过的最多回退两次,仍然失败则跳过并标记。
5. 汇总输出:可用模型列表、推荐场景、推理等级用法、切换命令。
参考文档:
- DeepSeek: https://api-docs.deepseek.com/quick_start/pricing
- 阿里云百炼: https://help.aliyun.com/zh/model-studio/text-generation-model/
- 智谱 GLM: https://docs.bigmodel.cn/cn/guide/models/text/glm-5.1.md
- Kimi: https://platform.kimi.com/docs/pricing/chat-k26
- OpenCode 配置: https://opencode.ai/config.json第三步:验证配置
让 opencode 逐个模型发 "say hello" 来确认全部可用。如果有模型不通,它会自己排查是 key 的问题、路由的问题还是模型名写错了。
第四步:跑性能测试(可选)
如果你想知道每个模型的实际快慢,在第三步之后追加性能测试提示词(本文「进阶」一节)。opencode 会自己写脚本、自己跑、自己出报告——你只需要看表格选模型。
本质上是把新算力的接入工作从「人查文档 + 人改配置」变成了「AI 查文档 + AI 改配置」。人的角色从操作者变成了审核者:看一眼 opencode 改完的配置对不对,看一眼测试报告哪个模型最快,就完了。
预期效果
opencode 执行这段提示词后,opencode.jsonc 会变成类似这样的结构:
{
"model": "company-gateway/deepseek-v4-flash",
"provider": {
"company-gateway": {
"npm": "@ai-sdk/openai-compatible",
"options": {
"baseURL": "https://your-gateway.example.com/v1",
"apiKey": "sk-xxx",
"timeout": 300000
},
"models": {
"deepseek-v4-flash": {
"name": "DeepSeek V4 Flash",
"reasoning": true,
"limit": { "context": 1048576, "output": 393216 }
},
"deepseek-v4-pro": {
"name": "DeepSeek V4 Pro",
"reasoning": true,
"limit": { "context": 1048576, "output": 393216 }
},
"qwen3.7-max": {
"name": "Qwen 3.7 Max",
"reasoning": true,
"limit": { "context": 1048576, "output": 65536 }
},
"qwen3.7-plus": {
"name": "Qwen 3.7 Plus",
"reasoning": true,
"limit": { "context": 1048576, "output": 65536 }
},
"glm-5.1": {
"name": "GLM 5.1",
"reasoning": true,
"limit": { "context": 204800, "output": 131072 }
},
"kimi-for-coding": {
"name": "Kimi For Coding",
"reasoning": true,
"attachment": true,
"limit": { "context": 262144, "output": 98304 }
},
"Qwen3.6-bf16": {
"name": "Qwen 3.6 BF16",
"reasoning": true,
"limit": { "context": 1048576, "output": 65536 }
},
"Qwen3.6-fp8": {
"name": "Qwen 3.6 FP8",
"reasoning": true,
"limit": { "context": 1048576, "output": 65536 }
}
}
}
}
}进阶:带性能测试的提示词
如果接入后还想知道各模型的真实性能,可以在上面提示词的基础上追加:
6. 写一个 Python 测试脚本,用标准库实现(不用 pip 装任何包),测试两件事:
a) 速度测试:每个模型用同一个 prompt("写一篇 500 字的中文短文")发流式请求 3 次,记 TTFT、总耗时、输出 token 数、tokens/s,取平均
b) 并发测试:每模型用非流式请求(max_tokens=1024),测并发 1、5、10 三档,记成功率、延迟、吞吐
8 个模型全部并行跑
7. 跑完测试,按速度排序输出两个表格(速度、并发),给每档推荐一个首选模型。
执行后会拿到完整基准:
| 模型 | TTFT(ms) | 总耗时(ms) | tok/s | 适用场景 |
|---|---|---|---|---|
| Qwen3.6-bf16 | 1,516 | 5,596 | 139.8 | 日常编程首选 |
| deepseek-v4-flash | 3,825 | 7,533 | 71.8 | 全天候通用 |
| Qwen3.6-fp8 | 10,636 | 11,857 | 190.5 | 长文本生成 |
| kimi-for-coding | 12,331 | 18,473 | 41.0 | 附件问答 |
| qwen3.7-plus | 24,814 | 28,240 | 48.5 | 批处理备选 |
| qwen3.7-max | 25,137 | 28,174 | 57.3 | 批处理备选 |
| glm-5.1 | 26,690 | 30,918 | 48.9 | 批处理备选 |
| deepseek-v4-pro | 49,448 | 53,012 | 49.0 | 深度推理 |
参考
- OpenCode — AI 编程终端
- OpenCode 配置 Schema
- new-api — 开源 AI 网关
- DeepSeek V4 模型与定价
- 阿里云百炼文本生成模型
- 智谱 GLM-5.1 模型文档
- Kimi K2.6 模型定价