> ## Documentation Index
> Fetch the complete documentation index at: https://docs.flexinference.com/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenCode

> 仅通过配置将 OpenCode 指向 FlexInference。

OpenCode 从 JSON 文件中读取提供商条目，无需更改源代码，也无需分叉。

OpenCode 可以在配置中设置 `start_within`，因此此处代理密钥是可选的。密钥仍然是更简单的路径，因为它无需重启即可更改。请参阅[代理密钥](/zh/agent-keys)。

## 使用代理进行设置

打开下面的代码块并将其复制到任何编码代理中。此提示绝不会要求您的 API 密钥：代理会配置所有其他内容，然后打印一行导出命令供您自行运行。

<Accordion title="复制代理设置提示">
  ```text theme={null}
  Configure OpenCode to send its requests to FlexInference. This is config only: no source change, no fork, no patch.

  You will never see or handle my API key. The provider entry stores only the `{env:FLEXINFERENCE_API_KEY}` template, and I export the value myself. Do not ask me for the key, and do not read it from my environment.

  Ask me whether this should apply to every project or one project, then write the provider entry to the matching file. Global is `~/.config/opencode/opencode.json`. Per project is `opencode.json` in the project root. Project config wins over global. Create the file if it does not exist, and merge into it rather than overwriting anything already there.

  The entry:

  {
    "$schema": "https://opencode.ai/config.json",
    "model": "flexinference/gpt-5.6-sol",
    "provider": {
      "flexinference": {
        "name": "FlexInference",
        "env": ["FLEXINFERENCE_API_KEY"],
        "npm": "@ai-sdk/openai-compatible",
        "options": {
          "apiKey": "{env:FLEXINFERENCE_API_KEY}",
          "baseURL": "https://api.flexinference.com/v1"
        },
        "models": {
          "gpt-5.6-sol": {
            "name": "GPT-5.6 Sol",
            "reasoning": true,
            "tool_call": true,
            "limit": { "context": 400000, "output": 128000 }
          }
        }
      }
    }
  }

  Then print this line for me to run in the shell I start OpenCode from. Do not run it and do not ask for the value:

      export FLEXINFERENCE_API_KEY=<paste your key here>

  Rules that matter, do not deviate:
  - Keep `options.apiKey` as the literal `{env:FLEXINFERENCE_API_KEY}` template. Never inline my key.
  - The top-level `model` field is what selects the provider. Without it OpenCode stays on its built-in default and never reaches FlexInference.
  - `options.baseURL` must end in `/v1`.
  - Every key under `models` is a slug sent verbatim on the wire, so it must be one FlexInference serves. `gpt-5.6-sol` is a safe default.
  - Do not put `seed`, `stop`, `presence_penalty`, `frequency_penalty`, `logit_bias`, `logprobs`, `top_logprobs`, `prediction`, `audio`, `modalities`, or `web_search_options` in any model's `options`. FlexInference refuses those Chat Completions parameters, and they also conflict with a key that carries a flex duration.
  - Do not add `start_within` unless I ask. My agent key already carries the deadline, and a key duration falls back safely on models that cannot race while a config duration errors.
  - If I mention working in repositories I do not control, tell me to set `OPENCODE_DISABLE_PROJECT_CONFIG`. A project config wins over the global one and can repoint `baseURL` while still reading my exported key. `OPENCODE_CONFIG` does not prevent that, because project config loads after it.

  Verify the file is valid JSON, then report what you changed and which file you wrote.
  ```
</Accordion>

## 配置 OpenCode

1. 在启动 OpenCode 的 shell 中导出密钥。

   ```bash theme={null}
   export FLEXINFERENCE_API_KEY=flex_live_...
   ```

2. 将提供商条目写入 `opencode.json`。

   ```json theme={null}
   {
     "$schema": "https://opencode.ai/config.json",
     "model": "flexinference/gpt-5.6-sol",
     "provider": {
       "flexinference": {
         "name": "FlexInference",
         "env": ["FLEXINFERENCE_API_KEY"],
         "npm": "@ai-sdk/openai-compatible",
         "options": {
           "apiKey": "{env:FLEXINFERENCE_API_KEY}",
           "baseURL": "https://api.flexinference.com/v1"
         },
         "models": {
           "gpt-5.6-sol": {
             "name": "GPT-5.6 Sol",
             "reasoning": true,
             "tool_call": true,
             "limit": { "context": 400000, "output": 128000 }
           }
         }
       }
     }
   }
   ```

3. 针对该文件启动 OpenCode。

   ```bash theme={null}
   OPENCODE_CONFIG=/path/to/opencode.json opencode
   ```

四个字段用于集成。`npm` 选择与 OpenAI 兼容的包。`options.baseURL` 将请求发送到路由器。`options.apiKey` 从环境中读取密钥。`models` 下的每个键都是我们运行的 slug。

通过在 `models` 下添加一个键来添加模型。条目中的其他所有内容都是显示和限制。

## 全局或每个项目

根据您希望其应用的范围，将提供商条目放置在以下两个位置之一。

| 范围   | 路径                                  | 适用于       |
| ---- | ----------------------------------- | --------- |
| 全局   | `~/.config/opencode/opencode.json`  | 此机器上的所有项目 |
| 每个项目 | `opencode.json` in the project root | 仅该项目      |
| 单次运行 | 任何路径，通过 `OPENCODE_CONFIG`           | 您传递它的启动   |

**项目配置优先于全局配置。** OpenCode 会在工作目录中查找 `opencode.json` 并向上遍历到最近的 Git 目录，因此仓库可以覆盖您的机器范围提供商而无需修改它。

将提供商条目放入全局文件，截止时间在任何地方都保持不变。将其放入项目文件，则只有该仓库通过我们进行路由。

设置 `OPENCODE_DISABLE_PROJECT_CONFIG` 以停止向上搜索。

还有两个用于捆绑包和自动化的来源。`OPENCODE_CONFIG_DIR` 指定 OpenCode 读取配置的目录，而 `OPENCODE_CONFIG_CONTENT` 则内联保存 JSON。对于您每次运行都选择的文件，请使用 `OPENCODE_CONFIG`。

```bash theme={null}
OPENCODE_CONFIG=/path/to/opencode.json opencode
```

## 改为在配置中设置截止时间

OpenCode 可以自行发送 `start_within`。这两个位置都是纯 JSON，并且都优先于密钥。

按模型设置。`options` 块会进入该模型的请求正文。

```json theme={null}
"gpt-5.6-sol": {
  "name": "GPT-5.6 Sol",
  "options": { "start_within": "00h-00m-30s", "reasoningEffort": "medium" }
}
```

改为按变体设置，无需更改模型即可切换。在 `opencode run` 上使用 `--variant` 选择一个，或在 TUI 中使用 `ctrl+t` 循环切换它们。

```json theme={null}
"gpt-5.6-sol": {
  "name": "GPT-5.6 Sol",
  "variants": {
    "patient": { "start_within": "00h-05m-00s", "reasoningEffort": "high" },
    "now": { "start_within": "priority", "reasoningEffort": "low" }
  }
}
```

此处设置的持续时间在无法竞争的模型上会失败。而密钥上设置的相同持续时间则会回退。这就是优先选择密钥的原因。

## 确认密钥已应用

每个响应都带有 `x-flexinference-defaults-applied`。OpenCode 不会在 TUI 中显示响应头，因此请改为在仪表板的“**日志**”下读取请求。

## 故障排除

**请求从未到达我们。** 顶层 `model` 字段是选择提供商的关键，因此如果定义了提供商但未在此处命名，OpenCode 将保持其内置默认设置。请设置 `"model": "flexinference/<slug>"`。

**[`401 invalid_api_key`](/zh/errors#invalid_api_key)。** 启动 OpenCode 的 shell 中未设置 `FLEXINFERENCE_API_KEY`。由于 `options.apiKey` 从环境中读取它，请在启动前导出它。

**[`400 unsupported_parameter`](/zh/errors#unsupported_parameter)。** 我们不支持的 Chat Completions 参数出现在模型的 `options` 中，例如 `seed` 或 `stop`。请将其从条目中删除。

**[`400 key_default_not_applicable`](/zh/errors#key_default_not_applicable)。** 您的密钥包含持续时间，并且其中一个参数与其同时出现。请将密钥编辑为某个层级，或删除该参数。

**[`400 flex_unsupported_for_anthropic`](/zh/errors#flex_unsupported_for_anthropic)。** `options` 中的持续时间到达了您自己的 Anthropic 密钥上的 `claude-*` 模型。Claude 持续时间运行的[竞争](/zh/deadline-routing)需要托管密钥。请删除持续时间，让密钥持有截止时间，因为密钥持续时间会回退。

**配置更改未生效。** 后续的配置源覆盖了它。请检查上面的搜索顺序，并优先使用 `OPENCODE_CONFIG` 以选择每次运行一个文件。

请参阅[错误](/zh/errors)以了解我们返回的所有拒绝信息，并参阅[代理密钥](/zh/agent-keys)以了解不特定于 OpenCode 的错误。[OpenWork](/zh/openwork) 和 [OpenScience](/zh/openscience) 读取相同的提供商条目。
