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

> Configure OpenAI Codex CLI and the Codex IDE extension to use a Corvex Token Factory model through the Responses API.

Corvex Token Factory exposes an OpenAI Responses API endpoint, which is the
protocol Codex requires from a custom model provider. Add Token Factory as a
provider in `~/.codex/config.toml`, describe its models in a model catalog file
so Codex knows their real context windows, and select a model. The Codex CLI and
the Codex IDE extension read the same configuration.

Verified with Codex 0.162.1, October 2026.

## Prerequisites

* A Token Factory API key. See [Authentication](https://docs.corvex.cloud/getting-started/authentication.md).
* [Codex CLI](https://developers.openai.com/codex/cli) 0.162.1 or later
  (`npm install -g @openai/codex`) and `jq`.
* A model ID from the [Token Factory catalog](https://docs.corvex.cloud/models/overview.md).

## Context window and compaction

Codex treats a model ID it doesn't know as a 272,000-token model, and
`model_context_window` cannot raise that ceiling. A model catalog file
(`model_catalog_json`) gives Codex each model's real window.

Codex does not send `max_output_tokens`, so it reserves no output room of its
own (`max_tokens` = 0 in the formula below). Its default compaction point is 90%
of the window. A single turn with large tool results can push the context past
the compaction point before compaction runs, so leave about 15% headroom: set
`model_auto_compact_token_limit` to `(context_window − max_tokens) / 1.15`,
rounded down:

| Model | Context window | Output cap | `max_tokens` sent | Compaction window |
| - | - | - | - | - |
| `zai-org/GLM-5.3` | 393,216 | 393,216 | 0 | 341,926 |
| `deepseek-ai/DeepSeek-V4-Flash-0731` | 1,048,576 | 1,048,576 | 0 | 911,805 |

Codex counts 95% of the window as usable (373,555 tokens for a 393,216-token
window); after the first turn, `/status` shows that usable window.
`GET /v1/models` reports each model's current `context_window` and
`max_output_tokens`.

**1. Store the key in your shell**

Codex reads the API key from the environment variable named by `env_key`.
Export it in your shell profile so both the CLI and the IDE extension can
read it.

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

**2. Create the model catalog**

Build the catalog from Codex's bundled one, so the Token Factory models keep
Codex's standard instructions. The command writes
`~/.codex/token-factory-models.json`:

```bash create-catalog.sh theme={null}
codex debug models --bundled | jq '
  (.models | min_by(.priority) | .model_messages) as $messages
  | {models: [
      {slug: "zai-org/GLM-5.3", context_window: 393216},
      {slug: "deepseek-ai/DeepSeek-V4-Flash-0731", context_window: 1048576}
    ] | map(. + {
      display_name: .slug, description: null, max_context_window: .context_window,
      effective_context_window_percent: 95, model_messages: $messages,
      supported_reasoning_levels: [], shell_type: "unified_exec", visibility: "list",
      supported_in_api: true, priority: 99, availability_nux: null, upgrade: null,
      support_verbosity: false, default_verbosity: null, apply_patch_tool_type: null,
      web_search_tool_type: "text", truncation_policy: {mode: "bytes", limit: 10000},
      experimental_supported_tools: []
    })}' > ~/.codex/token-factory-models.json
```

The catalog replaces Codex's built-in model list, so it lists only Token
Factory models.

**3. Add the provider to config.toml**

Create or edit `~/.codex/config.toml`:

```toml config.toml theme={null}
model = "zai-org/GLM-5.3"
model_provider = "corvex"
model_catalog_json = "~/.codex/token-factory-models.json"
model_auto_compact_token_limit = 341926

[model_providers.corvex]
name = "Corvex Token Factory"
base_url = "https://api.tokenfactory.corvex.cloud/v1"
env_key = "CORVEX_API_KEY"
wire_api = "responses"
```

Enter the model ID exactly as the catalog lists it, including the
`namespace/` prefix. Do not add a `corvex/` prefix.

**4. Add a profile for each other model**

The compaction limit is per model, so keep one profile file per model next
to `config.toml`. Codex layers `~/.codex/<name>.config.toml` on top of
`config.toml` when you pass `--profile <name>`:

```toml deepseek.config.toml theme={null}
model = "deepseek-ai/DeepSeek-V4-Flash-0731"
model_auto_compact_token_limit = 911805
```

**5. Run Codex**

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

To run a single task non-interactively:

```bash theme={null}
codex exec "Summarize what this repository does."
```

**6. Verify the connection**

Ask Codex to read a file in the repository, then run `/status`. It shows
**Corvex Token Factory**, the model ID, and a usable window of 95% of the
model's context window. A window of 258,400 means Codex did not load the
catalog; check the `model_catalog_json` path.

## How Token Factory serves the Responses API

`POST /v1/responses` accepts the Codex request shape, including `instructions`,
`input` items, `tools`, `tool_choice`, `max_output_tokens`, `temperature`, and
`stream`. The gateway translates each request to the model's Chat Completions
interface and returns Responses-shaped output, with streaming events in the
Responses event order. The model's reasoning is returned as a `reasoning` output
item ahead of the message item.

Conversation persistence is not exposed: `store` and `previous_response_id`
have no effect, and there is no `/v1/conversations` endpoint. Codex resends the
full conversation on each turn, so this does not affect normal use. Request
fields that control OpenAI-hosted reasoning (`reasoning`, `include`) are
accepted and ignored; the model reasons with its default settings.

See the [API reference](https://docs.corvex.cloud/api-reference/openapi.md) for the documented request and
response fields.

## Compatibility notes

* **Context length.** If a session exceeds the model's window, the gateway
  returns `context_length_exceeded`. Run `/compact` in Codex, or start a new
  session. See [Errors](https://docs.corvex.cloud/reference/errors.md#context_length_exceeded-400).
* **Output budget.** Reasoning tokens count toward the output budget. If a
  response is truncated, shorten the prompt or split the task.
* **Input types.** Neither model accepts image input. Do not attach images with
  `-i`; the request returns a structured error.

## Related documentation

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