Skip to content

About

Local context-limit guard proxy for Claude CLI behind ccswitch

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

5 stars

Watchers

1 watching

Forks

Latest commit

ย 

History

45 Commits

Folders and files

Repository files navigation

๐Ÿ›ก๏ธ CCProxy Agent

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.

CI version release platform language license stars downloads

๐Ÿš€ Quick Start ยท โœจ Features ยท โš™๏ธ How It Works ยท ๐Ÿ”ง Configuration ยท ๐Ÿค Contributing ยท ๐Ÿ‡จ๐Ÿ‡ณ ไธญๆ–‡่ฏดๆ˜Ž

CCProxy Agent prevents context-window crashes on private model stacks


โš ๏ธ This is an independent community project and is not affiliated with ccswitch, Anthropic, or Claude Code.

๐Ÿ“ธ Dashboard Preview

CCProxy Agent ships with a local web console for checking proxy status, editing guardrail settings, and watching live routing logs.

CCProxy Agent dashboard showing runtime status, guardrail settings, and live routing logs

๐Ÿ’ก Why This Exists

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_tokens before the request even leaves your machine
  • ๐Ÿ” Parsing the upstream 400 and 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.

๐Ÿ—๏ธ Topology

                 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  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.

โœจ Features

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

๐Ÿš€ Quick Start

Windows portable app

  1. Download CCProxy-Agent-v0.4.104.exe from the latest GitHub Release.
  2. Run the EXE. On first startup it creates a secret-free config.json beside the executable.
  3. 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.

macOS app

  1. Download the arm64 build for Apple Silicon or x64 for Intel Macs from the latest release.
  2. Open the DMG and move CCProxy Agent to Applications, or extract the ZIP.
  3. 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.

From source

git clone https://github.com/geekchongv/ccswitch-context-guard.git
cd ccswitch-context-guard
npm install
npm run dev

The 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.

โš™๏ธ How It Works

Token budgeting (before the request leaves)

   safe budget = hardLimit โˆ’ safetyMargin
   max_tokens  = safe budget โˆ’ estimated_input_tokens

   e.g.  200000 โˆ’ 8000 โˆ’ 136001  =  55999   โœ… never overflows

Context-limit retry (after a 400)

   upstream โ”€โ”€400โ”€โ”€โ–ถ parse "200000 ... 136001 input ... 64000 output"
                   โ”€โ”€โ–ถ recompute max_tokens
                   โ”€โ”€โ–ถ retry once โ”€โ”€โ–ถ 200 OK

Vision routing (Claude Desktop โ†’ text-only model)

   image blocks โ”€โ”€โ–ถ vision model (qwen3-vl) โ”€โ”€โ–ถ [VISION SUMMARY]
                 โ”€โ”€โ–ถ strip original images โ”€โ”€โ–ถ forward to GLM-5.2

๐Ÿ”ง Configuration

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.

๐Ÿ“œ Logs

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

๐Ÿ› ๏ธ Development

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

๐Ÿ“š Documentation

๐Ÿ”’ Security & Privacy

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.json files and configured secret bytes
  • โœ… Desktop Vision API keys are encrypted with the operating-system credential service
  • โœ… CLI users can still use CCPROXY_VISION_API_KEY without writing a key to config
  • ๐Ÿšซ Never commit your config.json, runtime/, logs/, or personal builds

See SECURITY.md for details.

โš ๏ธ Current Limits

  • 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

About

Local context-limit guard proxy for Claude CLI behind ccswitch

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages