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

# Connect OpenCode to Token Factory

> Configure Corvex Token Factory as an OpenAI-compatible provider in OpenCode and size each model's limits.

Corvex Token Factory connects to OpenCode through a custom OpenAI-compatible
provider. Add the provider and its models to your OpenCode configuration, then
select a Token Factory model from `/models`.

Verified with OpenCode 1.19.0, October 2026.

## Prerequisites

* A Token Factory API key. See [Authentication](https://docs.corvex.cloud/getting-started/authentication.md).
* [OpenCode](https://opencode.ai/docs/) 1.19.0 or later.
* A model from the [Token Factory catalog](https://docs.corvex.cloud/models/overview.md).

## Context window and compaction

OpenCode does not read limits from the endpoint, so each model declares them
under `limit`:

* `limit.context` is the model's `context_window`.
* `limit.output` is the model's `max_output_tokens`. OpenCode sends
  `max_tokens` = the smaller of this and its built-in 32,000-token cap.
* `limit.input` sets where OpenCode compacts: it summarizes the conversation
  once a request reaches `limit.input` minus 20,000 tokens (its default
  `compaction.reserved` while the output cap is at least 20,000). Without
  `limit.input`, OpenCode compacts only at `limit.context − max_tokens`, with no
  headroom.

A single turn with large tool results can push the context past the compaction
point before compaction runs, so leave about 15% headroom: the compaction window
is `(context_window − max_tokens) / 1.15`, rounded down, and `limit.input` is
that value plus 20,000:

| Model | Context window | Output cap | `max_tokens` sent | Compaction window | `limit.input` |
| - | - | - | - | - | - |
| `zai-org/GLM-5.3` | 393,216 | 393,216 | 32,000 | 314,100 | 334,100 |
| `deepseek-ai/DeepSeek-V4-Flash-0731` | 1,048,576 | 1,048,576 | 32,000 | 883,979 | 903,979 |

`GET /v1/models` reports each model's current `context_window` and
`max_output_tokens`.

**1. Store the API key**

Export the key in your shell profile. The configuration below reads it with
`{env:CORVEX_API_KEY}`.

```bash theme={null}
export CORVEX_API_KEY="sk-corvex-..."
```

You can instead run `/connect` in OpenCode, select **Other**, enter `corvex`
as the provider ID, and paste the key; then omit `apiKey` below.

**2. Add the provider configuration**

Add the provider to `~/.config/opencode/opencode.json` for global use, or to
`opencode.json` in the project root:

```json opencode.json theme={null}
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "corvex": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Corvex Token Factory",
      "options": {
        "baseURL": "https://api.tokenfactory.corvex.cloud/v1",
        "apiKey": "{env:CORVEX_API_KEY}"
      },
      "models": {
        "zai-org/GLM-5.3": {
          "name": "GLM 5.3",
          "reasoning": true,
          "tool_call": true,
          "cost": { "input": 1.4, "output": 4.4 },
          "limit": { "context": 393216, "input": 334100, "output": 393216 }
        },
        "deepseek-ai/DeepSeek-V4-Flash-0731": {
          "name": "DeepSeek V4 Flash 0731",
          "reasoning": true,
          "tool_call": true,
          "cost": { "input": 0.14, "output": 0.28 },
          "limit": { "context": 1048576, "input": 903979, "output": 1048576 }
        }
      }
    }
  },
  "model": "corvex/zai-org/GLM-5.3"
}
```

`cost` is in US dollars per million tokens and only affects OpenCode's cost
display; see [Pricing](https://docs.corvex.cloud/pricing.md) for current rates.

**3. Select and test a model**

Start OpenCode in your repository and run `/models`. Select a model under
**Corvex Token Factory**, then run a short task that exercises a tool call.
To run one prompt non-interactively:

```bash theme={null}
opencode run -m corvex/deepseek-ai/DeepSeek-V4-Flash-0731 "Summarize this repository."
```

`opencode models corvex --verbose` prints the limits OpenCode loaded for
each model.

## Compatibility notes

* **Output budget.** Reasoning tokens count toward the 32,000-token step
  budget. If a response or tool call is truncated, retry with a shorter prompt
  or split the task. The `OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX` environment
  variable changes the cap; recompute the compaction window if you change it.
* **Context length.** If a session still exceeds the model's window, the gateway
  returns `context_length_exceeded`. Run `/compact` or start a new session. See
  [Errors](https://docs.corvex.cloud/reference/errors.md#context_length_exceeded-400).
* **Model IDs.** Use the exact `id` from `GET /v1/models`, prefixed with the
  provider ID (`corvex/`) when you select it on the command line.

## Related documentation

* [Connect Kilo Code](https://docs.corvex.cloud/integrations/kilo-code.md) — the same configuration format in
  Kilo Code.
* [Connect pi](https://docs.corvex.cloud/integrations/pi.md) and [Connect Codex](https://docs.corvex.cloud/integrations/codex.md) — other
  terminal coding agents.
* [Errors](https://docs.corvex.cloud/reference/errors.md) — authentication, rate-limit, and validation
  responses.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.