中文
文档
提供商

Providers

在 OpenCode 中使用任意 LLM provider。

OpenCode 使用 AI SDK (opens in a new tab)Models.dev (opens in a new tab),支持 75+ 种 LLM provider,并支持运行本地模型。

要添加一个 provider,你需要:

  1. 使用 /connect 命令为该 provider 添加 API key。
  2. 在你的 OpenCode 配置中配置该 provider。

Credentials

当你用 /connect 命令添加某个 provider 的 API key 时,它们会存储在 ~/.local/share/opencode/auth.json 中。


Config

你可以通过 OpenCode 配置中的 provider 部分来自定义各个 provider。


Base URL

你可以通过设置 baseURL 选项来自定义任意 provider 的 base URL。这在使用代理服务或自定义端点时很有用。

opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "anthropic": {
      "options": {
        "baseURL": "https://api.anthropic.com/v1"
      }
    }
  }
}

OpenCode Zen

OpenCode Zen 是由 OpenCode 团队提供的一份模型清单,这些模型已经过测试并验证可与 OpenCode 良好配合。了解更多

Tip: 如果你是新手,我们推荐从 OpenCode Zen 开始。

  1. 在 TUI 中运行 /connect 命令,选择 OpenCode Zen,然后前往 opencode.ai/auth (opens in a new tab)

    /connect
  2. 登录、填写账单信息,并复制你的 API key。

  3. 粘贴你的 API key。

    ┌ API key
    
    
    └ enter
  4. 在 TUI 中运行 /models 查看我们推荐的模型列表。

    /models

它的工作方式与 OpenCode 中的其他 provider 完全一样,是否使用完全可选。


OpenCode Go

OpenCode Go 是一种低成本的订阅方案,可可靠地访问由 OpenCode 团队提供的热门开源编码模型,这些模型已经过测试并验证可与 OpenCode 良好配合。

  1. 在 TUI 中运行 /connect 命令,选择 OpenCode Go,然后前往 opencode.ai/auth (opens in a new tab)

    /connect
  2. 登录、填写账单信息,并复制你的 API key。

  3. 粘贴你的 API key。

    ┌ API key
    
    
    └ enter
  4. 在 TUI 中运行 /models 查看我们推荐的模型列表。

    /models

它的工作方式与 OpenCode 中的其他 provider 完全一样,是否使用完全可选。


Directory

我们来详细看看其中一些 provider。如果你想把某个 provider 添加到这个列表中,欢迎提交 PR。

Note: 没看到你要找的 provider?提交一个 PR 吧。


302.AI

  1. 前往 302.AI 控制台 (opens in a new tab),创建账号,并生成一个 API key。

  2. 运行 /connect 命令并搜索 302.AI

    /connect
  3. 输入你的 302.AI API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个模型。

    /models

Amazon Bedrock

在 OpenCode 中使用 Amazon Bedrock:

  1. 前往 Amazon Bedrock 控制台中的 Model catalog,申请你想使用的模型的访问权限。

    Tip: 你需要在 Amazon Bedrock 中拥有你想使用的模型的访问权限。

  2. 使用以下方法之一配置身份验证

    环境变量(快速开始)

    在运行 opencode 时设置以下环境变量之一:

    # 方式 1:使用 AWS access key
    AWS_ACCESS_KEY_ID=XXX AWS_SECRET_ACCESS_KEY=YYY opencode
     
    # 方式 2:使用命名的 AWS profile
    AWS_PROFILE=my-profile opencode
     
    # 方式 3:使用 Bedrock bearer token
    AWS_BEARER_TOKEN_BEDROCK=XXX opencode

    或者将它们添加到你的 bash profile 中:

    ~/.bash_profile
    export AWS_PROFILE=my-dev-profile
    export AWS_REGION=us-east-1

    配置文件(推荐)

    对于针对特定项目或需要持久化的配置,使用 opencode.json

    opencode.json
    {
      "$schema": "https://opencode.ai/config.json",
      "provider": {
        "amazon-bedrock": {
          "options": {
            "region": "us-east-1",
            "profile": "my-aws-profile"
          }
        }
      }
    }

    可用选项:

    • region - AWS region(例如 us-east-1eu-west-1

    • profile - 来自 ~/.aws/credentials 的 AWS 命名 profile

    • endpoint - 用于 VPC 端点的自定义端点 URL(通用 baseURL 选项的别名)

    Tip: 配置文件中的选项优先级高于环境变量。

    进阶:VPC 端点

    如果你为 Bedrock 使用 VPC 端点:

    opencode.json
    {
      "$schema": "https://opencode.ai/config.json",
      "provider": {
        "amazon-bedrock": {
          "options": {
            "region": "us-east-1",
            "profile": "production",
            "endpoint": "https://bedrock-runtime.us-east-1.vpce-xxxxx.amazonaws.com"
          }
        }
      }
    }

    Note: endpoint 选项是通用 baseURL 选项的别名,使用的是 AWS 专用术语。如果同时指定了 endpointbaseURL,则 endpoint 优先。

    身份验证方法

    • AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY :在 AWS 控制台中创建一个 IAM 用户并生成 access key
    • AWS_PROFILE :使用来自 ~/.aws/credentials 的命名 profile。先用 aws configure --profile my-profileaws sso login 进行配置
    • AWS_BEARER_TOKEN_BEDROCK :从 Amazon Bedrock 控制台生成长期 API key
    • AWS_WEB_IDENTITY_TOKEN_FILE / AWS_ROLE_ARN :用于 EKS IRSA(IAM Roles for Service Accounts)或其他带 OIDC 联合身份的 Kubernetes 环境。在使用 service account 注解时,这些环境变量会由 Kubernetes 自动注入。

    身份验证优先级

    Amazon Bedrock 使用以下身份验证优先级:

    1. Bearer Token - AWS_BEARER_TOKEN_BEDROCK 环境变量,或来自 /connect 命令的 token

    2. AWS 凭证链 - profile、access key、共享凭证、IAM 角色、Web Identity Token(EKS IRSA)、实例元数据

    Note: 当设置了 bearer token(通过 /connectAWS_BEARER_TOKEN_BEDROCK)时,它的优先级高于所有 AWS 凭证方法,包括已配置的 profile。

  3. 运行 /models 命令来选择你想使用的模型。

    /models

Note: 对于自定义推理 profile,请在 key 中使用模型和 provider 名称,并将 id 属性设置为该 arn。这能确保缓存正确。

opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "amazon-bedrock": {
      // ...
      "models": {
        "anthropic-claude-sonnet-4.5": {
          "id": "arn:aws:bedrock:us-east-1:xxx:application-inference-profile/yyy"
        }
      }
    }
  }
}

Anthropic

  1. 注册完成后,运行 /connect 命令并选择 Anthropic。

    /connect
  2. 在这里你可以选择 Claude Pro/Max 选项,它会打开你的浏览器并要求你进行身份验证。

    ┌ Select auth method
    
    │ Manually enter API Key
    
  3. 现在当你使用 /models 命令时,所有 Anthropic 模型都应该可用了。

    /models

有一些插件可以让你在 OpenCode 中使用你的 Claude Pro/Max 模型。Anthropic 明确禁止这种做法。

早期版本的 OpenCode 曾捆绑这些插件,但从 1.3.0 起不再如此。

其他公司在开发者工具上支持自由选择——你可以在 OpenCode 中零配置使用以下订阅:

  • ChatGPT Plus
  • Github Copilot
  • Gitlab Duo

Atomic Chat

你可以配置 opencode 通过 Atomic Chat (opens in a new tab) 使用本地模型,它是一个桌面应用,在一个 OpenAI 兼容的 API 服务器(默认端点 http://127.0.0.1:1337/v1)后面运行本地 LLM。

opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "atomic-chat": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Atomic Chat (local)",
      "options": {
        "baseURL": "http://127.0.0.1:1337/v1"
      },
      "models": {
        "<your-model-id>": {
          "name": "<your-model-name>"
        }
      }
    }
  }
}

在这个例子中:

  • atomic-chat 是自定义的 provider ID。可以是你想要的任意字符串。
  • npm 指定该 provider 使用的包。这里使用 @ai-sdk/openai-compatible,适用于任何 OpenAI 兼容的 API。
  • name 是 provider 在 UI 中的显示名称。
  • options.baseURL 是本地服务器的端点。请修改 host 和端口以匹配你的 Atomic Chat 设置。
  • models 是模型 ID 到其显示名称的映射。每个 ID 必须与 GET /v1/models 返回的 id 匹配——运行 curl http://127.0.0.1:1337/v1/models 可列出 Atomic Chat 中当前已加载的 id。

Tip: 如果工具调用效果不佳,请选择一个对工具调用支持较强的已加载模型(例如 Qwen-Coder 或 DeepSeek-Coder 变体)。


Azure OpenAI

Note: 如果你遇到 "I'm sorry, but I cannot assist with that request" 错误,请尝试在你的 Azure 资源中把内容过滤器从 DefaultV2 改为 Default

  1. 前往 Azure portal (opens in a new tab) 并创建一个 Azure OpenAI 资源。你需要:

    • Resource name :这会成为你的 API 端点的一部分( https://RESOURCE_NAME.openai.azure.com/
    • API key :来自你资源的 KEY 1KEY 2
  2. 前往 Azure AI Foundry (opens in a new tab) 并部署一个模型。

    Note: 部署名称必须与模型名称匹配,opencode 才能正常工作。

  3. 运行 /connect 命令并搜索 Azure

    /connect
  4. 输入你的 API key。

    ┌ API key
    
    
    └ enter
  5. 将你的资源名称设置为环境变量:

    AZURE_RESOURCE_NAME=XXX opencode

    或者将其添加到你的 bash profile 中:

    ~/.bash_profile
    export AZURE_RESOURCE_NAME=XXX
  6. 运行 /models 命令来选择你部署的模型。

    /models

Azure Cognitive Services

  1. 前往 Azure portal (opens in a new tab) 并创建一个 Azure OpenAI 资源。你需要:

    • Resource name :这会成为你的 API 端点的一部分( https://AZURE_COGNITIVE_SERVICES_RESOURCE_NAME.cognitiveservices.azure.com/
    • API key :来自你资源的 KEY 1KEY 2
  2. 前往 Azure AI Foundry (opens in a new tab) 并部署一个模型。

    Note: 部署名称必须与模型名称匹配,opencode 才能正常工作。

  3. 运行 /connect 命令并搜索 Azure Cognitive Services

    /connect
  4. 输入你的 API key。

    ┌ API key
    
    
    └ enter
  5. 将你的资源名称设置为环境变量:

    AZURE_COGNITIVE_SERVICES_RESOURCE_NAME=XXX opencode

    或者将其添加到你的 bash profile 中:

    ~/.bash_profile
    export AZURE_COGNITIVE_SERVICES_RESOURCE_NAME=XXX
  6. 运行 /models 命令来选择你部署的模型。

    /models

Baseten

  1. 前往 Baseten (opens in a new tab),创建账号,并生成一个 API key。

  2. 运行 /connect 命令并搜索 Baseten

    /connect
  3. 输入你的 Baseten API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个模型。

    /models

Cerebras

  1. 前往 Cerebras 控制台 (opens in a new tab),创建账号,并生成一个 API key。

  2. 运行 /connect 命令并搜索 Cerebras

    /connect
  3. 输入你的 Cerebras API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个模型,例如 Qwen 3 Coder 480B

    /models

Cloudflare AI Gateway

Cloudflare AI Gateway 让你能通过统一端点访问来自 OpenAI、Anthropic、Workers AI 等的模型。借助 Unified Billing (opens in a new tab),你无需为每个 provider 分别准备 API key。

  1. 前往 Cloudflare 仪表盘 (opens in a new tab),导航到 AI > AI Gateway,并创建一个新的 gateway。记下你的 Account IDGateway ID

  2. 运行 /connect 命令并搜索 Cloudflare AI Gateway

    /connect
  3. 在提示时输入你的 Account ID

    ┌ Enter your Cloudflare Account ID
    
    
    └ enter
  4. 在提示时输入你的 Gateway ID

    ┌ Enter your Cloudflare AI Gateway ID
    
    
    └ enter
  5. 输入你的 Cloudflare API token

    ┌ Gateway API token
    
    
    └ enter
  6. 运行 /models 命令来选择一个模型。

    /models

    你也可以通过 opencode 配置添加模型。

    opencode.json
    {
      "$schema": "https://opencode.ai/config.json",
      "provider": {
        "cloudflare-ai-gateway": {
          "models": {
            "openai/gpt-4o": {},
            "anthropic/claude-sonnet-4": {}
          }
        }
      }
    }

    或者,你可以设置环境变量来代替使用 /connect

    ~/.bash_profile
    export CLOUDFLARE_ACCOUNT_ID=your-32-character-account-id
    export CLOUDFLARE_GATEWAY_ID=your-gateway-id
    export CLOUDFLARE_API_TOKEN=your-api-token

Cloudflare Workers AI

Cloudflare Workers AI 让你能直接通过 REST API 在 Cloudflare 的全球网络上运行 AI 模型,对于受支持的模型无需单独的 provider 账号。

  1. 前往 Cloudflare 仪表盘 (opens in a new tab),导航到 Workers AI,并选择 Use REST API 来获取你的 Account ID 并创建一个 API token。

  2. 运行 /connect 命令并搜索 Cloudflare Workers AI

    /connect
  3. 在提示时输入你的 Account ID

    ┌ Enter your Cloudflare Account ID
    
    
    └ enter
  4. 输入你的 Cloudflare API key

    ┌ API key
    
    
    └ enter
  5. 运行 /models 命令来选择一个模型。

    /models

    或者,你可以设置环境变量来代替使用 /connect

    ~/.bash_profile
    export CLOUDFLARE_ACCOUNT_ID=your-32-character-account-id
    export CLOUDFLARE_API_KEY=your-api-token

Cortecs

  1. 前往 Cortecs 控制台 (opens in a new tab),创建账号,并生成一个 API key。

  2. 运行 /connect 命令并搜索 Cortecs

    /connect
  3. 输入你的 Cortecs API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个模型,例如 Kimi K2 Instruct

    /models

DeepSeek

  1. 前往 DeepSeek 控制台 (opens in a new tab),创建账号,并点击 Create new API key

  2. 运行 /connect 命令并搜索 DeepSeek

    /connect
  3. 输入你的 DeepSeek API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个 DeepSeek 模型,例如 DeepSeek V4 Pro

    /models

Deep Infra

  1. 前往 Deep Infra 仪表盘 (opens in a new tab),创建账号,并生成一个 API key。

  2. 运行 /connect 命令并搜索 Deep Infra

    /connect
  3. 输入你的 Deep Infra API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个模型。

    /models

DigitalOcean

DigitalOcean 的 Inference Engine (opens in a new tab) 提供对 GPT-OSS、Llama、Qwen 和 DeepSeek 等开源模型的访问,外加自定义的 Inference Routers (opens in a new tab),可将每个请求路由到针对某项任务最便宜、最快或最合适的模型。

OpenCode 支持两种身份验证方法:

  • OAuth(推荐) —— 登录你的 DigitalOcean 账号;OpenCode 会自动创建一个 Model Access Key 并发现你可用的 Models 和 Inference Routers。
  • Model Access Key —— 从 DigitalOcean 控制台粘贴一个现有的 key。

OAuth(推荐)

  1. 运行 /connect 命令并搜索 DigitalOcean

    /connect
  2. 选择 Login with DigitalOcean

    ┌ Select auth method
    
    │ Login with DigitalOcean
    │ Paste Model Access Key
    
  3. 你的浏览器会打开以授权 OpenCode。登录并批准。

    Note: OpenCode 会在你的 DigitalOcean 账号中创建一个名为 opencode-oauth-<timestamp> 的 Model Access Key。你可以在 DigitalOcean 控制台 Inference 下 "Manage" 部分的 Model Access Keys 页面轮换或撤销它。

  4. 运行 /models 命令。你的 Inference Routers 会以 router: 的格式出现在模型选择中。

    /models
  5. 要获取新创建的 Inference Routers,请重新运行 /connect 并再次选择 DigitalOcean

使用 Model Access Key

如果你更愿意直接粘贴一个 key:

  1. 前往 DigitalOcean 控制台 (opens in a new tab) Inference 部分的 Manage 页面,并创建一个新的 key。

  2. 运行 /connect 命令并选择 DigitalOcean,然后选择 Paste Model Access Key

    ┌ Enter your DigitalOcean Model Access Key
    
    
    └ enter

    Note: 使用此方法不会自动发现 Inference Routers。要在模型选择器中显示它们,请改用 OAuth 登录。

  3. 运行 /models 命令来选择一个模型。

    /models

环境变量

或者,将你的 Model Access Key 设置为环境变量。

export DIGITALOCEAN_ACCESS_TOKEN=your-model-access-key

Inference Routers

Inference Routers 让你能在多个模型之间定义路由策略——根据任务为每个请求挑选最便宜、最快或最合适的模型。OAuth 后,OpenCode 会在模型选择器中将每个 router 显示为 router:<router-name>

选择一个 router 模型可以无缝替换任何其他模型——OpenCode 转发你的请求,DigitalOcean 根据你的 router 策略挑选底层模型。了解更多关于 Inference Routers (opens in a new tab) 的信息。


FrogBot

  1. 前往 FrogBot 仪表盘 (opens in a new tab),创建账号,并生成一个 API key。

  2. 运行 /connect 命令并搜索 FrogBot

    /connect
  3. 输入你的 FrogBot API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个模型。

    /models

Fireworks AI

  1. 前往 Fireworks AI 控制台 (opens in a new tab),创建账号,并点击 Create API Key

  2. 运行 /connect 命令并搜索 Fireworks AI

    /connect
  3. 输入你的 Fireworks AI API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个模型,例如 Kimi K2 Instruct

    /models

GitLab Duo

Caution: OpenCode 中的 GitLab Duo 支持仍处于实验阶段。功能、配置和 行为在未来版本中可能会变化。

OpenCode 与 GitLab Duo Agent Platform (opens in a new tab) 集成, 提供由 AI 驱动、具备原生工具调用能力的 agentic 聊天。

Note: GitLab Duo Agent Platform 需要 PremiumUltimate GitLab 订阅。它在 GitLab.com 和 GitLab Self-Managed 上可用。 完整要求请参阅 GitLab Duo Agent Platform prerequisites (opens in a new tab)

  1. 运行 /connect 命令并选择 GitLab。

    /connect
  2. 选择你的身份验证方法:

    ┌ Select auth method
    
    │ OAuth (Recommended)
    │ Personal Access Token
    

    使用 OAuth(推荐)

    选择 OAuth,你的浏览器将打开以进行授权。

    使用 Personal Access Token

    1. 前往 GitLab User Settings > Access Tokens (opens in a new tab)
    2. 点击 Add new token
    3. Name:OpenCode ,Scopes:api
    4. 复制该 token(以 glpat- 开头)
    5. 在终端中输入它
  3. 运行 /models 命令查看可用模型。

    /models

    有三个基于 Claude 的模型可用:

    • duo-chat-haiku-4-5(默认)- 快速响应,适合快速任务
    • duo-chat-sonnet-4-5 - 性能均衡,适合大多数工作流
    • duo-chat-opus-4-5 - 能力最强,适合复杂分析

Note: 如果你不想在 opencode 的 auth 存储中保存 token,你也可以指定 'GITLAB_TOKEN' 环境变量。

Self-Hosted GitLab

Note: OpenCode 会为某些 AI 任务(例如生成会话标题)使用一个小模型。 它默认配置为使用由 Zen 托管的 gpt-5-nano。要将 OpenCode 锁定为只使用你自己的 GitLab 托管实例,请将以下内容添加到你的 opencode.json 文件中。同时也建议禁用会话共享。

{
  "$schema": "https://opencode.ai/config.json",
  "small_model": "gitlab/duo-chat-haiku-4-5",
  "share": "disabled"
}

对于自托管的 GitLab 实例:

export GITLAB_INSTANCE_URL=https://gitlab.company.com
export GITLAB_TOKEN=glpat-...

如果你的实例运行一个自定义的 AI Gateway:

GITLAB_AI_GATEWAY_URL=https://ai-gateway.company.com

或者添加到你的 bash profile:

~/.bash_profile
export GITLAB_INSTANCE_URL=https://gitlab.company.com
export GITLAB_AI_GATEWAY_URL=https://ai-gateway.company.com
export GITLAB_TOKEN=glpat-...

Note: 你的 GitLab 管理员必须:

  1. 为该用户、群组或实例 Turn on GitLab Duo (opens in a new tab)
  2. Turn on the Agent Platform (opens in a new tab)(GitLab 18.8+)或 enable beta and experimental features (opens in a new tab)(GitLab 18.7 及更早版本)
  3. 对于 Self-Managed,configure your instance (opens in a new tab)
用于自托管实例的 OAuth

为了让 OAuth 在你的自托管实例上工作,你需要创建 一个新的应用(Settings → Applications),其回调 URL 为 http://127.0.0.1:8080/callback,并具有以下 scope:

  • api(代表你访问 API)
  • read_user(读取你的个人信息)
  • read_repository(允许对仓库进行只读访问)

然后将应用 ID 暴露为环境变量:

export GITLAB_OAUTH_CLIENT_ID=your_application_id_here

更多文档参见 opencode-gitlab-auth (opens in a new tab) 主页。

Configuration

通过 opencode.json 自定义:

opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "gitlab": {
      "options": {
        "instanceUrl": "https://gitlab.com"
      }
    }
  }
}
GitLab Duo Agent Platform (DAP) Workflow Models

DAP workflow 模型提供一条替代的执行路径,它将工具调用 通过 GitLab 的 Duo Workflow Service (DWS) 路由,而不是走标准的 agentic 聊天。 当选择 duo-workflow-* 模型时,OpenCode 将会:

  1. 从你的 GitLab namespace 发现可用模型
  2. 如果有多个可用模型,显示一个选择器
  3. 将选定的模型缓存到磁盘,以便后续启动更快
  4. 通过 OpenCode 的受权限门控的工具系统路由工具执行请求

可用的 DAP workflow 模型遵循 duo-workflow-* 命名约定, 并从你的 GitLab 实例动态发现。

GitLab API Tools(可选,但强烈推荐)

要访问 GitLab 工具(merge request、issue、pipeline、CI/CD 等):

opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "plugin": ["opencode-gitlab-plugin"]
}

此插件提供全面的 GitLab 仓库管理能力,包括 MR 评审、issue 跟踪、pipeline 监控等。


GitHub Copilot

在 opencode 中使用你的 GitHub Copilot 订阅:

Note: 某些模型可能需要 Pro+ 订阅 (opens in a new tab) 才能使用。

  1. 运行 /connect 命令并搜索 GitHub Copilot。

    /connect
  2. 导航到 github.com/login/device (opens in a new tab) 并输入该代码。

    ┌ Login with GitHub Copilot
    
    │ https://github.com/login/device
    
    │ Enter code: 8F43-6FCF
    
    └ Waiting for authorization...
  3. 现在运行 /models 命令来选择你想使用的模型。

    /models

Google Vertex AI

在 OpenCode 中使用 Google Vertex AI:

  1. 前往 Google Cloud Console 中的 Model Garden,查看你所在 region 可用的模型。

    Note: 你需要有一个启用了 Vertex AI API 的 Google Cloud 项目。

  2. 设置所需的环境变量:

    • GOOGLE_CLOUD_PROJECT :你的 Google Cloud 项目 ID

    • VERTEX_LOCATION (可选):Vertex AI 的 region(默认为 global

    • 身份验证(任选其一):

      • GOOGLE_APPLICATION_CREDENTIALS :你的 service account JSON key 文件的路径
      • 使用 gcloud CLI 进行身份验证:gcloud auth application-default login

    在运行 opencode 时设置它们。

    GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json GOOGLE_CLOUD_PROJECT=your-project-id opencode

    或者将它们添加到你的 bash profile。

    ~/.bash_profile
    export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json
    export GOOGLE_CLOUD_PROJECT=your-project-id
    export VERTEX_LOCATION=global

Tip: global region 可以提升可用性并减少错误,且不产生额外费用。如果有数据驻留要求,请使用区域端点(例如 us-central1)。了解更多 (opens in a new tab)

  1. 运行 /models 命令来选择你想使用的模型。

    /models

Groq

  1. 前往 Groq 控制台 (opens in a new tab),点击 Create API Key,并复制该 key。

  2. 运行 /connect 命令并搜索 Groq。

    /connect
  3. 输入该 provider 的 API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择你想要的那一个。

    /models

Hugging Face

Hugging Face Inference Providers (opens in a new tab) 提供对由 17+ 个 provider 支持的开源模型的访问。

  1. 前往 Hugging Face settings (opens in a new tab) 创建一个有权限调用 Inference Providers 的 token。

  2. 运行 /connect 命令并搜索 Hugging Face

    /connect
  3. 输入你的 Hugging Face token。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个模型,例如 Kimi-K2-InstructGLM-4.6

    /models

Helicone

Helicone (opens in a new tab) 是一个 LLM 可观测性平台,为你的 AI 应用提供日志、监控和分析。Helicone AI Gateway 会根据模型自动将你的请求路由到合适的 provider。

  1. 前往 Helicone (opens in a new tab),创建账号,并从你的仪表盘生成一个 API key。

  2. 运行 /connect 命令并搜索 Helicone

    /connect
  3. 输入你的 Helicone API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个模型。

    /models

要了解更多 provider 以及缓存、限流等进阶功能,请查看 Helicone 文档 (opens in a new tab)

可选配置

如果你看到 Helicone 提供的某项功能或某个模型没有通过 opencode 自动配置,你随时可以自己配置它。

这里是 Helicone 的模型目录 (opens in a new tab),你需要用它来获取你想添加的模型的 ID。

~/.config/opencode/opencode.jsonc
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "helicone": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Helicone",
      "options": {
        "baseURL": "https://ai-gateway.helicone.ai",
      },
      "models": {
        "gpt-4o": {
          // Model ID (from Helicone's model directory page)
          "name": "GPT-4o", // Your own custom name for the model
        },
        "claude-sonnet-4-20250514": {
          "name": "Claude Sonnet 4",
        },
      },
    },
  },
}

自定义 Headers

Helicone 支持自定义 header,用于缓存、用户跟踪和会话管理等功能。使用 options.headers 将它们添加到你的 provider 配置中:

~/.config/opencode/opencode.jsonc
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "helicone": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Helicone",
      "options": {
        "baseURL": "https://ai-gateway.helicone.ai",
        "headers": {
          "Helicone-Cache-Enabled": "true",
          "Helicone-User-Id": "opencode",
        },
      },
    },
  },
}
会话跟踪

Helicone 的 Sessions (opens in a new tab) 功能让你能将相关的 LLM 请求分组到一起。使用 opencode-helicone-session (opens in a new tab) 插件,可以自动将每次 OpenCode 对话作为一个会话记录到 Helicone 中。

npm install -g opencode-helicone-session

将它添加到你的配置中。

opencode.json
{
  "plugin": ["opencode-helicone-session"]
}

该插件会向你的请求中注入 Helicone-Session-IdHelicone-Session-Name header。在 Helicone 的 Sessions 页面中,你会看到每次 OpenCode 对话被列为一个单独的会话。

常见的 Helicone header
Header说明
Helicone-Cache-Enabled启用响应缓存(true/false
Helicone-User-Id按用户跟踪指标
Helicone-Property-[Name]添加自定义属性(例如 Helicone-Property-Environment
Helicone-Prompt-Id将请求与 prompt 版本关联

所有可用的 header 请参阅 Helicone Header Directory (opens in a new tab)


llama.cpp

你可以配置 opencode 通过 llama.cpp (opens in a new tab) 的 llama-server 工具使用本地模型。

opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "llama.cpp": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "llama-server (local)",
      "options": {
        "baseURL": "http://127.0.0.1:8080/v1"
      },
      "models": {
        "qwen3-coder:a3b": {
          "name": "Qwen3-Coder: a3b-30b (local)",
          "limit": {
            "context": 128000,
            "output": 65536
          }
        }
      }
    }
  }
}

在这个例子中:

  • llama.cpp 是自定义的 provider ID。可以是你想要的任意字符串。
  • npm 指定该 provider 使用的包。这里使用 @ai-sdk/openai-compatible,适用于任何 OpenAI 兼容的 API。
  • name 是 provider 在 UI 中的显示名称。
  • options.baseURL 是本地服务器的端点。
  • models 是模型 ID 到其配置的映射。模型名称会显示在模型选择列表中。

IO.NET

IO.NET 提供针对各种用例优化的 17 个模型:

  1. 前往 IO.NET 控制台 (opens in a new tab),创建账号,并生成一个 API key。

  2. 运行 /connect 命令并搜索 IO.NET

    /connect
  3. 输入你的 IO.NET API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个模型。

    /models

LM Studio

你可以配置 opencode 通过 LM Studio 使用本地模型。

opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "lmstudio": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "LM Studio (local)",
      "options": {
        "baseURL": "http://127.0.0.1:1234/v1"
      },
      "models": {
        "google/gemma-3n-e4b": {
          "name": "Gemma 3n-e4b (local)"
        }
      }
    }
  }
}

在这个例子中:

  • lmstudio 是自定义的 provider ID。可以是你想要的任意字符串。
  • npm 指定该 provider 使用的包。这里使用 @ai-sdk/openai-compatible,适用于任何 OpenAI 兼容的 API。
  • name 是 provider 在 UI 中的显示名称。
  • options.baseURL 是本地服务器的端点。
  • models 是模型 ID 到其配置的映射。模型名称会显示在模型选择列表中。

Moonshot AI

使用来自 Moonshot AI 的 Kimi K2:

  1. 前往 Moonshot AI 控制台 (opens in a new tab),创建账号,并点击 Create API key

  2. 运行 /connect 命令并搜索 Moonshot AI

    /connect
  3. 输入你的 Moonshot API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择 Kimi K2

    /models

MiniMax

  1. 前往 MiniMax API 控制台 (opens in a new tab),创建账号,并生成一个 API key。

  2. 运行 /connect 命令并搜索 MiniMax

    /connect
  3. 输入你的 MiniMax API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个模型,例如 M2.1

    /models

NVIDIA

NVIDIA 通过 build.nvidia.com (opens in a new tab) 免费提供对 Nemotron 模型以及许多其他开源模型的访问。

  1. 前往 build.nvidia.com (opens in a new tab),创建账号,并生成一个 API key。

  2. 运行 /connect 命令并搜索 NVIDIA

    /connect
  3. 输入你的 NVIDIA API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个模型,例如 nemotron-3-super-120b-a12b。

    /models

On-Prem / NIM

你也可以通过设置自定义 base URL,借助 NVIDIA NIM (opens in a new tab) 在本地使用 NVIDIA 模型。

opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "nvidia": {
      "options": {
        "baseURL": "http://localhost:8000/v1"
      }
    }
  }
}

环境变量

或者,将你的 API key 设置为环境变量。

export NVIDIA_API_KEY=nvapi-your-key-here

Nebius Token Factory

  1. 前往 Nebius Token Factory 控制台 (opens in a new tab),创建账号,并点击 Add Key

  2. 运行 /connect 命令并搜索 Nebius Token Factory

    /connect
  3. 输入你的 Nebius Token Factory API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个模型,例如 Kimi K2 Instruct

    /models

Ollama

你可以配置 opencode 通过 Ollama 使用本地模型。

Tip: Ollama 可以自动为 OpenCode 完成配置。详情请参阅 Ollama 集成文档 (opens in a new tab)

opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "ollama": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Ollama (local)",
      "options": {
        "baseURL": "http://localhost:11434/v1"
      },
      "models": {
        "llama2": {
          "name": "Llama 2"
        }
      }
    }
  }
}

在这个例子中:

  • ollama 是自定义的 provider ID。可以是你想要的任意字符串。
  • npm 指定该 provider 使用的包。这里使用 @ai-sdk/openai-compatible,适用于任何 OpenAI 兼容的 API。
  • name 是 provider 在 UI 中的显示名称。
  • options.baseURL 是本地服务器的端点。
  • models 是模型 ID 到其配置的映射。模型名称会显示在模型选择列表中。

Tip: 如果工具调用不工作,请尝试在 Ollama 中增大 num_ctx。从 16k - 32k 左右开始尝试。


Ollama Cloud

在 OpenCode 中使用 Ollama Cloud:

  1. 前往 https://ollama.com/ (opens in a new tab) 并登录或创建账号。

  2. 导航到 Settings > Keys 并点击 Add API Key 以生成一个新的 API key。

  3. 复制该 API key 以便在 OpenCode 中使用。

  4. 运行 /connect 命令并搜索 Ollama Cloud

    /connect
  5. 输入你的 Ollama Cloud API key。

    ┌ API key
    
    
    └ enter
  6. 重要:在 OpenCode 中使用云端模型之前,你必须先在本地拉取模型信息:

    ollama pull gpt-oss:20b-cloud
  7. 运行 /models 命令来选择你的 Ollama Cloud 模型。

    /models

OpenAI

我们推荐注册 ChatGPT Plus 或 Pro (opens in a new tab)

  1. 注册完成后,运行 /connect 命令并选择 OpenAI。

    /connect
  2. 在这里你可以选择 ChatGPT Plus/Pro 选项,它会打开你的浏览器并要求你进行身份验证。

    ┌ Select auth method
    
    │ ChatGPT Plus/Pro
    │ Manually enter API Key
    
  3. 现在当你使用 /models 命令时,所有 OpenAI 模型都应该可用了。

    /models
使用 API key

如果你已经有一个 API key,你可以选择 Manually enter API Key 并在终端中粘贴它。


OpenCode Zen

OpenCode Zen 是由 OpenCode 团队提供的一份经过测试和验证的模型清单。了解更多

  1. 登录 OpenCode Zen (opens in a new tab) 并点击 Create API Key

  2. 运行 /connect 命令并搜索 OpenCode Zen

    /connect
  3. 输入你的 OpenCode API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个模型,例如 Qwen 3 Coder 480B

    /models

OpenRouter

  1. 前往 OpenRouter 仪表盘 (opens in a new tab),点击 Create API Key,并复制该 key。

  2. 运行 /connect 命令并搜索 OpenRouter。

    /connect
  3. 输入该 provider 的 API key。

    ┌ API key
    
    
    └ enter
  4. 许多 OpenRouter 模型默认已预加载,运行 /models 命令来选择你想要的那一个。

    /models

    你也可以通过 opencode 配置添加额外的模型。

    opencode.json
    {
      "$schema": "https://opencode.ai/config.json",
      "provider": {
        "openrouter": {
          "models": {
            "somecoolnewmodel": {}
          }
        }
      }
    }
  5. 你也可以通过 opencode 配置自定义它们。下面是一个指定 provider 的例子。

    opencode.json
    {
      "$schema": "https://opencode.ai/config.json",
      "provider": {
        "openrouter": {
          "models": {
            "moonshotai/kimi-k2": {
              "options": {
                "provider": {
                  "order": ["baseten"],
                  "allow_fallbacks": false
                }
              }
            }
          }
        }
      }
    }

LLM Gateway

  1. 前往 LLM Gateway 仪表盘 (opens in a new tab),点击 Create API Key,并复制该 key。

  2. 运行 /connect 命令并搜索 LLM Gateway。

    /connect
  3. 输入该 provider 的 API key。

    ┌ API key
    
    
    └ enter
  4. 许多 LLM Gateway 模型默认已预加载,运行 /models 命令来选择你想要的那一个。

    /models

    你也可以通过 opencode 配置添加额外的模型。

    opencode.json
    {
      "$schema": "https://opencode.ai/config.json",
      "provider": {
        "llmgateway": {
          "models": {
            "somecoolnewmodel": {}
          }
        }
      }
    }
  5. 你也可以通过 opencode 配置自定义它们。下面是一个指定 provider 的例子。

    opencode.json
    {
      "$schema": "https://opencode.ai/config.json",
      "provider": {
        "llmgateway": {
          "models": {
            "glm-4.7": {
              "name": "GLM 4.7"
            },
            "gpt-5.2": {
              "name": "GPT-5.2"
            },
            "gemini-2.5-pro": {
              "name": "Gemini 2.5 Pro"
            },
            "claude-3-5-sonnet-20241022": {
              "name": "Claude 3.5 Sonnet"
            }
          }
        }
      }
    }

SAP AI Core

SAP AI Core 通过一个统一平台提供对来自 OpenAI、Anthropic、Google、Amazon、Meta、Mistral 和 AI21 的 40+ 个模型的访问。

  1. 前往你的 SAP BTP Cockpit (opens in a new tab),导航到你的 SAP AI Core 服务实例,并创建一个 service key。

    Tip: 该 service key 是一个 JSON 对象,包含 clientidclientsecreturlserviceurls.AI_API_URL。你可以在 BTP Cockpit 中 Services > Instances and Subscriptions 下找到你的 AI Core 实例。

  2. 运行 /connect 命令并搜索 SAP AI Core

    /connect
  3. 输入你的 service key JSON。

    ┌ Service key
    
    
    └ enter

    或者设置 AICORE_SERVICE_KEY 环境变量:

    AICORE_SERVICE_KEY='{"clientid":"...","clientsecret":"...","url":"...","serviceurls":{"AI_API_URL":"..."}}' opencode

    或者将其添加到你的 bash profile:

    ~/.bash_profile
    export AICORE_SERVICE_KEY='{"clientid":"...","clientsecret":"...","url":"...","serviceurls":{"AI_API_URL":"..."}}'
  4. 可选地设置 deployment ID 和 resource group:

    AICORE_DEPLOYMENT_ID=your-deployment-id AICORE_RESOURCE_GROUP=your-resource-group opencode

    Note: 这些设置是可选的,应根据你的 SAP AI Core 设置进行配置。

  5. 运行 /models 命令从 40+ 个可用模型中进行选择。

    /models

STACKIT

STACKIT AI Model Serving 提供一个完全托管的主权托管环境来运行 AI 模型,聚焦于 Llama、Mistral 和 Qwen 等 LLM,并在欧洲基础设施上实现最大程度的数据主权。

  1. 前往 STACKIT Portal (opens in a new tab),导航到 AI Model Serving,并为你的项目创建一个 auth token。

    Tip: 在创建 auth token 之前,你需要先有一个 STACKIT 客户账号、用户账号和项目。

  2. 运行 /connect 命令并搜索 STACKIT

    /connect
  3. 输入你的 STACKIT AI Model Serving auth token。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令从可用模型中进行选择,例如 Qwen3-VL 235BLlama 3.3 70B

    /models

OVHcloud AI Endpoints

  1. 前往 OVHcloud 面板 (opens in a new tab)。导航到 Public Cloud 部分、AI & Machine Learning > AI Endpoints,并在 API Keys 标签页中点击 Create a new API key

  2. 运行 /connect 命令并搜索 OVHcloud AI Endpoints

    /connect
  3. 输入你的 OVHcloud AI Endpoints API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个模型,例如 gpt-oss-120b

    /models

Scaleway

在 Opencode 中使用 Scaleway Generative APIs (opens in a new tab)

  1. 前往 Scaleway Console IAM settings (opens in a new tab) 生成一个新的 API key。

  2. 运行 /connect 命令并搜索 Scaleway

    /connect
  3. 输入你的 Scaleway API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个模型,例如 devstral-2-123b-instruct-2512gpt-oss-120b

    /models

Together AI

  1. 前往 Together AI 控制台 (opens in a new tab),创建账号,并点击 Add Key

  2. 运行 /connect 命令并搜索 Together AI

    /connect
  3. 输入你的 Together AI API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个模型,例如 Kimi K2 Instruct

    /models

Venice AI

  1. 前往 Venice AI 控制台 (opens in a new tab),创建账号,并生成一个 API key。

  2. 运行 /connect 命令并搜索 Venice AI

    /connect
  3. 输入你的 Venice AI API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个模型,例如 Llama 3.3 70B

    /models

Vercel AI Gateway

Vercel AI Gateway 让你能通过统一端点访问来自 OpenAI、Anthropic、Google、xAI 等的模型。模型按标价提供,不加价。

  1. 前往 Vercel 仪表盘 (opens in a new tab),导航到 AI Gateway 标签页,并点击 API keys 来创建一个新的 API key。

  2. 运行 /connect 命令并搜索 Vercel AI Gateway

    /connect
  3. 输入你的 Vercel AI Gateway API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个模型。

    /models

你也可以通过 opencode 配置自定义模型。下面是一个指定 provider 路由顺序的例子。

opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "vercel": {
      "models": {
        "anthropic/claude-sonnet-4": {
          "options": {
            "order": ["anthropic", "vertex"]
          }
        }
      }
    }
  }
}

一些有用的路由选项:

选项说明
order要尝试的 provider 顺序
only限制为特定的 provider
zeroDataRetention仅使用具有零数据保留策略的 provider

xAI

有三种身份验证方式:通过浏览器 OAuth 使用 SuperGrok 订阅、通过无头设备代码流(device-code flow)使用同一个 SuperGrok 订阅(适用于 VPS / SSH / Docker),或使用来自 xAI 控制台的按量付费 API key。

方式 A —— SuperGrok OAuth(浏览器登录)

  1. 运行 /connect 命令并搜索 xAI

    /connect
  2. 选择 xAI Grok OAuth (SuperGrok Subscription)。OpenCode 会在你的浏览器中打开 xAI 的授权页面,并在 http://127.0.0.1:56121/callback 上等待回调。

  3. 运行 /models 命令来选择一个 Grok 模型。

    /models

OpenCode 会自动刷新 OAuth access token。任何包含 Grok API 访问权限的 Grok 或 X Premium 套餐都可用;你不需要单独的 XAI_API_KEY

方式 B —— SuperGrok device-code(无头 / 远程服务器 / VPS)

当 OpenCode 运行在浏览器无法访问 loopback 重定向的环境时使用此方式:VPS、通过 SSH 访问的远程开发机、Docker 内部、CI 等。运行 OpenCode 的主机上不会打开回调端口——相反,xAI 会向 CLI 提供一个短代码,你在任何其他设备(笔记本电脑、手机……)的浏览器中输入它。

  1. 在远程主机上运行 /connect 命令并搜索 xAI

    /connect
  2. 选择 xAI Grok OAuth (Headless / Remote / VPS)。OpenCode 会打印一个验证 URL 和一个短的用户代码。

    Open https://x.ai/device on any device and enter code: ABCD-1234
  3. 在一台有浏览器的设备(你的笔记本电脑或手机)上打开该 URL,输入代码,并批准授权页面。OpenCode 会轮询 xAI 的 token 端点,并在你批准后存储生成的 OAuth token。Token 刷新方式与方式 A 相同。

方式 C —— API key

  1. 前往 xAI 控制台 (opens in a new tab),创建账号,并生成一个 API key。

  2. 运行 /connect 命令并搜索 xAI

    /connect
  3. 选择 Manually enter API Key 并粘贴你的 xAI API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个模型,例如 Grok Beta

    /models

Z.AI

  1. 前往 Z.AI API 控制台 (opens in a new tab),创建账号,并点击 Create a new API key

  2. 运行 /connect 命令并搜索 Z.AI

    /connect

    如果你订阅了 GLM Coding Plan,请选择 Z.AI Coding Plan

  3. 输入你的 Z.AI API key。

    ┌ API key
    
    
    └ enter
  4. 运行 /models 命令来选择一个模型,例如 GLM-4.7

    /models

ZenMux

  1. 前往 ZenMux 仪表盘 (opens in a new tab),点击 Create API Key,并复制该 key。

  2. 运行 /connect 命令并搜索 ZenMux。

    /connect
  3. 输入该 provider 的 API key。

    ┌ API key
    
    
    └ enter
  4. 许多 ZenMux 模型默认已预加载,运行 /models 命令来选择你想要的那一个。

    /models

    你也可以通过 opencode 配置添加额外的模型。

    opencode.json
    {
      "$schema": "https://opencode.ai/config.json",
      "provider": {
        "zenmux": {
          "models": {
            "somecoolnewmodel": {}
          }
        }
      }
    }

Custom provider

要添加任何未在 /connect 命令中列出的 OpenAI 兼容 provider:

Tip: 你可以将任何 OpenAI 兼容的 provider 与 opencode 配合使用。大多数现代 AI provider 都提供 OpenAI 兼容的 API。

  1. 运行 /connect 命令并向下滚动到 Other

    $ /connect
     
      Add credential
    
      Select provider
      ...
       Other
    
  2. 为该 provider 输入一个唯一的 ID。

    $ /connect
     
      Add credential
    
      Enter provider id
      myprovider
    

    Note: 选择一个好记的 ID,你将在配置文件中用到它。

  3. 为该 provider 输入你的 API key。

    $ /connect
     
      Add credential
    
      This only stores a credential for myprovider - you will need to configure it in opencode.json, check the docs for examples.
    
      Enter your API key
      sk-...
    
  4. 在你的项目目录中创建或更新你的 opencode.json 文件:

    opencode.json
    {
      "$schema": "https://opencode.ai/config.json",
      "provider": {
        "myprovider": {
          "npm": "@ai-sdk/openai-compatible",
          "name": "My AI ProviderDisplay Name",
          "options": {
            "baseURL": "https://api.myprovider.com/v1"
          },
          "models": {
            "my-model-name": {
              "name": "My Model Display Name"
            }
          }
        }
      }
    }

    以下是这些配置选项:

    • npm :要使用的 AI SDK 包,OpenAI 兼容 provider(用于 /v1/chat/completions )使用 @ai-sdk/openai-compatible。如果你的 provider/模型使用 /v1/responses ,请使用 @ai-sdk/openai
    • name :UI 中的显示名称。
    • models :可用的模型。
    • options.baseURL :API 端点 URL。
    • options.apiKey :可选地设置 API key,如果不使用 auth 的话。
    • options.headers :可选地设置自定义 header。

    下面的例子中有更多进阶选项。

  5. 运行 /models 命令,你的自定义 provider 和模型就会出现在选择列表中。


Example

下面是一个设置 apiKeyheaders 和模型 limit 选项的例子。

opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "myprovider": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "My AI ProviderDisplay Name",
      "options": {
        "baseURL": "https://api.myprovider.com/v1",
        "apiKey": "{env:ANTHROPIC_API_KEY}",
        "headers": {
          "Authorization": "Bearer custom-token"
        }
      },
      "models": {
        "my-model-name": {
          "name": "My Model Display Name",
          "limit": {
            "context": 200000,
            "output": 65536
          }
        }
      }
    }
  }
}

配置详情:

  • apiKey :使用 env 变量语法设置,了解更多
  • headers :随每个请求发送的自定义 header。
  • limit.context :模型接受的最大输入 token 数。
  • limit.output :模型可以生成的最大 token 数。

limit 字段让 OpenCode 能够理解你还剩多少上下文。标准 provider 会自动从 models.dev 拉取这些信息。


Troubleshooting

如果你在配置某个 provider 时遇到问题,请检查以下内容:

  1. 检查 auth 设置:运行 opencode auth list 查看该 provider 的凭证 是否已添加到你的配置中。这不适用于像 Amazon Bedrock 这样依赖环境变量进行 auth 的 provider。

  2. 对于自定义 provider,请检查 opencode 配置并:

    • 确保在 /connect 命令中使用的 provider ID 与你 opencode 配置中的 ID 匹配。
    • 为该 provider 使用了正确的 npm 包。例如,Cerebras 使用 @ai-sdk/cerebras。对于所有其他 OpenAI 兼容的 provider,使用 @ai-sdk/openai-compatible(用于 /v1/chat/completions );如果某个模型使用 /v1/responses ,则使用 @ai-sdk/openai。对于同一个 provider 下的混合配置,你可以通过 provider.npm 按模型覆盖。
    • 检查 options.baseURL 字段中使用的 API 端点是否正确。