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. - A model ID from the Models catalog.
- Claude Code 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_TOKENSsets the window Claude Code assumes for the model. Set it to the model’scontext_window.CLAUDE_CODE_AUTO_COMPACT_WINDOWsets the window Claude Code compacts against./contextreports usage out of this value. Claude Code compacts before usage reaches it, keeping a buffer ofmin(max_tokens, 20,000) + 13,000tokens (33,000 by default), shown in/contextas the autocompact buffer.
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:
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.
2
Add the settings
Add an The user settings file is
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.settings.json
~/.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
/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 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=0is 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. - Context length. If a session still reaches the model’s limit, the gateway
returns
context_length_exceeded. Run/compactand retry. See Errors. - 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 page.
Related documentation
- Use the Anthropic SDK — the same Messages API from Python or TypeScript.
- Connect Codex and Connect OpenCode — other terminal coding agents.
- Errors — authentication, rate-limit, and validation responses.