Skip to content

Zero-model mid-session backend switching with shell commands#36

Open
oloflun wants to merge 1 commit into
aattaran:mainfrom
oloflun:per-session-proxy-isolation
Open

Zero-model mid-session backend switching with shell commands#36
oloflun wants to merge 1 commit into
aattaran:mainfrom
oloflun:per-session-proxy-isolation

Conversation

@oloflun

@oloflun oloflun commented Jun 22, 2026

Copy link
Copy Markdown

What

Switch between DeepSeek and Anthropic backends mid-session without model involvement. Type !ds or !ant directly in Claude Code -- the shell executes the switch before any model call. No /command delay, no skill/agent loading, works even when the current backend is completely unreachable.

How it works

A lightweight 3-server routing proxy (routing-proxy.mjs) sits between Claude Code and the API backends. It reads a mode file on every request and routes to the appropriate backend:

Claude Code -> Router (:3199) -> reads mode file -> DeepSeek (:3200) or Anthropic (:3201)
  • !ds / !ant / !backend are Bash scripts that write to the session mode file. Claude Code's ! prefix runs them at the CLI layer before any API request.
  • Each session gets its own proxy on unique ports with a per-session mode file, so switching one session doesn't affect others.
  • Auto-fallback: if Anthropic returns 401/402/403 (no credits), the router silently retries via DeepSeek and switches the mode.
  • Backward compatible: CLI args for mode file path and router port are optional -- falls back to global defaults.

PowerShell session management (master commands)

From any PowerShell window:

sessions          # list all running sessions with ports and modes
ds                # switch current session to DeepSeek
ant               # switch current session to Anthropic
backend           # show current session backend
ds -All           # master switch: ALL sessions to DeepSeek
ant -All          # master switch: ALL sessions to Anthropic
ds -Session 3202  # target a specific session by port

Functions auto-detect the current session via ANTHROPIC_BASE_URL env var. Auto-refresh from disk -- no manual profile reload needed.

Changes

  • New file: proxy/routing-proxy.mjs -- the routing proxy. Accepts optional 4th arg (mode file path) and 5th arg (router port). Backend ports derived as routerPort+1 / routerPort+2. Status endpoint at /_router/status.

Companion setup (one-time, on user machine)

Three Bash scripts in ~/.local/bin/ and PowerShell profile functions provide the in-session commands and session management. See the companion Gist linked below for full setup.

🤖 Generated with Claude Code

New file: proxy/routing-proxy.mjs

A lightweight 3-server routing proxy enabling mid-session backend
switching without model involvement. Supports per-session isolation
via dynamic ports and per-session mode files. Backward compatible --
defaults to global mode file and port 3199 when CLI args omitted.

Auto-fallback: Anthropic 401/402/403 silently retries via DeepSeek.
Status endpoint at GET /_router/status returns current mode and port.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant