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

> Point Claude Code at Corvex Token Factory through its Anthropic-compatible Messages API and size the context window to the model you serve.

Corvex Token Factory exposes an Anthropic-compatible Messages API, so Claude Code
connects to it as an [LLM gateway](https://code.claude.com/docs/en/llm-gateway).
You set the endpoint, key, and model as environment variables, either in your
shell or in the `env` block of a Claude Code settings file, and tell Claude Code
the model's real context window.

Verified with Claude Code 2.1.299, October 2026.

## Prerequisites

* A Token Factory API key (`sk-corvex-...`). See
  [Authentication](https://docs.corvex.cloud/getting-started/authentication.md).
* A model ID from the [Models](https://docs.corvex.cloud/models/overview.md) catalog.
* [Claude Code](https://code.claude.com/docs/en/setup) 2.1.296 or later.

## Context window and compaction

Claude Code does not read the context window of a model it doesn't recognize from
the endpoint. Unless you tell it otherwise, it assumes 200,000 tokens and
compacts the conversation well before a Token Factory model's real limit. Two
variables fix that:

* `CLAUDE_CODE_MAX_CONTEXT_TOKENS` sets the window Claude Code assumes for the
  model. Set it to the model's `context_window`.
* `CLAUDE_CODE_AUTO_COMPACT_WINDOW` sets the window Claude Code compacts
  against. `/context` reports usage out of this value. Claude Code compacts
  before usage reaches it, keeping a buffer of `min(max_tokens, 20,000) + 13,000`
  tokens (33,000 by default), shown in `/context` as the autocompact buffer.

Every request reserves room for the reply: the engine enforces
`input + max_tokens ≤ context_window`, and Claude Code sends `max_tokens` =
32,000 by default (`CLAUDE_CODE_MAX_OUTPUT_TOKENS`). A single turn with
large tool results can push the context past the compaction point before
compaction runs, so leave about 15% headroom: size the compaction window as
`(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 | 32,000 | 314,100 |
| `deepseek-ai/DeepSeek-V4-Flash-0731` | 1,048,576 | 1,048,576 | 32,000 | 883,979 |

`CLAUDE_CODE_AUTO_COMPACT_WINDOW` accepts plain integers from `100000` to
`1000000`. If you change `CLAUDE_CODE_MAX_OUTPUT_TOKENS`, recompute the
compaction window with the formula. `GET /v1/models` reports each model's
current `context_window` and `max_output_tokens`.

**1. Verify the key**

Export the key and send a token-counting request. It checks the key and
request shape without running inference.

```bash theme={null}
export ANTHROPIC_BASE_URL="https://api.tokenfactory.corvex.cloud"
export ANTHROPIC_AUTH_TOKEN="sk-corvex-..."

curl -sS "${ANTHROPIC_BASE_URL}/v1/messages/count_tokens" \
  -H "Authorization: Bearer ${ANTHROPIC_AUTH_TOKEN}" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "zai-org/GLM-5.3",
    "messages": [{"role": "user", "content": "Hello"}]
  }'
```

**2. Add the settings**

Add an `env` block to your user settings file. This example selects
`zai-org/GLM-5.3`; for another model, change every model ID and take both
numbers from the table above.

```json settings.json theme={null}
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.tokenfactory.corvex.cloud",
    "ANTHROPIC_AUTH_TOKEN": "sk-corvex-...",
    "ANTHROPIC_MODEL": "zai-org/GLM-5.3",
    "ANTHROPIC_DEFAULT_OPUS_MODEL": "zai-org/GLM-5.3",
    "ANTHROPIC_DEFAULT_SONNET_MODEL": "zai-org/GLM-5.3",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL": "zai-org/GLM-5.3",
    "CLAUDE_CODE_SUBAGENT_MODEL": "zai-org/GLM-5.3",
    "CLAUDE_CODE_MAX_CONTEXT_TOKENS": "393216",
    "CLAUDE_CODE_AUTO_COMPACT_WINDOW": "314100",
    "CLAUDE_CODE_AUTO_MODE_SERVER": "0"
  }
}
```

The user settings file is `~/.claude/settings.json`. Keep the key out of a
project's `.claude/settings.json`, which is usually committed. The same
variables also work as shell exports. To keep your default Claude Code setup
and use Token Factory only on demand, put the block in a separate file and
start Claude Code with `claude --settings <file>`.

The `ANTHROPIC_DEFAULT_*_MODEL` and `CLAUDE_CODE_SUBAGENT_MODEL` variables
route Claude Code's background and subagent requests to the same model, so
no request asks for a model Token Factory doesn't serve.

**3. Run Claude Code and check the window**

```bash theme={null}
cd /path/to/your/repo
claude
```

Run `/status` to confirm the base URL and model, and `/context` to confirm
the window. For `zai-org/GLM-5.3`, `/context` reports usage out of
`314.1k` tokens.

## Compatibility notes

* **Prompt caching.** The Messages API accepts `cache_control`, but Token Factory
  does not treat it as a request to cache content. Engine-reported cache usage is
  passed through; missing cache-usage fields are reported as zero. Cached input
  tokens are billed at the cached input rate. See
  [Errors: Prompt caching](https://docs.corvex.cloud/reference/errors.md#prompt-caching-cache_control) for
  details.
* **Auto mode classifier.** In auto mode, Claude Code asks the Anthropic API to
  run its safety classifier server-side and shows a notice saying the session
  "isn't eligible" when the results don't come back. Token Factory serves
  non-Anthropic models and does not provide those server-side checks.
  `CLAUDE_CODE_AUTO_MODE_SERVER=0` is the opt-out Anthropic documents for this
  case: auto mode keeps working with the local classifier, its requests count
  toward token usage at the applicable model rates, and the notice does not
  appear. See
  [Auto mode classifier request charges](https://code.claude.com/docs/en/auto-mode-classifier-billing).
* **Context length.** If a session still reaches the model's limit, the gateway
  returns `context_length_exceeded`. Run `/compact` and retry. See
  [Errors](https://docs.corvex.cloud/reference/errors.md#context_length_exceeded-400).
* **Model capabilities.** Choose a model whose input types and features match
  the task. Unsupported inputs return a structured error; compare the available
  models on the [Models](https://docs.corvex.cloud/models/overview.md) page.

## Related documentation

* [Use the Anthropic SDK](https://docs.corvex.cloud/integrations/anthropic-drop-in.md) — the same Messages
  API from Python or TypeScript.
* [Connect Codex](https://docs.corvex.cloud/integrations/codex.md) and [Connect OpenCode](https://docs.corvex.cloud/integrations/opencode.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.