The local guardrail that stops Claude Code from blowing past its context window.
A transparent safety proxy that sits between Claude Code / Claude Desktop and CC Switch โ protecting tool protocol, managing token budgets, retrying context-limit errors once, and routing multimodal inputs.
๐ Quick Start ยท โจ Features ยท โ๏ธ How It Works ยท ๐ง Configuration ยท ๐ค Contributing ยท ๐จ๐ณ ไธญๆ่ฏดๆ
โ ๏ธ This is an independent community project and is not affiliated with ccswitch, Anthropic, or Claude Code.
CCProxy Agent ships with a local web console for checking proxy status, editing guardrail settings, and watching live routing logs.
Ever seen this error mid-coding session?
This model's maximum context length is 200000 tokens.
However, you requested 64000 output tokens and your prompt contains at least 136001 input tokens.
That's a hard stop โ your session dies, work is lost, and you restart. CCProxy Agent fixes this automatically by:
- ๐ Lowering
max_tokensbefore the request even leaves your machine - ๐ Parsing the upstream
400and retrying once with a safe budget - ๐งฎ The math is dead simple:
200000 โ 8000 safety โ 136001 input = 55999 max_tokens
It's not a replacement for ccswitch โ it's the crumple zone above it.
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ
Claude CLI โโโถ โ CCProxy Agent โ โโโถ โ ccswitch โ โโโถ โ model provider โ
Claude Desktop โ :15722 (guard) โ โ :15721 โ โ (GLM, Qwen...) โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ
token budgeting ยท local switch upstream LLM
retry ยท vision ยท
compact reminder
The proxy intercepts /v1/messages and /v1/chat/completions, applies guardrails, then forwards to ccswitch. On startup it temporarily re-points Claude CLI / Claude Desktop configs at itself and restores them cleanly on shutdown.
| Feature | What it does | |
|---|---|---|
| ๐ง | Auto token budgeting | Reduces max_tokens before forwarding so input + output never exceeds the context limit |
| ๐ | One-shot retry | Parses upstream context-limit 400 errors and retries with a safer output budget |
| ๐งฐ | Agent protocol rescue | Clears old tool results and preserves bounded head/tail evidence from the newest result without breaking tool IDs |
| ๐ชง | Compact reminder | Appends a /compact hint to responses when the session nears the threshold |
| ๐ผ๏ธ | Vision summarization | Routes Claude Desktop images through vision models, injects [VISION SUMMARY] for text-only downstreams like GLM-5.2 |
| ๐งฉ | Fallback chunking | Splits requests that remain too large after budgeting |
| ๐ | Claude CLI patching | Temporarily redirects Claude CLI settings โ proxy, restores on exit |
| ๐ฅ๏ธ | Claude Desktop 3P patching | Rewrites Desktop gateway config under %LOCALAPPDATA%/Claude-3p/configLibrary |
| ๐ | Port fallback | Auto-jumps to the next free port (15722 โ 15723 โ โฆ) if busy |
| ๐ | Upstream auto-discovery | Finds a reachable local ccswitch when the configured upstream is down |
| ๐ | Built-in dashboard | Optional status, routing, process info & live logs at the active proxy URL |
| ๐๏ธ | Per-session context center | Keeps concurrent Claude sessions separate with stable selection, live 200k usage, thresholds, and protection history |
| ๐ชต | Local Chinese logs | Human-readable Token้ข็ฎ่ฏไผฐ / ๅทฒ่ชๅจ้ไฝmax_tokens style messages |
| ๐ฆ | Windows + macOS packaging | Ship a portable Windows EXE plus macOS DMG/ZIP builds |
- Download
CCProxy-Agent-v0.4.104.exefrom the latest GitHub Release. - Run the EXE. On first startup it creates a secret-free
config.jsonbeside the executable. - Configure CC Switch and optional Vision settings in the desktop UI.
The release also includes config.example.json and SHA256SUMS.txt. Private config.json files are never included in release artifacts.
- Download the
arm64build for Apple Silicon orx64for Intel Macs from the latest release. - Open the DMG and move CCProxy Agent to Applications, or extract the ZIP.
- This first macOS build is unsigned. If Gatekeeper blocks it, Control-click the app and choose Open. Configuration is stored under the app's macOS user-data directory, never inside the read-only app bundle.
git clone https://github.com/geekchongv/ccswitch-context-guard.git
cd ccswitch-context-guard
npm install
npm run devThe first source startup also creates config.json when it is missing. The proxy starts on http://127.0.0.1:15722 and the local dashboard remains available at that URL.
Health check:
curl http://127.0.0.1:15722/health๐ก If you open the optional dashboard manually, its Stop button closes cleanly and restores patched Claude settings before exit.
safe budget = hardLimit โ safetyMargin
max_tokens = safe budget โ estimated_input_tokens
e.g. 200000 โ 8000 โ 136001 = 55999 โ
never overflows
upstream โโ400โโโถ parse "200000 ... 136001 input ... 64000 output"
โโโถ recompute max_tokens
โโโถ retry once โโโถ 200 OK
image blocks โโโถ vision model (qwen3-vl) โโโถ [VISION SUMMARY]
โโโถ strip original images โโโถ forward to GLM-5.2
Copy config.example.json โ config.json. Key sections:
๐ซ Token policy
{
"tokenPolicy": {
"compactThreshold": 160000,
"hardLimit": 200000,
"safetyMargin": 8000,
"compactMode": "proxy",
"autoReduceMaxTokens": true,
"retryOnContextError": true,
"minOutputTokens": 1024
}
}Claude Code native compact and tool observation
{
"claudeConfigPatch": {
"enabled": true,
"autoCompactEnabled": true,
"autoCompactReserveTokens": 30000,
"hookObserverEnabled": true
}
}With the bundled 200k context window, reserving 30k configures new Claude Code CLI processes to compact at about 85%. The GUI keeps concurrent Claude sessions separate and shows each session's token percentage together with the 140k tool-result cleanup, 160k proxy compact, 170k Claude native compact, and 192k effective hard-protection markers. The legacy CCProxy-managed 130k/76% pair is upgraded automatically; other user-defined values remain untouched. Session and prompt identifiers are stored only as short irreversible hashes; prompt and tool output content is not persisted.
Agent requests containing tools, tool_use, or tool_result are never sent through generic proxy chunking or text-flattening compaction. When an Agent request reaches the configured threshold, CCProxy Agent keeps every tool_use, tool_use_id, message role, and message position while replacing only older tool_result.content values with explicit placeholders. The three most recent tool results are preserved by default. After an upstream context-limit error, old results are cleared first; if that is insufficient, the newest oversized result keeps bounded head and tail evidence instead of being erased.
๐ผ๏ธ Vision
{
"vision": {
"enabled": true,
"models": ["qwen3-vl-30b-a3b-instruct", "Qwen3.6-35B-A3B"],
"compareModels": true,
"apiKeyEnv": "CCPROXY_VISION_API_KEY",
"stripImagesAfterSummary": true
}
}Desktop users can enter the API key directly on the Multimodal page. It is encrypted with Windows DPAPI or macOS Keychain-backed Electron safe storage and is never returned to the renderer. The environment variable remains an optional CLI fallback:
[Environment]::SetEnvironmentVariable("CCPROXY_VISION_API_KEY", "your-token", "User")๐ฅ๏ธ Claude Desktop 3P patching
When claudeDesktopConfigPatch.enabled is true, the proxy rewrites the ccswitch Desktop gateway path:
http://127.0.0.1:15721/claude-desktop โโโถ http://127.0.0.1:15722/claude-desktop
โ ๏ธ Fully quit and reopen Claude Desktop after starting the proxy โ it reads 3P config only at launch.
Written to logs/ccproxy-agent.log. Useful Chinese log lines:
| Message | Meaning |
|---|---|
Token้ข็ฎ่ฏไผฐ |
Estimated input / output / total tokens |
ๅทฒ่ชๅจ้ไฝmax_tokensโฆ |
Output budget reduced before sending upstream |
ไธๆธธ่ฟๅไธไธๆ่ถ
้้่ฏฏโฆ |
Upstream returned a context-limit error, parsed |
ๅทฒ้ไฝmax_tokensๅนถ่ชๅจ้่ฏไธๆฌก |
Retried once with a safer budget |
ๅทฒ่งฆๅcompactๆ้ๆจกๅผ |
Response will include a /compact reminder |
ๅทฒ่งฆๅๅๅๆง่ก |
Chunking fallback started |
npm run check # typecheck
npm test # run test suite
npm run build # compile TS โ dist
npm run check:gui # validate desktop UI references
npm run package:gui # build sanitized portable Windows EXE
npm run package:gui:mac # build macOS DMG + ZIP (run on macOS)
npm run smoke:packaged # run the real packaged context-rescue smoke test- ๐ Design
- โ Validation
- ๐งญ Next release assessment
- ๐ Architecture and Operations Wiki
- ๐ฆ Latest Release
- ๐ Full Changelog
CCProxy Agent is a local proxy โ it can see prompts/responses passing through it, but by default everything stays on your machine.
- โ Logs and session snapshots are local-only
- โ
Release builds reject private
config.jsonfiles and configured secret bytes - โ Desktop Vision API keys are encrypted with the operating-system credential service
- โ
CLI users can still use
CCPROXY_VISION_API_KEYwithout writing a key to config - ๐ซ Never commit your
config.json,runtime/,logs/, or personal builds
See SECURITY.md for details.
- Token counting before upstream is heuristic, not provider-native
- Claude Code CLI native auto-compact settings apply to newly started CLI processes
- Tool-loop hooks are observe-only and require a Claude Code version with HTTP hook support
- Claude Desktop does not expose the same CLI hook lifecycle and receives base proxy protection only
- Agent rescue preserves protocol structure but cannot guarantee lossless recovery after the provider hard limit is exceeded
- Multimodal summarization requires explicit endpoint, model, and Vision API key configuration
- Compact reminders on SSE responses are currently buffered before rewriting
- Chunking is a fallback, not a full long-task planning engine
- Configuration restart is not yet transactional; invalid settings can leave the proxy stopped until corrected
- macOS builds are currently unsigned; Apple notarization requires release signing credentials
- Linux desktop binaries are not currently published
CCProxy Agent ๆฏไธไธชๆพๅจ Claude CLI / Claude Desktop ๅ ccswitch ไธญ้ด็ๆฌๅฐๆคๆ ไปฃ็ใ
ๅฎไธๆฏ ccswitch ็ๆฟไปฃๅ๏ผ่ๆฏ ccswitch ไธๅฑ็้ฒๆๆคๆ ๏ผไธป่ฆ่งฃๅณ่ฟไบ้ฎ้ข๏ผ
- ๐ ๅฟซๅฐไธไธๆไธ้ๆถๆ้ไฝ ๆง่ก
/compact - ๐ง
input_tokens + max_tokens่ถ ่ฟๆจกๅไธไธๆๆถ๏ผ่ชๅจ้ไฝmax_tokens - ๐ ไธๆธธ่ฟๅ context limit
400ๆถ๏ผ่งฃๆ้่ฏฏๅนถ่ชๅจ้่ฏไธๆฌก - ๐ฅ๏ธ ไธดๆถๆฅ็ฎก Claude Desktop 3P ็ฝๅ ณ้ ็ฝฎ
- ๐ผ๏ธ Claude Desktop ๅๅพๅ่ฐ็จ่ง่งๆจกๅ็ๆๆ่ฆ๏ผๅ่ฝฌ็ปไธๆฏๆๅพ็็ GLM-5.2
้ป่ฎคๆๆ๏ผ
Claude CLI / Claude Desktop โโโถ CCProxy Agent :15722 โโโถ ccswitch :15721 โโโถ ๆจกๅ
(่ชๅจ้ไฝ / ้่ฏ / ๆ่ฆ / ๆ้)
ๅฟซ้ๅผๅง๏ผ
npm install
cp config.example.json config.json
npm run devๆ ธๅฟๆฅๅฟ๏ผ
Token้ข็ฎ่ฏไผฐ๏ผไผฐ็ฎ็่พๅ ฅ / ่พๅบ / ๆป tokenๅทฒ่ชๅจ้ไฝmax_tokens๏ผ้ฟๅ ๆปtokenๆไธไธไธๆ็กฌไธ้ไธๆธธ่ฟๅไธไธๆ่ถ ้้่ฏฏ๏ผๅทฒ่งฃๆ้่ฏฏ่ฏฆๆๅทฒ้ไฝmax_tokensๅนถ่ชๅจ้่ฏไธๆฌกๅทฒ่งฆๅcompactๆ้ๆจกๅผ
Made with ๐ก๏ธ for the Claude Code + ccswitch community.
If this saved your session, a โญ on GitHub means a lot.
Report a bug ยท Request a feature ยท Contributing ยท View releases

