> ## 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 pi to Token Factory

> Add Corvex Token Factory as a custom OpenAI-compatible provider in the pi coding agent and size each model's limits.

Corvex Token Factory connects to the [pi coding agent](https://github.com/earendil-works/pi)
as a custom provider on the OpenAI-compatible Chat Completions API. You declare
the provider and its models in pi's `models.json`, and set per-model compaction
in its `settings.json`.

Verified with pi 1.2.0, October 2026.

## Prerequisites

* A Token Factory API key. See [Authentication](https://docs.corvex.cloud/getting-started/authentication.md).
* pi 1.2.0 or later (`npm install -g @earendil-works/pi-coding-agent`; requires
  Node.js 22.19 or later).
* A model from the [Token Factory catalog](https://docs.corvex.cloud/models/overview.md).

## Context window and compaction

pi does not read limits from the endpoint, so each model declares them:

* `contextWindow` is the model's `context_window`.
* `maxTokens` is the output reservation pi sends with every request (as
  `max_completion_tokens`), reduced only when the context is nearly full. The
  example keeps pi's default of 16,384. Setting it to the model's full output
  cap would make every request reserve almost the entire window.
* pi compacts the conversation once it exceeds `contextWindow − reserveTokens`,
  where `reserveTokens` is a compaction setting.

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 `reserveTokens` is
the context window minus that value:

| Model | Context window | Output cap | `max_tokens` sent | Compaction window | `reserveTokens` |
| - | - | - | - | - | - |
| `zai-org/GLM-5.3` | 393,216 | 393,216 | 16,384 | 327,680 | 65,536 |
| `deepseek-ai/DeepSeek-V4-Flash-0731` | 1,048,576 | 1,048,576 | 16,384 | 897,558 | 151,018 |

`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. `models.json` reads it with
`$CORVEX_API_KEY`; a bare variable name without `$` is sent as the key
itself.

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

**2. Add the provider and models**

Add the provider to `~/.pi/agent/models.json`:

```json models.json theme={null}
{
  "providers": {
    "corvex": {
      "name": "Corvex Token Factory",
      "baseUrl": "https://api.tokenfactory.corvex.cloud/v1",
      "api": "openai-completions",
      "apiKey": "$CORVEX_API_KEY",
      "models": [
        {
          "id": "zai-org/GLM-5.3",
          "name": "GLM 5.3",
          "reasoning": true,
          "input": ["text"],
          "contextWindow": 393216,
          "maxTokens": 16384,
          "cost": { "input": 1.4, "output": 4.4, "cacheRead": 0, "cacheWrite": 0 }
        },
        {
          "id": "deepseek-ai/DeepSeek-V4-Flash-0731",
          "name": "DeepSeek V4 Flash 0731",
          "reasoning": true,
          "input": ["text"],
          "contextWindow": 1048576,
          "maxTokens": 16384,
          "cost": { "input": 0.14, "output": 0.28, "cacheRead": 0, "cacheWrite": 0 }
        }
      ]
    }
  }
}
```

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

**3. Set the default model and compaction**

Add these keys to `~/.pi/agent/settings.json`:

```json settings.json theme={null}
{
  "defaultProvider": "corvex",
  "defaultModel": "zai-org/GLM-5.3",
  "compaction": {
    "modelOverrides": {
      "corvex/zai-org/GLM-5.3": { "reserveTokens": 65536 },
      "corvex/deepseek-ai/DeepSeek-V4-Flash-0731": { "reserveTokens": 151018 }
    }
  }
}
```

`defaultModel` takes the bare model ID; the provider goes in
`defaultProvider`.

**4. Run pi and verify the connection**

```bash theme={null}
cd /path/to/your/repo
pi                                                       # zai-org/GLM-5.3
pi --model corvex/deepseek-ai/DeepSeek-V4-Flash-0731     # another model
```

`pi --list-models corvex` prints the context window and output reservation
pi loaded for each model. To run one prompt non-interactively:

```bash theme={null}
pi -p "Summarize this repository."
```

## Compatibility notes

* **Reasoning.** Both models reason before answering. Reasoning tokens count
  toward `maxTokens`; raise it if responses are cut off, and recompute
  `reserveTokens` with the formula.
* **Context length.** If a session still exceeds the model's window, the gateway
  returns `context_length_exceeded`, and pi compacts and retries. See
  [Errors](https://docs.corvex.cloud/reference/errors.md#context_length_exceeded-400).
* **Input types.** Neither model accepts image input; keep `input` set to
  `["text"]`.

## Related documentation

* [Connect OpenCode](https://docs.corvex.cloud/integrations/opencode.md) — another terminal coding agent on
  the OpenAI-compatible surface.
* [Use the OpenAI SDK](https://docs.corvex.cloud/integrations/openai-drop-in.md) — the same endpoint from
  Python or TypeScript.
* [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.