exceeds model's maximum context length(Claude Code)
Quick fix
Set CLAUDE_CODE_AUTO_COMPACT_WINDOW in the Claude Code provider’s advanced settings in cc-switch to a value below the model’s context limit.
Symptom
Section titled “Symptom”API Error: 500 Input length (121079) exceeds model's maximum context length (120000). This is a server-side issue, usually temporary — try again in a moment. If it persists, check your inference gateway (127.0.0.1:15721).Claude Code has no official setting for configuring the actual context window of a BYOK backend. cc-switch can only write to Claude Code’s configuration file and environment variables; it cannot make Claude Code automatically truncate input to the model’s actual limit. When a model accessed through a local or third-party gateway has a smaller context window (such as 120k) than Claude Code assumes by default, input accumulated during a long session can exceed the model’s limit, and the server returns 500. A workaround is to set CLAUDE_CODE_AUTO_COMPACT_WINDOW so Claude Code compacts automatically before reaching that threshold.
The same variable is involved in two other issues. First, if its value is pinned too low (such as 272000), compact may trigger around 256K even when routing to a 1M context model. Check whether the variable is set in `~/.claude/settings.json`. Second, cc-switch v3.17.0 and earlier overwrote provider-specific values with CLAUDE_CODE_AUTO_COMPACT_WINDOW extracted from the general configuration, making different thresholds for different providers ineffective. This was fixed in v4.0.0, where the variable became a provider-specific field.
Edit the relevant Claude Code provider in cc-switch and add an advanced item under env named CLAUDE_CODE_AUTO_COMPACT_WINDOW. Set it to a number below the model’s actual context window (for example, set it to 100000 for a 120k model). Claude Code will compact automatically as it approaches the threshold; a user reported that this worked.
cc-switch → Claude Code provider → 高级项 {"env": {"CLAUDE_CODE_AUTO_COMPACT_WINDOW": "100000"}}If the opposite happens—the model has a 1M context window but compacts automatically around 256K—check `~/.claude/settings.json` and see whether CLAUDE_CODE_AUTO_COMPACT_WINDOW is pinned to a value that is too low (such as 272000). Change it to match the model’s window or remove it.
~/.claude/settings.json {"env": {"CLAUDE_CODE_AUTO_COMPACT_WINDOW": "272000"}}As a temporary workaround, run /compact manually in the Claude Code session to reduce the context.
If your different provider-specific compact thresholds are being overwritten by automatic extraction from the general configuration (a known bug in v3.17.0 and earlier), upgrade to cc-switch v4.0.0 or later. That version removed automatic extraction from the general configuration and handles CLAUDE_CODE_AUTO_COMPACT_WINDOW as a provider-specific field.
Affected Versions
Section titled “Affected Versions”Source Issues
Section titled “Source Issues”This page is distilled from 3 real issues
- I’m using a 1M context route. Why does compact run automatically around 256K?
- The 1M window may be working; the environment variable CLAUDE_CODE_AUTO_COMPACT_WINDOW in `~/.claude/settings.json` may be pinning the automatic compact threshold (for example, to 272000). Check the file and change the value to match the model’s window or remove the variable.
- I set different CLAUDE_CODE_AUTO_COMPACT_WINDOW thresholds for different providers. Why do they stop working when I enable the general configuration?
- This is a bug in cc-switch v3.17.0 and earlier: the general configuration automatically extracts the property into a shared section and overrides provider-specific values. v4.0.0 removed this automatic extraction; the variable is now a provider-specific field. Upgrade to the latest version.
- Can cc-switch make Claude Code automatically truncate input according to the actual context window of a BYOK model?
- No. Claude Code currently has no official setting for configuring the actual context window of a BYOK backend. cc-switch can only write to Claude Code’s configuration file and environment variables. You can set CLAUDE_CODE_AUTO_COMPACT_WINDOW as the automatic compact threshold or run /compact manually.
- What value should I use for CLAUDE_CODE_AUTO_COMPACT_WINDOW?
- Set it below the model’s actual context window to leave room for compact. In the issues, setting it to 100000 worked for a model with 120k context; a user set it to 386000 for a model with a 386k window. Adjust the value to your model’s limit.
Related problems
Section titled “Related problems”This is an unofficial community wiki with no affiliation to the cc-switch authors or the project itself. Its content is compiled from the project's public GitHub issues. This site distributes no software.